Skip to content

Instantly share code, notes, and snippets.

@msankhala
Created June 21, 2021 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msankhala/4446763ade115438f4da36b386ae910f to your computer and use it in GitHub Desktop.
Save msankhala/4446763ade115438f4da36b386ae910f to your computer and use it in GitHub Desktop.
custom mode form to entities
<?php
// Source https://www.flocondetoile.fr/blog/provide-custom-mode-form-entities-drupal-8
/**
* Implements hook_entity_type_build().
*/
function my_module_entity_type_build(array &$entity_types) {
$entity_types['user']->setFormClass('profil', 'Drupal\user\ProfileForm');
}
// my_module.routing.yml,
my_module.user.profile:
path: '/user/{user}/profil'
defaults:
_entity_form: 'user.profil'
_title: 'Profil'
requirements:
_entity_access: 'user.update'
_custom_access: '\Drupal\my_module\Access\MyModuleUserAccess::editProfil'
user: \d+
options:
_admin_route: FALSE
// my_module.links.menu.yml
my_module.user.profil:
title: 'Profil'
weight: 10
route_name: my_module.user.profil
base_route: entity.user.canonical
menu_name: user-account
class: Drupal\my_module\Plugin\Menu\ProfilUserBase
# src/Plugin/Menu
namespace Drupal\my_module\Plugin\Menu;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* Profile Menu Link
*/
class ProfilUserBase extends MenuLinkDefault implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* The current route match service.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $currentRouteMatch;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Constructs a new MenuLinkDefault.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
* The static override storage.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
* The current route match service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $current_route_match, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
$this->entityTypeManager = $entity_type_manager;
$this->currentRouteMatch = $current_route_match;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('menu_link.static.overrides'),
$container->get('entity_type.manager'),
$container->get('current_route_match'),
$container->get('current_user')
);
}
public function getRouteParameters() {
return ['user' => $this->getUserIdFromRoute()];
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return ['user', 'url'];
}
/**
* Get the Account user id from the request or fallback to current user.
*
* @return int
*/
public function getUserIdFromRoute() {
$user = $this->currentRouteMatch->getParameter('user');
if ($user instanceof AccountInterface) {
return $user->id();
}
elseif (!empty($user)) {
$user = $this->entityTypeManager->getStorage('user')->load($user);
if($user instanceof AccountInterface) {
return $user->id();
}
}
return $this->currentUser->id();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment