class AddMenuOverride in Entity Reference Hierarchy 3.x
Same name and namespace in other branches
- 8.2 modules/entity_hierarchy_microsite/src/Controller/AddMenuOverride.php \Drupal\entity_hierarchy_microsite\Controller\AddMenuOverride
Defines a class for a controller for adding a menu overrride.
Hierarchy
- class \Drupal\entity_hierarchy_microsite\Controller\AddMenuOverride implements ContainerInjectionInterface
Expanded class hierarchy of AddMenuOverride
File
- modules/
entity_hierarchy_microsite/ src/ Controller/ AddMenuOverride.php, line 16
Namespace
Drupal\entity_hierarchy_microsite\ControllerView source
class AddMenuOverride implements ContainerInjectionInterface {
/**
* Form builder.
*
* @var \Drupal\Core\Entity\EntityFormBuilderInterface
*/
private $entityFormBuilder;
/**
* Menu link manager.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
private $menuLinkManager;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* Constructs a new AddMenuOverride.
*
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entityFormBuilder
* Entity form builder.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* Menu link manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(EntityFormBuilderInterface $entityFormBuilder, MenuLinkManagerInterface $menuLinkManager, EntityTypeManagerInterface $entityTypeManager) {
$this->entityFormBuilder = $entityFormBuilder;
$this->menuLinkManager = $menuLinkManager;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.form_builder'), $container
->get('plugin.manager.menu.link'), $container
->get('entity_type.manager'));
}
/**
* Route callback for adding a new override.
*
* @param string $target
* Target UUID.
*
* @return array
* Form.
*/
public function addMenuOverride(string $target) {
$plugin_id = 'entity_hierarchy_microsite:' . $target;
if (!$this->menuLinkManager
->hasDefinition($plugin_id)) {
throw new NotFoundHttpException();
}
$storage = $this->entityTypeManager
->getStorage('eh_microsite_menu_override');
if ($storage
->loadByProperties([
'target' => $target,
])) {
throw new NotFoundHttpException();
}
/** @var \Drupal\entity_hierarchy_microsite\Plugin\Menu\MicrositeMenuItem $instance */
$instance = $this->menuLinkManager
->createInstance($plugin_id);
$override = $storage
->create([
'target' => $target,
'enabled' => $instance
->isEnabled(),
'weight' => $instance
->getWeight(),
'title' => $instance
->getTitle(),
'parent' => $instance
->getParent(),
]);
return $this->entityFormBuilder
->getForm($override);
}
/**
* Title callback for adding a new override.
*
* @param string $target
* Target UUID.
*
* @return string
* Title.
*/
public function title(string $target) {
$plugin_id = 'entity_hierarchy_microsite:' . $target;
if (!$this->menuLinkManager
->hasDefinition($plugin_id)) {
return '';
}
/** @var \Drupal\entity_hierarchy_microsite\Plugin\Menu\MicrositeMenuItem $instance */
$instance = $this->menuLinkManager
->createInstance($plugin_id);
return new TranslatableMarkup('Add an override for @label', [
'@label' => $instance
->getTitle(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddMenuOverride:: |
private | property | Form builder. | |
AddMenuOverride:: |
private | property | Entity type manager. | |
AddMenuOverride:: |
private | property | Menu link manager. | |
AddMenuOverride:: |
public | function | Route callback for adding a new override. | |
AddMenuOverride:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
AddMenuOverride:: |
public | function | Title callback for adding a new override. | |
AddMenuOverride:: |
public | function | Constructs a new AddMenuOverride. |