ContentTranslationRouteSubscriber.php 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\content_translation\Routing\ContentTranslationRouteSubscriber.
 */

namespace Drupal\content_translation\Routing;

10
use Drupal\content_translation\ContentTranslationManagerInterface;
11
use Drupal\Core\Routing\RouteSubscriberBase;
12
use Drupal\Core\Routing\RoutingEvents;
13
use Symfony\Component\Routing\Route;
14
use Symfony\Component\Routing\RouteCollection;
15 16 17 18

/**
 * Subscriber for entity translation routes.
 */
19
class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
20 21

  /**
22
   * The content translation manager.
23
   *
24
   * @var \Drupal\content_translation\ContentTranslationManagerInterface
25
   */
26 27
  protected $contentTranslationManager;

28 29 30
  /**
   * Constructs a ContentTranslationRouteSubscriber object.
   *
31 32
   * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
   *   The content translation manager.
33
   */
34
  public function __construct(ContentTranslationManagerInterface $content_translation_manager) {
35
    $this->contentTranslationManager = $content_translation_manager;
36 37 38 39 40
  }

  /**
   * {@inheritdoc}
   */
41
  protected function alterRoutes(RouteCollection $collection) {
42
    foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
43
      // Try to get the route from the current collection.
44 45 46 47 48 49 50 51 52
      $link_template = $entity_type->getLinkTemplate('canonical');
      if (strpos($link_template, '/') !== FALSE) {
        $base_path = '/' . $link_template;
      }
      else {
        if (!$entity_route = $collection->get("entity.$entity_type_id.canonical")) {
          continue;
        }
        $base_path = $entity_route->getPath();
53
      }
54

55 56
      // Inherit admin route status from edit route, if exists.
      $is_admin = FALSE;
57 58
      $route_name = "entity.$entity_type_id.edit_form";
      if ($edit_route = $collection->get($route_name)) {
59 60 61
        $is_admin = (bool) $edit_route->getOption('_admin_route');
      }

62 63
      $path = $base_path . '/translations';

64
      $route = new Route(
65
        $path,
66
        array(
67
          '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
68
          'entity_type_id' => $entity_type_id,
69 70
        ),
        array(
71
          '_entity_access' =>  $entity_type_id . '.view',
72
          '_access_content_translation_overview' => $entity_type_id,
73 74 75
        ),
        array(
          'parameters' => array(
76 77 78
            $entity_type_id => array(
              'type' => 'entity:' . $entity_type_id,
            ),
79
          ),
80
          '_admin_route' => $is_admin,
81 82
        )
      );
83 84
      $route_name = "entity.$entity_type_id.content_translation_overview";
      $collection->add($route_name, $route);
85

86 87 88
      $route = new Route(
        $path . '/add/{source}/{target}',
        array(
89
          '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
90 91 92
          'source' => NULL,
          'target' => NULL,
          '_title' => 'Add',
93
          'entity_type_id' => $entity_type_id,
94

95 96
        ),
        array(
97
          '_entity_access' =>  $entity_type_id . '.view',
98 99 100 101
          '_access_content_translation_manage' => 'create',
        ),
        array(
          'parameters' => array(
102 103 104 105 106
            'source' => array(
              'type' => 'language',
            ),
            'target' => array(
              'type' => 'language',
107
            ),
108 109 110
            $entity_type_id => array(
              'type' => 'entity:' . $entity_type_id,
            ),
111
          ),
112
          '_admin_route' => $is_admin,
113 114
        )
      );
115
      $collection->add("entity.$entity_type_id.content_translation_add", $route);
116

117 118 119
      $route = new Route(
        $path . '/edit/{language}',
        array(
120
          '_controller' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
121 122
          'language' => NULL,
          '_title' => 'Edit',
123
          'entity_type_id' => $entity_type_id,
124 125 126 127 128 129
        ),
        array(
          '_access_content_translation_manage' => 'update',
        ),
        array(
          'parameters' => array(
130 131
            'language' => array(
              'type' => 'language',
132
            ),
133 134 135
            $entity_type_id => array(
              'type' => 'entity:' . $entity_type_id,
            ),
136
          ),
137
          '_admin_route' => $is_admin,
138 139
        )
      );
140
      $collection->add("entity.$entity_type_id.content_translation_edit", $route);
141 142 143 144

      $route = new Route(
        $path . '/delete/{language}',
        array(
145
          '_entity_form' => $entity_type_id . '.content_translation_deletion',
146 147
          'language' => NULL,
          '_title' => 'Delete',
148
          'entity_type_id' => $entity_type_id,
149 150 151 152 153 154
        ),
        array(
          '_access_content_translation_manage' => 'delete',
        ),
        array(
          'parameters' => array(
155 156
            'language' => array(
              'type' => 'language',
157
            ),
158 159 160
            $entity_type_id => array(
              'type' => 'entity:' . $entity_type_id,
            ),
161
          ),
162
          '_admin_route' => $is_admin,
163 164
        )
      );
165
      $collection->add("entity.$entity_type_id.content_translation_delete", $route);
166 167 168
    }
  }

169 170 171 172 173
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
174 175 176
    // Should run after AdminRouteSubscriber so the routes can inherit admin
    // status of the edit routes on entities. Therefore priority -210.
    $events[RoutingEvents::ALTER] = array('onAlterRoutes', -210);
177 178 179
    return $events;
  }

180
}