You are here

class GroupContentMenuParentFormSelector in Group Content Menu 8

Group content menu implementation of the menu parent form selector service.

The form selector is a list of all appropriate menu links.

Hierarchy

Expanded class hierarchy of GroupContentMenuParentFormSelector

File

src/GroupContentMenuParentFormSelector.php, line 13

Namespace

Drupal\group_content_menu
View source
class GroupContentMenuParentFormSelector extends MenuParentFormSelector {

  /**
   * Determine if menu is a group menu.
   *
   * @var bool
   */
  protected $isGroupMenu = FALSE;

  /**
   * {@inheritdoc}
   */
  public function parentSelectElement($menu_parent, $id = '', array $menus = NULL) {
    if (strpos($menu_parent, GroupContentMenuInterface::MENU_PREFIX) !== FALSE) {
      $this->isGroupMenu = TRUE;
    }
    $element = parent::parentSelectElement($menu_parent, $id, $menus);

    // Add the group content list tag in case a menu is created, deleted, etc.
    $element['#cache']['tags'] = $element['#cache']['tags'] ?? [];
    $element['#cache']['tags'] = Cache::mergeTags($element['#cache']['tags'], [
      'group_content_list',
    ]);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  protected function getMenuOptions(array $menu_names = NULL) {
    $entity_type = 'menu';
    if ($this->isGroupMenu) {
      $entity_type = 'group_content_menu';
    }
    $menus = $this->entityTypeManager
      ->getStorage($entity_type)
      ->loadMultiple($menu_names);
    $options = [];

    /** @var \Drupal\system\MenuInterface[] $menus */
    foreach ($menus as $menu) {
      if ($this->isGroupMenu) {
        if ($route_group = \Drupal::routeMatch()
          ->getParameter('group')) {
          $group_contents = $this->entityTypeManager
            ->getStorage('group_content')
            ->loadByEntity($menu);
          if ($group_contents) {
            $menu_group = array_pop($group_contents)
              ->getGroup();
            if ($menu_group
              ->id() === $route_group
              ->id()) {
              $options[GroupContentMenuInterface::MENU_PREFIX . $menu
                ->id()] = $menu
                ->label() . " ({$menu_group->label()})";
            }
          }
        }
      }
      else {
        $options[$menu
          ->id()] = $menu
          ->label();
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
GroupContentMenuParentFormSelector::$isGroupMenu protected property Determine if menu is a group menu.
GroupContentMenuParentFormSelector::getMenuOptions protected function Gets a list of menu names for use as options. Overrides MenuParentFormSelector::getMenuOptions
GroupContentMenuParentFormSelector::parentSelectElement public function Gets a form element to choose a menu and parent. Overrides MenuParentFormSelector::parentSelectElement
MenuParentFormSelector::$deprecatedProperties protected property
MenuParentFormSelector::$entityTypeManager protected property The entity type manager service.
MenuParentFormSelector::$menuLinkTree protected property The menu link tree service.
MenuParentFormSelector::getParentDepthLimit protected function Returns the maximum depth of the possible parents of the menu link.
MenuParentFormSelector::getParentSelectOptions public function Gets the options for a select element to choose a menu and parent. Overrides MenuParentFormSelectorInterface::getParentSelectOptions
MenuParentFormSelector::parentSelectOptionsTreeWalk protected function Iterates over all items in the tree to prepare the parents select options.
MenuParentFormSelector::__construct public function Constructs a \Drupal\Core\Menu\MenuParentFormSelector
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.