Commit 86f2ca41 authored by Paulo Henrique Cota Starling's avatar Paulo Henrique Cota Starling
Browse files

Issue #3280556 by paulocs, lucassc: Why does Context UI module create a menu...

Issue #3280556 by paulocs, lucassc: Why does Context UI module create a menu link for each context you create?
parent a62e5f82
Loading
Loading
Loading
Loading
+19 −0
Changes for modules/context_ui/context_ui.install: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Install, update and uninstall functions for the context_ui module.
 */

/**
 * Issue #3280556: Remove context menus links.
 */
function context_ui_update_8001() {
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $child_ids = $menu_link_manager->getChildIds('entity.context.collection');
  foreach ($child_ids as $key => $value) {
    if (strpos($value, 'menu_link_content') !== FALSE) {
      $menu_link_manager->removeDefinition($value);
    }
  }
}
+0 −21
Changes for modules/context_ui/context_ui.module: 0 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -4,24 +4,3 @@
 * @file
 * Defines Drupal hooks for context_ui module.
 */

/**
 * Implements hook_entity_insert().
 */
function context_ui_context_insert($entity) {
  Drupal::service('context_ui.menu_builder')->addSubMenuItem($entity);
}

/**
 * Implements hook_entity_update().
 */
function context_ui_context_update($entity) {
  Drupal::service('context_ui.menu_builder')->updateSubMenuItem($entity);
}

/**
 * Implements hook_entity_delete().
 */
function context_ui_context_delete($entity) {
  Drupal::service('context_ui.menu_builder')->deleteSubMenuItem($entity);
}
+0 −4
Changes for modules/context_ui/context_ui.services.yml: 0 added lines, 4 removed lines.
Original line number Diff line number Diff line
services:
  context_ui.menu_builder:
    class: Drupal\context_ui\MenuBuilder
    arguments: ['@plugin.manager.menu.link']
+0 −122
Changes for modules/context_ui/src/MenuBuilder.php: 0 added lines, 122 removed lines.
Original line number Diff line number Diff line
<?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.
 *
 * MenuBuilder configures and updates the submenu context items.
 *
 * @package Drupal\context_ui
 */
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();
  }

}