You are here

class DefaultHtmlRouteProvider in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider

Provides HTML routes for entities.

This class provides the following routes for entities, with title and access callbacks:

  • canonical
  • edit-form
  • delete-form

@internal

Hierarchy

Expanded class hierarchy of DefaultHtmlRouteProvider

See also

\Drupal\Core\Entity\Routing\AdminHtmlRouteProvider.

1 file declares its use of DefaultHtmlRouteProvider
DefaultHtmlRouteProviderTest.php in core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
Contains \Drupal\Tests\Core\Entity\Routing\DefaultHtmlRouteProviderTest.

File

core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php, line 31
Contains \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider.

Namespace

Drupal\Core\Entity\Routing
View source
class DefaultHtmlRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs a new DefaultHtmlRouteProvider.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($container
      ->get('entity.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getRoutes(EntityTypeInterface $entity_type) {
    $collection = new RouteCollection();
    $entity_type_id = $entity_type
      ->id();
    if ($edit_route = $this
      ->getEditFormRoute($entity_type)) {
      $collection
        ->add("entity.{$entity_type_id}.edit_form", $edit_route);
    }
    if ($canonical_route = $this
      ->getCanonicalRoute($entity_type)) {
      $collection
        ->add("entity.{$entity_type_id}.canonical", $canonical_route);
    }
    if ($delete_route = $this
      ->getDeleteFormRoute($entity_type)) {
      $collection
        ->add("entity.{$entity_type_id}.delete_form", $delete_route);
    }
    return $collection;
  }

  /**
   * Gets the canonical route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
    if ($entity_type
      ->hasLinkTemplate('canonical') && $entity_type
      ->hasViewBuilderClass()) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($entity_type
        ->getLinkTemplate('canonical'));
      $route
        ->addDefaults([
        '_entity_view' => "{$entity_type_id}.full",
        '_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::title',
      ])
        ->setRequirement('_entity_access', "{$entity_type_id}.view")
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);

      // Entity types with serial IDs can specify this in their route
      // requirements, improving the matching process.
      if ($this
        ->getEntityTypeIdKeyType($entity_type) === 'integer') {
        $route
          ->setRequirement($entity_type_id, '\\d+');
      }
      return $route;
    }
  }

  /**
   * Gets the edit-form route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getEditFormRoute(EntityTypeInterface $entity_type) {
    if ($entity_type
      ->hasLinkTemplate('edit-form')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($entity_type
        ->getLinkTemplate('edit-form'));

      // Use the edit form handler, if available, otherwise default.
      $operation = 'default';
      if ($entity_type
        ->getFormClass('edit')) {
        $operation = 'edit';
      }
      $route
        ->setDefaults([
        '_entity_form' => "{$entity_type_id}.{$operation}",
        '_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::editTitle',
      ])
        ->setRequirement('_entity_access', "{$entity_type_id}.update")
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);

      // Entity types with serial IDs can specify this in their route
      // requirements, improving the matching process.
      if ($this
        ->getEntityTypeIdKeyType($entity_type) === 'integer') {
        $route
          ->setRequirement($entity_type_id, '\\d+');
      }
      return $route;
    }
  }

  /**
   * Gets the delete-form route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getDeleteFormRoute(EntityTypeInterface $entity_type) {
    if ($entity_type
      ->hasLinkTemplate('delete-form')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($entity_type
        ->getLinkTemplate('delete-form'));
      $route
        ->addDefaults([
        '_entity_form' => "{$entity_type_id}.delete",
        '_title_callback' => '\\Drupal\\Core\\Entity\\Controller\\EntityController::deleteTitle',
      ])
        ->setRequirement('_entity_access', "{$entity_type_id}.delete")
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);

      // Entity types with serial IDs can specify this in their route
      // requirements, improving the matching process.
      if ($this
        ->getEntityTypeIdKeyType($entity_type) === 'integer') {
        $route
          ->setRequirement($entity_type_id, '\\d+');
      }
      return $route;
    }
  }

  /**
   * Gets the type of the ID key for a given entity type.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   An entity type.
   *
   * @return string|null
   *   The type of the ID key for a given entity type, or NULL if the entity
   *   type does not support fields.
   */
  protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
    if (!$entity_type
      ->isSubclassOf(FieldableEntityInterface::class)) {
      return NULL;
    }
    $field_storage_definitions = $this->entityManager
      ->getFieldStorageDefinitions($entity_type
      ->id());
    return $field_storage_definitions[$entity_type
      ->getKey('id')]
      ->getType();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultHtmlRouteProvider::$entityManager protected property The entity manager.
DefaultHtmlRouteProvider::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
DefaultHtmlRouteProvider::getCanonicalRoute protected function Gets the canonical route. 2
DefaultHtmlRouteProvider::getDeleteFormRoute protected function Gets the delete-form route. 1
DefaultHtmlRouteProvider::getEditFormRoute protected function Gets the edit-form route. 1
DefaultHtmlRouteProvider::getEntityTypeIdKeyType protected function Gets the type of the ID key for a given entity type. 1
DefaultHtmlRouteProvider::getRoutes public function Provides routes for entities. Overrides EntityRouteProviderInterface::getRoutes
DefaultHtmlRouteProvider::__construct public function Constructs a new DefaultHtmlRouteProvider.