You are here

class GroupContentMenuRouteProvider in Group Content Menu 8

Provides routes for group_content_menu content.

Hierarchy

Expanded class hierarchy of GroupContentMenuRouteProvider

File

src/Routing/GroupContentMenuRouteProvider.php, line 14

Namespace

Drupal\group_content_menu\Routing
View source
class GroupContentMenuRouteProvider extends DefaultHtmlRouteProvider {

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Gets the add-menu-link route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  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);
    }
  }

  /**
   * Gets the edit-menu-link route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  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);
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * Gets the collection route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getCollectionRoute(EntityTypeInterface $entity_type) {
    if ($entity_type
      ->hasLinkTemplate('collection') && $entity_type
      ->hasListBuilderClass()) {

      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $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',
        ],
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  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',
        ],
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  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',
        ],
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  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',
        ],
      ]);
    }
  }

  /**
   * Get create permissions.
   *
   * @return array
   *   List of create permissions.
   */
  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',
    ];
  }

  /**
   * Get plugin IDs.
   *
   * @return array
   *   The plugin IDs.
   */
  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',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultHtmlRouteProvider::$entityFieldManager protected property The entity field manager.
DefaultHtmlRouteProvider::$entityTypeManager protected property The entity type manager.
DefaultHtmlRouteProvider::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance 1
DefaultHtmlRouteProvider::getDeleteMultipleFormRoute protected function Returns the delete multiple form route. 1
DefaultHtmlRouteProvider::getEntityTypeIdKeyType protected function Gets the type of the ID key for a given entity type. 1
DefaultHtmlRouteProvider::__construct public function Constructs a new DefaultHtmlRouteProvider. 1
GroupContentMenuRouteProvider::getAddFormRoute protected function Gets the add-form route. Overrides DefaultHtmlRouteProvider::getAddFormRoute
GroupContentMenuRouteProvider::getAddMenuLink protected function Gets the add-menu-link route.
GroupContentMenuRouteProvider::getAddPageRoute protected function Gets the add page route. Overrides DefaultHtmlRouteProvider::getAddPageRoute
GroupContentMenuRouteProvider::getCanonicalRoute protected function Gets the canonical route. Overrides DefaultHtmlRouteProvider::getCanonicalRoute
GroupContentMenuRouteProvider::getCollectionRoute protected function Gets the collection route. Overrides DefaultHtmlRouteProvider::getCollectionRoute
GroupContentMenuRouteProvider::getCreatePermissions protected function Get create permissions.
GroupContentMenuRouteProvider::getDeleteFormRoute protected function Gets the delete-form route. Overrides DefaultHtmlRouteProvider::getDeleteFormRoute
GroupContentMenuRouteProvider::getEditFormRoute protected function Gets the edit-form route. Overrides DefaultHtmlRouteProvider::getEditFormRoute
GroupContentMenuRouteProvider::getEditMenuLink protected function Gets the edit-menu-link route.
GroupContentMenuRouteProvider::getPluginIds protected function Get plugin IDs.
GroupContentMenuRouteProvider::getRoutes public function Provides routes for entities. Overrides DefaultHtmlRouteProvider::getRoutes