class ConfigTranslationController

Same name and namespace in other branches
  1. 9 core/modules/config_translation/src/Controller/ConfigTranslationController.php \Drupal\config_translation\Controller\ConfigTranslationController
  2. 8.9.x core/modules/config_translation/src/Controller/ConfigTranslationController.php \Drupal\config_translation\Controller\ConfigTranslationController
  3. 10 core/modules/config_translation/src/Controller/ConfigTranslationController.php \Drupal\config_translation\Controller\ConfigTranslationController

Provides page callbacks for the configuration translation interface.

Hierarchy

Expanded class hierarchy of ConfigTranslationController

File

core/modules/config_translation/src/Controller/ConfigTranslationController.php, line 26

Namespace

Drupal\config_translation\Controller
View source
class ConfigTranslationController extends ControllerBase {
    
    /**
     * The configuration mapper manager.
     *
     * @var \Drupal\config_translation\ConfigMapperManagerInterface
     */
    protected $configMapperManager;
    
    /**
     * The menu link access service.
     *
     * @var \Drupal\Core\Access\AccessManagerInterface
     */
    protected $accessManager;
    
    /**
     * 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;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * The renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected $renderer;
    
    /**
     * Constructs a ConfigTranslationController.
     *
     * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
     *   The configuration mapper manager.
     * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
     *   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.
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager.
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer.
     */
    public function __construct(ConfigMapperManagerInterface $config_mapper_manager, AccessManagerInterface $access_manager, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, AccountInterface $account, LanguageManagerInterface $language_manager, RendererInterface $renderer) {
        $this->configMapperManager = $config_mapper_manager;
        $this->accessManager = $access_manager;
        $this->router = $router;
        $this->pathProcessor = $path_processor;
        $this->account = $account;
        $this->languageManager = $language_manager;
        $this->renderer = $renderer;
    }
    
