Chris@0: configMapperManager = $config_mapper_manager;
Chris@0: $this->accessManager = $access_manager;
Chris@0: $this->router = $router;
Chris@0: $this->pathProcessor = $path_processor;
Chris@0: $this->account = $account;
Chris@0: $this->languageManager = $language_manager;
Chris@0: $this->renderer = $renderer;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * {@inheritdoc}
Chris@0: */
Chris@0: public static function create(ContainerInterface $container) {
Chris@0: return new static(
Chris@0: $container->get('plugin.manager.config_translation.mapper'),
Chris@0: $container->get('access_manager'),
Chris@0: $container->get('router'),
Chris@0: $container->get('path_processor_manager'),
Chris@0: $container->get('current_user'),
Chris@0: $container->get('language_manager'),
Chris@0: $container->get('renderer')
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Language translations overview page for a configuration name.
Chris@0: *
Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0: * Page request object.
Chris@0: * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0: * The route match.
Chris@0: * @param string $plugin_id
Chris@0: * The plugin ID of the mapper.
Chris@0: *
Chris@0: * @return array
Chris@0: * Page render array.
Chris@0: */
Chris@0: public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {
Chris@0: /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
Chris@0: $mapper = $this->configMapperManager->createInstance($plugin_id);
Chris@0: $mapper->populateFromRouteMatch($route_match);
Chris@0:
Chris@0: $page = [];
Chris@0: $page['#title'] = $this->t('Translations for %label', ['%label' => $mapper->getTitle()]);
Chris@0:
Chris@0: $languages = $this->languageManager->getLanguages();
Chris@0: if (count($languages) == 1) {
Chris@17: $this->messenger()->addWarning($this->t('In order to translate configuration, the website must have at least two languages.', [':url' => $this->url('entity.configurable_language.collection')]));
Chris@0: }
Chris@0:
Chris@0: try {
Chris@0: $original_langcode = $mapper->getLangcode();
Chris@0: $operations_access = TRUE;
Chris@0: }
Chris@0: catch (ConfigMapperLanguageException $exception) {
Chris@0: $items = [];
Chris@0: foreach ($mapper->getConfigNames() as $config_name) {
Chris@0: $langcode = $mapper->getLangcodeFromConfig($config_name);
Chris@0: $items[] = $this->t('@name: @langcode', [
Chris@0: '@name' => $config_name,
Chris@0: '@langcode' => $langcode,
Chris@0: ]);
Chris@0: }
Chris@0: $message = [
Chris@0: 'message' => ['#markup' => $this->t('The configuration objects have different language codes so they cannot be translated:')],
Chris@0: 'items' => [
Chris@0: '#theme' => 'item_list',
Chris@0: '#items' => $items,
Chris@0: ],
Chris@0: ];
Chris@17: $this->messenger()->addWarning($this->renderer->renderPlain($message));
Chris@0:
Chris@0: $original_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
Chris@0: $operations_access = FALSE;
Chris@0: }
Chris@0:
Chris@0: if (!isset($languages[$original_langcode])) {
Chris@0: // If the language is not configured on the site, create a dummy language
Chris@0: // object for this listing only to ensure the user gets useful info.
Chris@0: $language_name = $this->languageManager->getLanguageName($original_langcode);
Chris@0: $languages[$original_langcode] = new Language(['id' => $original_langcode, 'name' => $language_name]);
Chris@0: }
Chris@0:
Chris@0: // We create a fake request object to pass into
Chris@0: // ConfigMapperInterface::populateFromRouteMatch() for the different languages.
Chris@0: // Creating a separate request for each language and route is neither easily
Chris@0: // possible nor performant.
Chris@0: $fake_request = $request->duplicate();
Chris@0:
Chris@0: $page['languages'] = [
Chris@0: '#type' => 'table',
Chris@0: '#header' => [$this->t('Language'), $this->t('Operations')],
Chris@0: ];
Chris@0: foreach ($languages as $language) {
Chris@0: $langcode = $language->getId();
Chris@0:
Chris@0: // This is needed because
Chris@0: // ConfigMapperInterface::getAddRouteParameters(), for example,
Chris@0: // needs to return the correct language code for each table row.
Chris@0: $fake_route_match = RouteMatch::createFromRequest($fake_request);
Chris@0: $mapper->populateFromRouteMatch($fake_route_match);
Chris@0: $mapper->setLangcode($langcode);
Chris@0:
Chris@0: // Prepare the language name and the operations depending on whether this
Chris@0: // is the original language or not.
Chris@0: if ($langcode == $original_langcode) {
Chris@0: $language_name = '' . $this->t('@language (original)', ['@language' => $language->getName()]) . '';
Chris@0:
Chris@0: // Check access for the path/route for editing, so we can decide to
Chris@0: // include a link to edit or not.
Chris@0: $edit_access = $this->accessManager->checkNamedRoute($mapper->getBaseRouteName(), $route_match->getRawParameters()->all(), $this->account);
Chris@0:
Chris@0: // Build list of operations.
Chris@0: $operations = [];
Chris@0: if ($edit_access) {
Chris@0: $operations['edit'] = [
Chris@0: 'title' => $this->t('Edit'),
Chris@0: 'url' => Url::fromRoute($mapper->getBaseRouteName(), $mapper->getBaseRouteParameters(), ['query' => ['destination' => $mapper->getOverviewPath()]]),
Chris@0: ];
Chris@0: }
Chris@0: }
Chris@0: else {
Chris@0: $language_name = $language->getName();
Chris@0:
Chris@0: $operations = [];
Chris@0: // If no translation exists for this language, link to add one.
Chris@0: if (!$mapper->hasTranslation($language)) {
Chris@0: $operations['add'] = [
Chris@0: 'title' => $this->t('Add'),
Chris@0: 'url' => Url::fromRoute($mapper->getAddRouteName(), $mapper->getAddRouteParameters()),
Chris@0: ];
Chris@0: }
Chris@0: else {
Chris@0: // Otherwise, link to edit the existing translation.
Chris@0: $operations['edit'] = [
Chris@0: 'title' => $this->t('Edit'),
Chris@0: 'url' => Url::fromRoute($mapper->getEditRouteName(), $mapper->getEditRouteParameters()),
Chris@0: ];
Chris@0:
Chris@0: $operations['delete'] = [
Chris@0: 'title' => $this->t('Delete'),
Chris@0: 'url' => Url::fromRoute($mapper->getDeleteRouteName(), $mapper->getDeleteRouteParameters()),
Chris@0: ];
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: $page['languages'][$langcode]['language'] = [
Chris@0: '#markup' => $language_name,
Chris@0: ];
Chris@0:
Chris@0: $page['languages'][$langcode]['operations'] = [
Chris@0: '#type' => 'operations',
Chris@0: '#links' => $operations,
Chris@0: // Even if the mapper contains multiple language codes, the source
Chris@0: // configuration can still be edited.
Chris@0: '#access' => ($langcode == $original_langcode) || $operations_access,
Chris@0: ];
Chris@0: }
Chris@0: return $page;
Chris@0: }
Chris@0:
Chris@0: }