You are here

protected function DefaultHtmlRouteProvider::getAddFormRoute in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider::getAddFormRoute()

Gets the add-form route.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type.

Return value

\Symfony\Component\Routing\Route|null The generated route, if available.

3 calls to DefaultHtmlRouteProvider::getAddFormRoute()
AdminHtmlRouteProvider::getAddFormRoute in core/lib/Drupal/Core/Entity/Routing/AdminHtmlRouteProvider.php
Gets the add-form route.
DefaultHtmlRouteProvider::getRoutes in core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
Provides routes for entities.
TestDefaultHtmlRouteProvider::getAddFormRoute in core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
Gets the add-form route.
2 methods override DefaultHtmlRouteProvider::getAddFormRoute()
AdminHtmlRouteProvider::getAddFormRoute in core/lib/Drupal/Core/Entity/Routing/AdminHtmlRouteProvider.php
Gets the add-form route.
TestDefaultHtmlRouteProvider::getAddFormRoute in core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
Gets the add-form route.

File

core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php, line 141

Class

DefaultHtmlRouteProvider
Provides HTML routes for entities.

Namespace

Drupal\Core\Entity\Routing

Code

protected function getAddFormRoute(EntityTypeInterface $entity_type) {
  if ($entity_type
    ->hasLinkTemplate('add-form')) {
    $entity_type_id = $entity_type
      ->id();
    $route = new Route($entity_type
      ->getLinkTemplate('add-form'));

    // Use the add form handler, if available, otherwise default.
    $operation = 'default';
    if ($entity_type
      ->getFormClass('add')) {
      $operation = 'add';
    }
    $route
      ->setDefaults([
      '_entity_form' => "{$entity_type_id}.{$operation}",
      'entity_type_id' => $entity_type_id,
    ]);

    // If the entity has bundles, we can provide a bundle-specific title
    // and access requirements.
    $expected_parameter = $entity_type
      ->getBundleEntityType() ?: $entity_type
      ->getKey('bundle');

    // @todo: We have to check if a route contains a bundle in its path as
    //   test entities have inconsistent usage of "add-form" link templates.
    //   Fix it in https://www.drupal.org/node/2699959.
    if (($bundle_key = $entity_type
      ->getKey('bundle')) && strpos($route
      ->getPath(), '{' . $expected_parameter . '}') !== FALSE) {
      $route
        ->setDefault('_title_callback', EntityController::class . '::addBundleTitle');

      // If the bundles are entities themselves, we can add parameter
      // information to the route options.
      if ($bundle_entity_type_id = $entity_type
        ->getBundleEntityType()) {
        $bundle_entity_type = $this->entityTypeManager
          ->getDefinition($bundle_entity_type_id);
        $route
          ->setDefault('bundle_parameter', $bundle_entity_type_id)
          ->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}');

        // Entity types with serial IDs can specify this in their route
        // requirements, improving the matching process.
        if ($this
          ->getEntityTypeIdKeyType($bundle_entity_type) === 'integer') {
          $route
            ->setRequirement($entity_type_id, '\\d+');
        }
        $bundle_entity_parameter = [
          'type' => 'entity:' . $bundle_entity_type_id,
        ];
        if ($bundle_entity_type instanceof ConfigEntityTypeInterface) {

          // The add page might be displayed on an admin path. Even then, we
          // need to load configuration overrides so that, for example, the
          // bundle label gets translated correctly.
          // @see \Drupal\Core\ParamConverter\AdminPathConfigEntityConverter
          $bundle_entity_parameter['with_config_overrides'] = TRUE;
        }
        $route
          ->setOption('parameters', [
          $bundle_entity_type_id => $bundle_entity_parameter,
        ]);
      }
      else {

        // If the bundles are not entities, the bundle key is used as the
        // route parameter name directly.
        $route
          ->setDefault('bundle_parameter', $bundle_key)
          ->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_key . '}');
      }
    }
    else {
      $route
        ->setDefault('_title_callback', EntityController::class . '::addTitle')
        ->setRequirement('_entity_create_access', $entity_type_id);
    }
    return $route;
  }
}