View source
<?php
namespace Drupal\language_hierarchy\Controller;
use Drupal\config_translation\ConfigMapperManagerInterface;
use Drupal\config_translation\Controller\ConfigTranslationController as CoreConfigTranslationController;
use Drupal\config_translation\Exception\ConfigMapperLanguageException;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Render\Element;
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 Drupal\language\Config\LanguageConfigFactoryOverrideInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
class ConfigTranslationController extends CoreConfigTranslationController {
protected $configFactoryOverride;
public function __construct(ConfigMapperManagerInterface $config_mapper_manager, AccessManagerInterface $access_manager, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, AccountInterface $account, LanguageManagerInterface $language_manager, RendererInterface $renderer, LanguageConfigFactoryOverrideInterface $config_override) {
parent::__construct($config_mapper_manager, $access_manager, $router, $path_processor, $account, $language_manager, $renderer);
$this->configFactoryOverride = $config_override;
}
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'), $container
->get('language.config_factory_override'));
}
public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {
$mapper = $this->configMapperManager
->createInstance($plugin_id);
$mapper
->populateFromRouteMatch($route_match);
$page = parent::itemPage($request, $route_match, $plugin_id);
try {
$original_langcode = $mapper
->getLangcode();
} catch (ConfigMapperLanguageException $exception) {
$original_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
$fake_request = $request
->duplicate();
foreach (array_diff(Element::children($page['languages']), [
$original_langcode,
]) as $langcode) {
if (!empty($page['languages'][$langcode]['operations']['#links']['edit'])) {
$fake_route_match = RouteMatch::createFromRequest($fake_request);
$mapper
->populateFromRouteMatch($fake_route_match);
$mapper
->setLangcode($langcode);
$exists_directly = FALSE;
foreach ($mapper
->getConfigNames() as $config_name) {
$override = $this->configFactoryOverride
->getOverride($langcode, $config_name);
if ($override
->getLangcode() === $langcode) {
$exists_directly = TRUE;
}
}
if (!$exists_directly) {
unset($page['languages'][$langcode]['operations']['#links']['edit'], $page['languages'][$langcode]['operations']['#links']['delete']);
$page['languages'][$langcode]['operations']['#links']['add'] = [
'title' => $this
->t('Add'),
'url' => Url::fromRoute($mapper
->getAddRouteName(), $mapper
->getAddRouteParameters()),
];
}
}
}
return $page;
}
}