GroupContentMenuParentFormSelector.php in Group Content Menu 8
File
src/GroupContentMenuParentFormSelector.php
View source
<?php
namespace Drupal\group_content_menu;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Menu\MenuParentFormSelector;
class GroupContentMenuParentFormSelector extends MenuParentFormSelector {
protected $isGroupMenu = FALSE;
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);
$element['#cache']['tags'] = $element['#cache']['tags'] ?? [];
$element['#cache']['tags'] = Cache::mergeTags($element['#cache']['tags'], [
'group_content_list',
]);
return $element;
}
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 = [];
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;
}
}