ConfigTranslationController.php 7.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @file
 * Contains \Drupal\config_translation\Controller\ConfigTranslationController.
 */

namespace Drupal\config_translation\Controller;

use Drupal\config_translation\ConfigMapperManagerInterface;
11
use Drupal\Core\Access\AccessManagerInterface;
12
13
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\Language;
14
use Drupal\Core\Language\LanguageManagerInterface;
15
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
16
use Drupal\Core\Routing\RouteMatchInterface;
17
use Drupal\Core\Session\AccountInterface;
18
use Drupal\Core\Url;
19
20
21
22
23
24
25
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;

/**
 * Provides page callbacks for the configuration translation interface.
 */
26
class ConfigTranslationController extends ControllerBase {
27
28

  /**
29
   * The configuration mapper manager.
30
31
32
33
34
35
36
37
   *
   * @var \Drupal\config_translation\ConfigMapperManagerInterface
   */
  protected $configMapperManager;

  /**
   * The menu link access service.
   *
38
   * @var \Drupal\Core\Access\AccessManagerInterface
39
40
41
   */
  protected $accessManager;

42
  /**
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
   * The dynamic router service.
   *
   * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface
   */
  protected $router;

  /**
   * The path processor service.
   *
   * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
   */
  protected $pathProcessor;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

63
64
65
66
67
68
69
  /**
   * The language manager.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   */
  protected $languageManager;

70
71
72
73
74
  /**
   * Constructs a ConfigTranslationController.
   *
   * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
   *   The configuration mapper manager.
75
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
76
77
78
79
80
81
82
   *   The menu link access service.
   * @param \Symfony\Component\Routing\Matcher\RequestMatcherInterface $router
   *   The dynamic router service.
   * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $path_processor
   *   The inbound path processor.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
83
84
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
85
   */
86
  public function __construct(ConfigMapperManagerInterface $config_mapper_manager, AccessManagerInterface $access_manager, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, AccountInterface $account, LanguageManagerInterface $language_manager) {
87
88
89
90
91
    $this->configMapperManager = $config_mapper_manager;
    $this->accessManager = $access_manager;
    $this->router = $router;
    $this->pathProcessor = $path_processor;
    $this->account = $account;
92
    $this->languageManager = $language_manager;
93
94
95
96
97
98
99
100
101
102
103
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('plugin.manager.config_translation.mapper'),
      $container->get('access_manager'),
      $container->get('router'),
      $container->get('path_processor_manager'),
104
105
      $container->get('current_user'),
      $container->get('language_manager')
106
107
108
109
110
111
112
113
    );
  }

  /**
   * Language translations overview page for a configuration name.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   Page request object.
114
115
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
116
117
118
119
120
121
   * @param string $plugin_id
   *   The plugin ID of the mapper.
   *
   * @return array
   *   Page render array.
   */
122
  public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {
123
124
125
126
127
128
129
130
131
132
133
    /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
    $mapper = $this->configMapperManager->createInstance($plugin_id);
    $mapper->populateFromRequest($request);

    $page = array();
    $page['#title'] = $this->t('Translations for %label', array('%label' => $mapper->getTitle()));

    // It is possible the original language this configuration was saved with is
    // not on the system. For example, the configuration shipped in English but
    // the site has no English configured. Represent the original language in
    // the table even if it is not currently configured.
134
    $languages = $this->languageManager->getLanguages();
135
136
    $original_langcode = $mapper->getLangcode();
    if (!isset($languages[$original_langcode])) {
137
      $language_name = $this->languageManager->getLanguageName($original_langcode);
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
      if ($original_langcode == 'en') {
        $language_name = $this->t('Built-in English');
      }
      // Create a dummy language object for this listing only.
      $languages[$original_langcode] = new Language(array('id' => $original_langcode, 'name' => $language_name));
    }

    // We create a fake request object to pass into
    // ConfigMapperInterface::populateFromRequest() for the different languages.
    // Creating a separate request for each language and route is neither easily
    // possible nor performant.
    $fake_request = $request->duplicate();

    $page['languages'] = array(
      '#type' => 'table',
      '#header' => array($this->t('Language'), $this->t('Operations')),
    );
    foreach ($languages as $language) {
156
      $langcode = $language->getId();
157

158
159
      // This is needed because
      // ConfigMapperInterface::getAddRouteParameters(), for example,
160
161
162
163
164
165
166
      // needs to return the correct language code for each table row.
      $fake_request->attributes->set('langcode', $langcode);
      $mapper->populateFromRequest($fake_request);

      // Prepare the language name and the operations depending on whether this
      // is the original language or not.
      if ($langcode == $original_langcode) {
167
        $language_name = '<strong>' . $this->t('@language (original)', array('@language' => $language->getName())) . '</strong>';
168
169
170

        // Check access for the path/route for editing, so we can decide to
        // include a link to edit or not.
171
        $edit_access = $this->accessManager->checkNamedRoute($mapper->getBaseRouteName(), $route_match->getRawParameters()->all(), $this->account);
172
173
174
175
176
177

        // Build list of operations.
        $operations = array();
        if ($edit_access) {
          $operations['edit'] = array(
            'title' => $this->t('Edit'),
178
            'url' => Url::fromRoute($mapper->getBaseRouteName(), $mapper->getBaseRouteParameters(), ['query' => ['destination' => $mapper->getOverviewPath()]]),
179
180
181
182
          );
        }
      }
      else {
183
        $language_name = $language->getName();
184
185
186
187
188
189

        $operations = array();
        // If no translation exists for this language, link to add one.
        if (!$mapper->hasTranslation($language)) {
          $operations['add'] = array(
            'title' => $this->t('Add'),
190
            'url' => Url::fromRoute($mapper->getAddRouteName(), $mapper->getAddRouteParameters()),
191
192
193
194
195
196
          );
        }
        else {
          // Otherwise, link to edit the existing translation.
          $operations['edit'] = array(
            'title' => $this->t('Edit'),
197
            'url' => Url::fromRoute($mapper->getEditRouteName(), $mapper->getEditRouteParameters()),
198
199
200
201
          );

          $operations['delete'] = array(
            'title' => $this->t('Delete'),
202
            'url' => Url::fromRoute($mapper->getDeleteRouteName(), $mapper->getDeleteRouteParameters()),
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
          );
        }
      }

      $page['languages'][$langcode]['language'] = array(
        '#markup' => $language_name,
      );

      $page['languages'][$langcode]['operations'] = array(
        '#type' => 'operations',
        '#links' => $operations,
      );
    }
    return $page;
  }

}