getLinkTemplate example


  protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('latest-version') && $entity_type->hasViewBuilderClass()) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('latest-version'));
      $route
        ->addDefaults([
          '_entity_view' => "{$entity_type_id}.full",
          '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
        ])
        // If the entity type is a node, unpublished content will be visible         // if the user has the "view any unpublished content" permission.         ->setRequirement('_entity_access', "{$entity_type_id}.view")
        ->setRequirement('_content_moderation_latest_version', 'TRUE')
        ->setOption('_content_moderation_entity_type', $entity_type_id)
        ->setOption('parameters', [
          
$defaults = [];
      $defaults['entity_type_id'] = $entity_type_id;

      // Retrieve the requirements from the canonical route.       $requirements = $collection->get("entity.$entity_type_id.canonical")->getRequirements();

      $options = [];
      // Ensure that upcasting is run in the correct order.       $options['parameters']['section_storage'] = [];
      $options['parameters'][$entity_type_id]['type'] = 'entity:' . $entity_type_id;

      $template = $entity_type->getLinkTemplate('canonical') . '/layout';
      $this->buildLayoutRoutes($collection$this->getPluginDefinition()$template$defaults$requirements$options$entity_type_id$entity_type_id);
    }
  }

  /** * {@inheritdoc} */
  public function buildLocalTasks($base_plugin_definition) {
    $local_tasks = [];
    foreach ($this->getEntityTypes() as $entity_type_id => $entity_type) {
      $local_tasks["layout_builder.overrides.$entity_type_id.view"] = $base_plugin_definition + [
        
protected function getEntityPermissionsRoute(EntityTypeInterface $entity_type): ?Route {
    if (!$entity_type->hasLinkTemplate('entity-permissions-form')) {
      return NULL;
    }

    if (!$bundle_of_id = $entity_type->getBundleOf()) {
      return NULL;
    }

    $entity_type_id = $entity_type->id();
    $route = new Route(
      $entity_type->getLinkTemplate('entity-permissions-form'),
      [
        '_title' => 'Manage permissions',
        '_form' => 'Drupal\user\Form\EntityPermissionsForm',
        'entity_type_id' => $bundle_of_id,
        'bundle_entity_type' => $entity_type_id,
      ],
      [
        '_permission' => 'administer permissions',
      ],
      [
        // Indicate that Drupal\Core\Entity\Enhancer\EntityBundleRouteEnhancer should
'label' => $entity_type->getLabel(),
        ];

        $default_uris = [
          'canonical' => "/entity/$entity_type_id/" . '{' . $entity_type_id . '}',
          'create' => "/entity/$entity_type_id",
        ];

        foreach ($default_uris as $link_relation => $default_uri) {
          // Check if there are link templates defined for the entity type and           // use the path from the route instead of the default.           if ($link_template = $entity_type->getLinkTemplate($link_relation)) {
            $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $link_template;
          }
          else {
            $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $default_uri;
          }
        }

        $this->derivatives[$entity_type_id] += $base_plugin_definition;
      }
    }
    return $this->derivatives;
  }

  protected function getVersionHistoryRoute(EntityTypeInterface $entityType): ?Route {
    if (!$entityType->hasLinkTemplate('version-history')) {
      return NULL;
    }

    $entityTypeId = $entityType->id();
    return (new Route($entityType->getLinkTemplate('version-history')))
      ->addDefaults([
        '_controller' => VersionHistoryController::class,
        '_title' => 'Revisions',
      ])
      ->setRequirement('_entity_access', $entityTypeId . '.view all revisions')
      ->setOption('entity_type_id', $entityTypeId)
      ->setOption('_admin_route', TRUE)
      ->setOption('parameters', [
        $entityTypeId => [
          'type' => 'entity:' . $entityTypeId,
        ],
      ]);
// Inherit admin route status from edit route, if exists.       $is_admin = FALSE;
      $route_name = "entity.$entity_type_id.edit_form";
      if ($edit_route = $collection->get($route_name)) {
        $is_admin = (bool) $edit_route->getOption('_admin_route');
      }

      $load_latest_revision = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id);

      if ($entity_type->hasLinkTemplate('drupal:content-translation-overview')) {
        $route = new Route(
          $entity_type->getLinkTemplate('drupal:content-translation-overview'),
          [
            '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
            'entity_type_id' => $entity_type_id,
          ],
          [
            '_entity_access' => $entity_type_id . '.view',
            '_access_content_translation_overview' => $entity_type_id,
          ],
          [
            'parameters' => [
              $entity_type_id => [
                
    return $has_canonical_url ? $this->entity->toUrl('canonical') : Url::fromUri('base:entity/' . static::$entityTypeId . '/' . $this->entity->id());
  }

  /** * Gets an entity resource's POST URL. * * @return \Drupal\Core\Url * The URL to POST to. */
  protected function getEntityResourcePostUrl() {
    $has_create_url = $this->entity->hasLinkTemplate('create');
    return $has_create_url ? Url::fromUri('internal:' . $this->entity->getEntityType()->getLinkTemplate('create')) : Url::fromUri('base:entity/' . static::$entityTypeId);
  }

  /** * Clones the given entity and modifies all PATCH-protected fields. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being tested and to modify. * * @return array * Contains two items: * 1. The modified entity object. * 2. The original field values, keyed by field name. * * @internal */
$no_canonical_link->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $no_canonical_link->hasViewBuilderClass()->willReturn(TRUE);
    $no_canonical_link->hasLinkTemplate('canonical')->willReturn(FALSE);
    $no_canonical_link->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $entity_types['no_canonical_link'] = $no_canonical_link->reveal();
    $this->entityFieldManager->getFieldStorageDefinitions('no_canonical_link')->shouldNotBeCalled();

    $canonical_link_no_route = $this->prophesize(EntityTypeInterface::class);
    $canonical_link_no_route->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $canonical_link_no_route->hasViewBuilderClass()->willReturn(TRUE);
    $canonical_link_no_route->hasLinkTemplate('canonical')->willReturn(TRUE);
    $canonical_link_no_route->getLinkTemplate('canonical')->willReturn('/entity/{entity}');
    $canonical_link_no_route->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $entity_types['canonical_link_no_route'] = $canonical_link_no_route->reveal();
    $this->entityFieldManager->getFieldStorageDefinitions('canonical_link_no_route')->shouldNotBeCalled();

    $from_canonical = $this->prophesize(EntityTypeInterface::class);
    $from_canonical->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
    $from_canonical->hasViewBuilderClass()->willReturn(TRUE);
    $from_canonical->hasLinkTemplate('canonical')->willReturn(TRUE);
    $from_canonical->getLinkTemplate('canonical')->willReturn('/entity/{entity}');
    $from_canonical->hasHandlerClass('form', 'layout_builder')->willReturn(TRUE);
    $entity_types['from_canonical'] = $from_canonical->reveal();

    

  protected function getAddPageRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('add-page') && $entity_type->getKey('bundle')) {
      $route = new Route($entity_type->getLinkTemplate('add-page'));
      $route->setDefault('_controller', EntityController::class D '::addPage');
      $route->setDefault('_title_callback', EntityController::class D '::addTitle');
      $route->setDefault('entity_type_id', $entity_type->id());
      $route->setRequirement('_entity_create_any_access', $entity_type->id());

      return $route;
    }
  }

  /** * Gets the add-form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */
$entity_type1 = static::getEntityType();
    $entity_type1->hasLinkTemplate('add-page')->willReturn(FALSE);
    $data['no_add_page_link_template'] = [NULL, $entity_type1->reveal()];

    $entity_type2 = static::getEntityType();
    $entity_type2->hasLinkTemplate('add-page')->willReturn(TRUE);
    $entity_type2->getKey('bundle')->willReturn(NULL);
    $data['no_bundle'] = [NULL, $entity_type2->reveal()];

    $entity_type3 = static::getEntityType();
    $entity_type3->hasLinkTemplate('add-page')->willReturn(TRUE);
    $entity_type3->getLinkTemplate('add-page')->willReturn('/the/add/page/link/template');
    $entity_type3->id()->willReturn('the_entity_type_id');
    $entity_type3->getKey('bundle')->willReturn('type');
    $route = new Route('/the/add/page/link/template');
    $route->setDefaults([
      '_controller' => 'Drupal\Core\Entity\Controller\EntityController::addPage',
      '_title_callback' => 'Drupal\Core\Entity\Controller\EntityController::addTitle',
      'entity_type_id' => 'the_entity_type_id',
    ]);
    $route->setRequirement('_entity_create_any_access', 'the_entity_type_id');
    $data['add_page'] = [clone $route$entity_type3->reveal()];

    

  protected function checkRevisionViewAccess(EntityInterface $entity, AccountInterface $account) {
    assert($entity instanceof RevisionableInterface);
    assert(!$entity->isDefaultRevision(), 'It is not necessary to check revision access when the entity is the default revision.');
    $entity_type = $entity->getEntityType();
    $access = $entity->access('view all revisions', $account, TRUE);
    // Apply content_moderation's additional access logic.     // @see \Drupal\content_moderation\Access\LatestRevisionCheck::access()     if ($entity_type->getLinkTemplate('latest-version') && $entity->isLatestRevision() && isset($this->latestRevisionCheck)) {
      // The latest revision access checker only expects to be invoked by the       // routing system, which makes it necessary to fake a route match.       $routes = $this->router->getRouteCollection();
      $resource_type = $this->resourceTypeRepository->get($entity->getEntityTypeId()$entity->bundle());
      $route_name = sprintf('jsonapi.%s.individual', $resource_type->getTypeName());
      $route = $routes->get($route_name);
      $route->setOption('_content_moderation_entity_type', 'entity');
      $route_match = new RouteMatch($route_name$route['entity' => $entity]['entity' => $entity->uuid()]);
      $moderation_access_result = $this->latestRevisionCheck->access($route$route_match$account);
      $access = $access->andIf($moderation_access_result);
    }
    

  protected function addModerationToEntityType(ContentEntityTypeInterface $type) {
    if (!$type->hasHandlerClass('moderation')) {
      $handler_class = !empty($this->moderationHandlers[$type->id()]) ? $this->moderationHandlers[$type->id()] : ModerationHandler::class;
      $type->setHandlerClass('moderation', $handler_class);
    }

    if (!$type->hasLinkTemplate('latest-version') && $type->hasLinkTemplate('canonical')) {
      $type->setLinkTemplate('latest-version', $type->getLinkTemplate('canonical') . '/latest');
    }

    $providers = $type->getRouteProviderClasses() ?: [];
    if (empty($providers['moderation'])) {
      $providers['moderation'] = EntityModerationRouteProvider::class;
      $type->setHandlerClass('route_provider', $providers);
    }

    return $type;
  }

  
Home | Imprint | This part of the site doesn't use cookies.