You are here

DefaultHtmlRouteProvider.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php

File

core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
View source
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider.
 */
namespace Drupal\Core\Entity\Routing;

use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Provides HTML routes for entities.
 *
 * This class provides the following routes for entities, with title and access
 * callbacks:
 * - canonical
 * - edit-form
 * - delete-form
 *
 * @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider.
 *
 * @internal
 */
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();
  }

}

Classes

Namesort descending Description
DefaultHtmlRouteProvider Provides HTML routes for entities.