    /**
     * {@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'), $container->get('current_user'), $container->get('language_manager'), $container->get('renderer'));
    }
    
    /**
     * Language translations overview page for a configuration name.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   Page request object.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The route match.
     * @param string $plugin_id
     *   The plugin ID of the mapper.
     *
     * @return array
     *   Page render array.
     */
    public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {
        
        /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
        $mapper = $this->configMapperManager
            ->createInstance($plugin_id);
        $mapper->populateFromRouteMatch($route_match);
        $page = [];
        $page['#title'] = $this->t('Translations for %label', [
            '%label' => $mapper->getTitle(),
        ]);
        $languages = $this->languageManager
            ->getLanguages();
        if (count($languages) == 1) {
            $this->messenger()
                ->addWarning($this->t('In order to translate configuration, the website must have at least two <a href=":url">languages</a>.', [
                ':url' => Url::fromRoute('entity.configurable_language.collection')->toString(),
            ]));
        }
        try {
            $original_langcode = $mapper->getLangcode();
            $operations_access = TRUE;
        } catch (ConfigMapperLanguageException $exception) {
            $items = [];
            foreach ($mapper->getConfigNames() as $config_name) {
                $langcode = $mapper->getLangcodeFromConfig($config_name);
                $items[] = $this->t('@name: @langcode', [
                    '@name' => $config_name,
                    '@langcode' => $langcode,
                ]);
            }
            $message = [
                'message' => [
                    '#markup' => $this->t('The configuration objects have different language codes so they cannot be translated:'),
                ],
                'items' => [
                    '#theme' => 'item_list',
                    '#items' => $items,
                ],
            ];
            $this->messenger()
                ->addWarning($this->renderer
                ->renderInIsolation($message));
            $original_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
            $operations_access = FALSE;
        }
        if (!isset($languages[$original_langcode])) {
            // If the language is not configured on the site, create a dummy language
            // object for this listing only to ensure the user gets useful info.
            $language_name = $this->languageManager
                ->getLanguageName($original_langcode);
            $languages[$original_langcode] = new Language([
                'id' => $original_langcode,
                'name' => $language_name,
            ]);
        }
        // We create a fake request object to pass into
        // ConfigMapperInterface::populateFromRouteMatch() 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'] = [
            '#type' => 'table',
            '#header' => [
                $this->t('Language'),
                $this->t('Operations'),
            ],
        ];
        foreach ($languages as $language) {
            $langcode = $language->getId();
            // This is needed because
            // ConfigMapperInterface::getAddRouteParameters(), for example,
            // needs to return the correct language code for each table row.
            $fake_route_match = RouteMatch::createFromRequest($fake_request);
            $mapper->populateFromRouteMatch($fake_route_match);
            $mapper->setLangcode($langcode);
            // Prepare the language name and the operations depending on whether this
            // is the original language or not.
            if ($langcode == $original_langcode) {
                $language_name = '<strong>' . $this->t('@language (original)', [
                    '@language' => $language->getName(),
                ]) . '</strong>';
                // Check access for the path/route for editing, so we can decide to
                // include a link to edit or not.
                $edit_access = $this->accessManager
                    ->checkNamedRoute($mapper->getBaseRouteName(), $route_match->getRawParameters()
                    ->all(), $this->account);
                // Build list of operations.
                $operations = [];
                if ($edit_access) {
                    $operations['edit'] = [
                        'title' => $this->t('Edit'),
                        'url' => Url::fromRoute($mapper->getBaseRouteName(), $mapper->getBaseRouteParameters(), [
                            'query' => [
                                'destination' => $mapper->getOverviewPath(),
                            ],
                        ]),
                    ];
                }
            }
            else {
                $language_name = $language->getName();
                $operations = [];
                // If no translation exists for this language, link to add one.
                if (!$mapper->hasTranslation($language)) {
                    $operations['add'] = [
                        'title' => $this->t('Add'),
                        'url' => Url::fromRoute($mapper->getAddRouteName(), $mapper->getAddRouteParameters()),
                        'attributes' => [
                            'class' => [
                                'use-ajax',
                            ],
                            'data-dialog-type' => 'modal',
                            'data-dialog-options' => Json::encode([
                                'width' => 880,
                            ]),
                        ],
                    ];
                }
                else {
                    // Otherwise, link to edit the existing translation.
                    $operations['edit'] = [
                        'title' => $this->t('Edit'),
                        'url' => Url::fromRoute($mapper->getEditRouteName(), $mapper->getEditRouteParameters()),
                        'attributes' => [
                            'class' => [
                                'use-ajax',
                            ],
                            'data-dialog-type' => 'modal',
                            'data-dialog-options' => Json::encode([
                                'width' => 880,
                            ]),
                        ],
                    ];
                    $operations['delete'] = [
                        'title' => $this->t('Delete'),
                        'url' => Url::fromRoute($mapper->getDeleteRouteName(), $mapper->getDeleteRouteParameters()),
                        'attributes' => [
                            'class' => [
                                'use-ajax',
                            ],
                            'data-dialog-type' => 'modal',
                            'data-dialog-options' => Json::encode([
                                'width' => 880,
                            ]),
                        ],
                    ];
                }
            }
            $page['languages'][$langcode]['language'] = [
                '#markup' => $language_name,
            ];
            $page['languages'][$langcode]['operations'] = [
                '#type' => 'operations',
                '#links' => $operations,
                // Even if the mapper contains multiple language codes, the source
                // configuration can still be edited.
'#access' => $langcode == $original_langcode || $operations_access,
                '#attached' => [
                    'library' => [
                        'core/drupal.dialog.ajax',
                    ],
                ],
            ];
        }
        return $page;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigTranslationController::$accessManager protected property The menu link access service.
ConfigTranslationController::$account protected property The current user.
ConfigTranslationController::$configMapperManager protected property The configuration mapper manager.
ConfigTranslationController::$languageManager protected property The language manager. Overrides ControllerBase::$languageManager
ConfigTranslationController::$pathProcessor protected property The path processor service.
ConfigTranslationController::$renderer protected property The renderer.
ConfigTranslationController::$router protected property The dynamic router service.
ConfigTranslationController::create public static function Instantiates a new instance of the implementing class using autowiring. Overrides AutowireTrait::create
ConfigTranslationController::itemPage public function Language translations overview page for a configuration name.
ConfigTranslationController::__construct public function Constructs a ConfigTranslationController.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 2
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 1
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$moduleHandler protected property The module handler. 1
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 2
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 1
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 1
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a &#039;destination&#039; URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.