ContentTranslationRouteSubscriber.php 4.53 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, $provider) {
42
    foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type => $entity_info) {
43
      // Try to get the route from the current collection.
44
      if (!$entity_route = $collection->get($entity_info->getLinkTemplate('canonical'))) {
45
        continue;
46
47
      }
      $path = $entity_route->getPath() . '/translations';
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
      $route = new Route(
       $path,
        array(
          '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::overview',
          'account' => 'NULL',
          '_entity_type' => $entity_type,
        ),
        array(
          '_access_content_translation_overview' => $entity_type,
          '_permission' => 'translate any entity',
        ),
        array(
          '_access_mode' => 'ANY',
          'parameters' => array(
            'entity' => array(
              'type' => 'entity:' . $entity_type,
65
            ),
66
67
68
          ),
        )
      );
69
      $collection->add($entity_info->getLinkTemplate('drupal:content-translation-overview'), $route);
70

71
72
73
74
75
76
77
78
      $route = new Route(
        $path . '/add/{source}/{target}',
        array(
          '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::add',
          'source' => NULL,
          'target' => NULL,
          '_title' => 'Add',
          '_entity_type' => $entity_type,
79

80
81
82
83
84
85
86
87
88
89
        ),
        array(
          '_permission' => 'translate any entity',
          '_access_content_translation_manage' => 'create',
        ),
        array(
          '_access_mode' => 'ANY',
          'parameters' => array(
            'entity' => array(
              'type' => 'entity:' . $entity_type,
90
91
            ),
          ),
92
93
94
        )
      );
      $collection->add("content_translation.translation_add_$entity_type", $route);
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
      $route = new Route(
        $path . '/edit/{language}',
        array(
          '_content' => '\Drupal\content_translation\Controller\ContentTranslationController::edit',
          'language' => NULL,
          '_title' => 'Edit',
          '_entity_type' => $entity_type,
        ),
        array(
          '_permission' => 'translate any entity',
          '_access_content_translation_manage' => 'update',
        ),
        array(
          '_access_mode' => 'ANY',
          'parameters' => array(
            'entity' => array(
              'type' => 'entity:' . $entity_type,
            ),
114
          ),
115
116
117
118
119
120
121
        )
      );
      $collection->add("content_translation.translation_edit_$entity_type", $route);

      $route = new Route(
        $path . '/delete/{language}',
        array(
122
          '_form' => '\Drupal\content_translation\Form\ContentTranslationDeleteForm',
123
124
125
126
127
128
129
130
131
132
133
134
          'language' => NULL,
          '_title' => 'Delete',
          '_entity_type' => $entity_type,
        ),
        array(
          '_permission' => 'translate any entity',
          '_access_content_translation_manage' => 'delete',
        ),
        array(
          'parameters' => array(
            'entity' => array(
              'type' => 'entity:' . $entity_type,
135
            ),
136
137
138
139
140
          ),
          '_access_mode' => 'ANY',
        )
      );
      $collection->add("content_translation.delete_$entity_type", $route);
141
142
143
    }
  }

144
145
146
147
148
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
149
    $events[RoutingEvents::ALTER] = array('onAlterRoutes', -100);
150
151
152
    return $events;
  }

153
}