You are here

class PreviewLinkRouteProvider in Preview Link 8

Same name and namespace in other branches
  1. 2.x src/Routing/PreviewLinkRouteProvider.php \Drupal\preview_link\Routing\PreviewLinkRouteProvider
  2. 2.0.x src/Routing/PreviewLinkRouteProvider.php \Drupal\preview_link\Routing\PreviewLinkRouteProvider

Preview Link route provider.

Hierarchy

Expanded class hierarchy of PreviewLinkRouteProvider

1 file declares its use of PreviewLinkRouteProvider
preview_link.module in ./preview_link.module
Module file.

File

src/Routing/PreviewLinkRouteProvider.php, line 13

Namespace

Drupal\preview_link\Routing
View source
class PreviewLinkRouteProvider implements EntityRouteProviderInterface {

  /**
   * {@inheritdoc}
   */
  public function getRoutes(EntityTypeInterface $entity_type) {
    $collection = new RouteCollection();
    if ($route = $this
      ->getGeneratePreviewLinkRoute($entity_type)) {
      $entity_type_id = $entity_type
        ->id();
      $collection
        ->add("entity.{$entity_type_id}.generate_preview_link", $route);
    }
    if ($route = $this
      ->getPreviewLinkRoute($entity_type)) {
      $entity_type_id = $entity_type
        ->id();
      $collection
        ->add("entity.{$entity_type_id}.preview_link", $route);
    }
    return $collection;
  }

  /**
   * Gets the route for generating and viewing preview links for this entity.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getGeneratePreviewLinkRoute(EntityTypeInterface $entity_type) {
    if (!$entity_type
      ->hasLinkTemplate('canonical')) {
      return NULL;
    }
    $entity_type_id = $entity_type
      ->id();
    $route = new Route($entity_type
      ->getLinkTemplate('canonical') . '/generate-preview-link');
    $route
      ->setDefaults([
      '_entity_form' => "preview_link.preview_link",
      '_title' => 'Preview',
    ])
      ->setRequirement('_permission', 'generate preview links')
      ->setRequirement('_access_preview_enabled', 'TRUE')
      ->setOption('preview_link.entity_type_id', $entity_type_id)
      ->setOption('parameters', [
      $entity_type_id => [
        'type' => 'entity:' . $entity_type_id,
      ],
    ]);
    return $route;
  }

  /**
   * Gets the preview 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 getPreviewLinkRoute(EntityTypeInterface $entity_type) {
    if (!$entity_type
      ->hasLinkTemplate('canonical')) {
      return NULL;
    }
    $entity_type_id = $entity_type
      ->id();
    $route = new Route('/preview-link/' . $entity_type_id . '/{' . $entity_type_id . '}/{preview_token}');
    $route
      ->setDefaults([
      '_controller' => 'Drupal\\preview_link\\Controller\\PreviewLinkController::preview',
      '_title_callback' => 'Drupal\\preview_link\\Controller\\PreviewLinkController::title',
    ])
      ->setRequirement('_entity_access', $entity_type_id . '.view')
      ->setRequirement('_access_preview_enabled', 'TRUE')
      ->setOption('preview_link.entity_type_id', $entity_type_id)
      ->setOption('parameters', [
      $entity_type_id => [
        'type' => 'entity:' . $entity_type_id,
        'load_latest_revision' => TRUE,
      ],
      'preview_token' => [
        'type' => 'string',
      ],
    ])
      ->setOption('_preview_link_route', TRUE);
    return $route;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreviewLinkRouteProvider::getGeneratePreviewLinkRoute protected function Gets the route for generating and viewing preview links for this entity.
PreviewLinkRouteProvider::getPreviewLinkRoute protected function Gets the preview link route.
PreviewLinkRouteProvider::getRoutes public function Provides routes for entities. Overrides EntityRouteProviderInterface::getRoutes