View source
<?php
namespace Drupal\group_content_menu\Routing;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
use Drupal\group_content_menu\Controller\GroupContentMenuController;
use Drupal\group_content_menu\Entity\GroupContentMenuType;
use Symfony\Component\Routing\Route;
class GroupContentMenuRouteProvider extends DefaultHtmlRouteProvider {
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
if ($add_menu_link = $this
->getAddMenuLink($entity_type)) {
$collection
->add('entity.group_content_menu.add_link', $add_menu_link);
}
if ($add_menu_link = $this
->getEditMenuLink($entity_type)) {
$collection
->add('entity.group_content_menu.edit_link', $add_menu_link);
}
return $collection;
}
protected function getAddMenuLink(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('add-menu-link')) {
$route = new Route($entity_type
->getLinkTemplate('add-menu-link'));
return $route
->setDefaults([
'_title' => 'Add menu link',
'_controller' => sprintf('%s::addLink', GroupContentMenuController::class),
])
->setRequirement('_group_permission', implode('+', $this
->getCreatePermissions()))
->setRequirement('_group_installed_content', implode('+', $this
->getPluginIds()))
->setOption('parameters', [
'group' => [
'type' => 'entity:group',
],
'group_content_menu' => [
'type' => 'entity:group_content_menu',
],
])
->setOption('_group_operation_route', TRUE);
}
}
protected function getEditMenuLink(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('edit-menu-link')) {
$route = new Route($entity_type
->getLinkTemplate('edit-menu-link'));
return $route
->setDefaults([
'_title' => 'Edit menu link',
'_controller' => sprintf('%s::editLink', GroupContentMenuController::class),
])
->setRequirement('_group_permission', 'manage group_content_menu')
->setRequirement('_group_installed_content', implode('+', $this
->getPluginIds()))
->setOption('parameters', [
'group' => [
'type' => 'entity:group',
],
'group_content_menu' => [
'type' => 'entity:group_content_menu',
],
'menu_link_content' => [
'type' => 'entity:menu_link_content',
],
])
->setOption('_group_operation_route', TRUE);
}
}
protected function getAddPageRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getAddPageRoute($entity_type)) {
return $route
->setDefaults([
'_title' => 'Add new menu',
'_controller' => sprintf('%s::addPage', GroupContentMenuController::class),
])
->setRequirement('_group_permission', 'manage group_content_menu')
->setRequirement('_group_installed_content', implode('+', $this
->getPluginIds()))
->setOption('_group_operation_route', TRUE);
}
}
protected function getAddFormRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getAddFormRoute($entity_type)) {
$requirements = $route
->getRequirements();
unset($requirements['_entity_create_access']);
$route
->setRequirements($requirements);
return $route
->setDefault('_title', 'Add new menu')
->setDefault('_controller', sprintf('%s::createForm', GroupContentMenuController::class))
->setRequirement('_group_permission', 'manage group_content_menu')
->setRequirement('_group_installed_content', implode('+', $this
->getPluginIds()))
->setOption('_group_operation_route', TRUE);
}
}
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('collection') && $entity_type
->hasListBuilderClass()) {
$label = $entity_type
->getCollectionLabel();
$route = new Route($entity_type
->getLinkTemplate('collection'));
return $route
->addDefaults([
'_entity_list' => $entity_type
->id(),
'_title' => $label
->getUntranslatedString(),
'_title_arguments' => $label
->getArguments(),
'_title_context' => $label
->getOption('context'),
])
->setOption('_group_operation_route', TRUE)
->setRequirement('_group_permission', 'access group content menu overview')
->setOption('parameters', [
'group' => [
'type' => 'entity:group',
],
]);
}
}
protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getCanonicalRoute($entity_type)) {
$requirements = $route
->getRequirements();
unset($requirements['_entity_access']);
$route
->setRequirements($requirements);
return $route
->setRequirement('_group_menu_owns_content', 'TRUE')
->setOption('_group_operation_route', TRUE)
->setOption('parameters', [
'group' => [
'type' => 'entity:group',
],
'group_content_menu' => [
'type' => 'entity:group_content_menu',
],
]);
}
}
protected function getEditFormRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getEditFormRoute($entity_type)) {
$requirements = $route
->getRequirements();
unset($requirements['_entity_access']);
$route
->setRequirements($requirements);
return $route
->setRequirement('_group_menu_owns_content', 'TRUE')
->setRequirement('_group_permission', 'manage group_content_menu')
->setOption('_group_operation_route', TRUE)
->setOption('parameters', [
'group' => [
'type' => 'entity:group',
],
'group_content_menu' => [
'type' => 'entity:group_content_menu',
],
]);
}
}
protected function getDeleteFormRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getDeleteFormRoute($entity_type)) {
$requirements = $route
->getRequirements();
unset($requirements['_entity_access']);
$route
->setRequirements($requirements);
return $route
->setRequirement('_group_menu_owns_content', 'TRUE')
->setRequirement('_group_permission', 'manage group_content_menu')
->setOption('_group_operation_route', TRUE)
->setOption('parameters', [
'group' => [
'type' => 'entity:group',
],
'group_content_menu' => [
'type' => 'entity:group_content_menu',
],
]);
}
}
protected function getCreatePermissions() {
$permissions = [];
foreach (array_keys(GroupContentMenuType::loadMultiple()) as $entity_type_id) {
$permissions[] = "create group_content_menu:{$entity_type_id} content";
}
return $permissions ?: [
'access group content menu overview',
];
}
protected function getPluginIds() {
$plugin_ids = [];
foreach (array_keys(GroupContentMenuType::loadMultiple()) as $entity_type_id) {
$plugin_ids[] = "group_content_menu:{$entity_type_id}";
}
return $plugin_ids ?: [
'group_content_menu',
];
}
}