You are here

class MenuBuilder in Context 8.4

Same name and namespace in other branches
  1. 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

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_ui
View 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

Namesort descending Modifiers Type Description Overrides
MenuBuilder::$menuLinkManager protected property The menu link plugin manager.
MenuBuilder::addSubMenuItem public function Adds a submenu item for the $entity item.
MenuBuilder::create public static function
MenuBuilder::deleteSubMenuItem public function Deletes the submenu item of the $entity item.
MenuBuilder::getUriString private function Return the URI string of the given context entity.
MenuBuilder::updateSubMenuItem public function Updates the submenu item of the $entity item.
MenuBuilder::__construct public function MenuBuilder constructor.