class MenuBuilder in Context 8.4
Same name and namespace in other branches
- 8 modules/context_ui/src/MenuBuilder.php \Drupal\context_ui\MenuBuilder
Implements the MenuBuilder class.
MenuBuilder configures and updates the submenu context items.
@package Drupal\context_ui
Hierarchy
- class \Drupal\context_ui\MenuBuilder
Expanded class hierarchy of MenuBuilder
1 string reference to 'MenuBuilder'
- context_ui.services.yml in modules/
context_ui/ context_ui.services.yml - modules/context_ui/context_ui.services.yml
1 service uses MenuBuilder
- context_ui.menu_builder in modules/
context_ui/ context_ui.services.yml - Drupal\context_ui\MenuBuilder
File
- modules/
context_ui/ src/ MenuBuilder.php, line 17
Namespace
Drupal\context_uiView source
class MenuBuilder {
/**
* The menu link plugin manager.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* MenuBuilder constructor.
*
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* The menu link plugin manager.
*/
public function __construct(MenuLinkManagerInterface $menuLinkManager) {
$this->menuLinkManager = $menuLinkManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.menu.link'));
}
/**
* Adds a submenu item for the $entity item.
*
* @param \Drupal\context\Entity\Context $entity
* The given entity item.
*/
public function addSubMenuItem(Context $entity) {
$menu_link = MenuLinkContent::create([
'title' => $entity
->getLabel(),
'link' => $this
->getUriString($entity),
'menu_name' => 'admin',
'parent' => 'entity.context.collection',
'expanded' => TRUE,
'weight' => 10,
]);
$menu_link
->save();
$this->menuLinkManager
->rebuild();
}
/**
* Updates the submenu item of the $entity item.
*
* @param \Drupal\context\Entity\Context $entity
* The given entity item.
*/
public function updateSubMenuItem(Context $entity) {
$result = $this->menuLinkManager
->loadLinksByRoute('entity.context.edit_form', [
'context' => $entity
->id(),
]);
if (!empty($result)) {
foreach ($result as $id => $instance) {
if (strpos($id, 'menu_link_content:') === 0) {
$instance
->updateLink([
'title' => $entity
->getLabel(),
'link' => $this
->getUriString($entity),
], TRUE);
}
}
$this->menuLinkManager
->rebuild();
}
else {
$this
->addSubMenuItem($entity);
}
}
/**
* Deletes the submenu item of the $entity item.
*
* @param \Drupal\context\Entity\Context $entity
* The given entity item.
*/
public function deleteSubMenuItem(Context $entity) {
$result = $this->menuLinkManager
->loadLinksByRoute('entity.context.edit_form', [
'context' => $entity
->id(),
]);
if (!empty($result)) {
foreach ($result as $id => $instance) {
if ($instance
->isDeletable() && strpos($id, 'menu_link_content:') === 0) {
$instance
->deleteLink();
$this->menuLinkManager
->rebuild();
}
}
}
$this->menuLinkManager
->rebuild();
}
/**
* Return the URI string of the given context entity.
*
* @param \Drupal\context\Entity\Context $entity
* The context entity.
*
* @return string
* The URI string.
*/
private function getUriString(Context $entity) {
$url = Url::fromRoute('entity.context.edit_form', [
'context' => $entity
->id(),
]);
return $url
->toUriString();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MenuBuilder:: |
protected | property | The menu link plugin manager. | |
MenuBuilder:: |
public | function | Adds a submenu item for the $entity item. | |
MenuBuilder:: |
public static | function | ||
MenuBuilder:: |
public | function | Deletes the submenu item of the $entity item. | |
MenuBuilder:: |
private | function | Return the URI string of the given context entity. | |
MenuBuilder:: |
public | function | Updates the submenu item of the $entity item. | |
MenuBuilder:: |
public | function | MenuBuilder constructor. |