View source
<?php
namespace Drupal\config_translation\Controller;
use Drupal\config_translation\ConfigMapperManagerInterface;
use Drupal\config_translation\Exception\ConfigMapperLanguageException;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
class ConfigTranslationController extends ControllerBase {
protected $configMapperManager;
protected $accessManager;
protected $router;
protected $pathProcessor;
protected $account;
protected $languageManager;
protected $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;
}
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'));
}
public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {
$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
->renderPlain($message));
$original_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
$operations_access = FALSE;
}
if (!isset($languages[$original_langcode])) {
$language_name = $this->languageManager
->getLanguageName($original_langcode);
$languages[$original_langcode] = new Language([
'id' => $original_langcode,
'name' => $language_name,
]);
}
$fake_request = $request
->duplicate();
$page['languages'] = [
'#type' => 'table',
'#header' => [
$this
->t('Language'),
$this
->t('Operations'),
],
];
foreach ($languages as $language) {
$langcode = $language
->getId();
$fake_route_match = RouteMatch::createFromRequest($fake_request);
$mapper
->populateFromRouteMatch($fake_route_match);
$mapper
->setLangcode($langcode);
if ($langcode == $original_langcode) {
$language_name = '<strong>' . $this
->t('@language (original)', [
'@language' => $language
->getName(),
]) . '</strong>';
$edit_access = $this->accessManager
->checkNamedRoute($mapper
->getBaseRouteName(), $route_match
->getRawParameters()
->all(), $this->account);
$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 (!$mapper
->hasTranslation($language)) {
$operations['add'] = [
'title' => $this
->t('Add'),
'url' => Url::fromRoute($mapper
->getAddRouteName(), $mapper
->getAddRouteParameters()),
];
}
else {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute($mapper
->getEditRouteName(), $mapper
->getEditRouteParameters()),
];
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute($mapper
->getDeleteRouteName(), $mapper
->getDeleteRouteParameters()),
];
}
}
$page['languages'][$langcode]['language'] = [
'#markup' => $language_name,
];
$page['languages'][$langcode]['operations'] = [
'#type' => 'operations',
'#links' => $operations,
'#access' => $langcode == $original_langcode || $operations_access,
];
}
return $page;
}
}