groupmenu-8.x-1.0-beta2/src/GroupMenuListBuilder.php
src/GroupMenuListBuilder.php
<?php
namespace Drupal\groupmenu;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\group\Entity\GroupRelationshipType;
use Drupal\menu_ui\MenuListBuilder;
/**
* Override the default menu overview to exclude group menus.
*/
class GroupMenuListBuilder extends MenuListBuilder {
/**
* {@inheritdoc}
*/
public function getEntityIds() {
$plugin_id = 'group_menu:menu';
$group_relationship_types = GroupRelationshipType::loadByPluginId($plugin_id);
if (empty($group_relationship_types)) {
return parent::getEntityIds();
}
// Load all the group menu content to exclude.
/** @var \Drupal\group\Entity\GroupRelationshipInterface[] $group_relationships */
try {
$group_relationships = \Drupal::entityTypeManager()
->getStorage('group_relationship')
->loadByProperties([
'type' => array_keys($group_relationship_types),
]);
}
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
// Exception handling.
$group_relationships = [];
}
$menus = [];
if (!empty($group_relationships)) {
foreach ($group_relationships as $group_relationship) {
$menu = $group_relationship->getEntity();
if (!$menu) {
continue;
}
$menu_name = $menu->id();
if (!in_array($menu_name, $menus)) {
$menus[] = $menu_name;
}
}
}
// Load all menus not used as group content.
$query = $this->getStorage()->getQuery()
->accessCheck(TRUE)
->condition($this->entityType->getKey('id'), $menus, 'NOT IN')
->sort($this->entityType->getKey('id'));
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return $query->execute();
}
}
