You are here

RouteSubscriber.php in Config Entity Revisions 8.2

File

modules/view_revisions/src/Routing/RouteSubscriber.php
View source
<?php

namespace Drupal\view_revisions\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Class RouteSubscriber.
 *
 * @package Drupal\view_revisions\Routing
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    $latest_revision_routes = [
      'entity.view.edit_form',
    ];
    foreach ($latest_revision_routes as $route_name) {
      if ($route = $collection
        ->get($route_name)) {
        $current = $route
          ->getOption('parameters') ?: [];
        $current['view']['load_latest_revision'] = TRUE;
        $route
          ->setOption('parameters', $current);
      }
    }

    // The following array should match the list in the RouteProcessor.
    $specific_revision_routes = [
      'entity.view.edit_display_form',
      'entity.view.preview_form',
      'views_ui.form_add_handler',
      'views_ui.form_edit_details',
      'views_ui.form_reorder_displays',
      'views_ui.form_analyze',
      'views_ui.form_rearrange',
      'views_ui.form_rearrange_filter',
      'views_ui.form_display',
      'views_ui.form_handler',
      'views_ui.form_handler_extra',
      'views_ui.form_handler_group',
    ];
    foreach ($specific_revision_routes as $route_name) {
      if ($route = $collection
        ->get($route_name)) {
        $current = $route
          ->getOption('parameters') ?: [];
        $current['revision_id'] = TRUE;
        $route
          ->setOption('parameters', $current);
        $path = $route
          ->getPath();
        $path = str_replace('{view}', '{view}/{revision_id}', $path);
        $route
          ->setPath($path);
      }
    }
    foreach ($collection as $route) {
      $options = $route
        ->getOptions();
      if (array_key_exists('_view_display_plugin_id', $options)) {
        $route
          ->setRequirement('_can_access_views_revision', 'TRUE');
      }
    }
  }

}

Classes

Namesort descending Description
RouteSubscriber Class RouteSubscriber.