Commit 2db5f0bb authored by git's avatar git Committed by boshtian

Issue #2925844 by maticb, krina.addweb, matjaz_zavski: Submenu items for context

parent 830f1623
......@@ -3,3 +3,9 @@ entity.context.collection:
parent: system.admin_structure
description: 'Associate menus, views, blocks, etc. with different contexts to structure your site.'
route_name: entity.context.collection
entity.context.add_form:
title: 'Add new context'
parent: entity.context.collection
description: 'Add a new context.'
route_name: entity.context.add_form
weight: 0
<?php
function context_ui_context_insert($entity) {
Drupal::service('context_ui.menu_builder')->addSubMenuItem($entity);
}
function context_ui_context_update($entity) {
Drupal::service('context_ui.menu_builder')->updateSubMenuItem($entity);
}
function context_ui_context_delete($entity) {
Drupal::service('context_ui.menu_builder')->deleteSubMenuItem($entity);
}
\ No newline at end of file
services:
context_ui.menu_builder:
class: Drupal\context_ui\MenuBuilder
arguments: ['@plugin.manager.menu.link']
\ No newline at end of file
<?php
namespace Drupal\context_ui;
use Drupal\context\Entity\Context;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
/**
* Implements the MenuBuilder class, which configures and updates the submenu
* context items
*
* @package Drupal\context_ui
*/
class MenuBuilder {
/**
* @var \Drupal\Core\Menu\MenuLinkManagerInterface Stores the menuLinkManager interface.
*/
protected $menuLinkManager;
/**
* MenuBuilder constructor.
*
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
*/
public function __construct(MenuLinkManagerInterface $menuLinkManager) {
$this->menuLinkManager = $menuLinkManager;
}
/**
*
* Create function.
* @param \Drupal\context_ui\ContainerInterface $container
*
* @return static
*/
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();
}
}
}
}
/**
*
* 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();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment