View source
<?php
namespace Drupal\config_translation;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\locale\LocaleConfigManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, ContainerFactoryPluginInterface {
protected $configFactory;
protected $typedConfigManager;
protected $localeConfigManager;
protected $configMapperManager;
protected $routeProvider;
protected $baseRoute;
protected $routeCollection;
protected $langcode = NULL;
protected $languageManager;
public function __construct($plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config, LocaleConfigManager $locale_config_manager, ConfigMapperManagerInterface $config_mapper_manager, RouteProviderInterface $route_provider, TranslationInterface $string_translation, LanguageManagerInterface $language_manager) {
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->routeProvider = $route_provider;
$this->configFactory = $config_factory;
$this->typedConfigManager = $typed_config;
$this->localeConfigManager = $locale_config_manager;
$this->configMapperManager = $config_mapper_manager;
$this->stringTranslation = $string_translation;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('config.typed'), $container
->get('locale.config_manager'), $container
->get('plugin.manager.config_translation.mapper'), $container
->get('router.route_provider'), $container
->get('string_translation'), $container
->get('language_manager'));
}
public function setRouteCollection(RouteCollection $collection) {
$this->routeCollection = $collection;
}
public function getTitle() {
return $this
->t($this->pluginDefinition['title']);
}
public function getBaseRouteName() {
return $this->pluginDefinition['base_route_name'];
}
public function getBaseRouteParameters() {
return array();
}
public function getBaseRoute() {
if ($this->routeCollection) {
return $this->routeCollection
->get($this
->getBaseRouteName());
}
else {
return $this->routeProvider
->getRouteByName($this
->getBaseRouteName());
}
}
protected function processRoute(Route $route) {
}
public function getBasePath() {
return Url::fromRoute($this
->getBaseRouteName(), $this
->getBaseRouteParameters())
->getInternalPath();
}
public function getOverviewRouteName() {
return 'config_translation.item.overview.' . $this
->getBaseRouteName();
}
public function getOverviewRouteParameters() {
return $this
->getBaseRouteParameters();
}
public function getOverviewRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate', array(
'_controller' => '\\Drupal\\config_translation\\Controller\\ConfigTranslationController::itemPage',
'plugin_id' => $this
->getPluginId(),
), array(
'_config_translation_overview_access' => 'TRUE',
));
$this
->processRoute($route);
return $route;
}
public function getOverviewPath() {
return Url::fromRoute($this
->getOverviewRouteName(), $this
->getOverviewRouteParameters())
->getInternalPath();
}
public function getAddRouteName() {
return 'config_translation.item.add.' . $this
->getBaseRouteName();
}
public function getAddRouteParameters() {
$parameters = $this
->getBaseRouteParameters();
$parameters['langcode'] = $this->langcode;
return $parameters;
}
public function getAddRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate/{langcode}/add', array(
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationAddForm',
'plugin_id' => $this
->getPluginId(),
), array(
'_config_translation_form_access' => 'TRUE',
));
$this
->processRoute($route);
return $route;
}
public function getEditRouteName() {
return 'config_translation.item.edit.' . $this
->getBaseRouteName();
}
public function getEditRouteParameters() {
return $this
->getAddRouteParameters();
}
public function getEditRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate/{langcode}/edit', array(
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationEditForm',
'plugin_id' => $this
->getPluginId(),
), array(
'_config_translation_form_access' => 'TRUE',
));
$this
->processRoute($route);
return $route;
}
public function getDeleteRouteName() {
return 'config_translation.item.delete.' . $this
->getBaseRouteName();
}
public function getDeleteRouteParameters() {
return $this
->getAddRouteParameters();
}
public function getDeleteRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate/{langcode}/delete', array(
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationDeleteForm',
'plugin_id' => $this
->getPluginId(),
), array(
'_config_translation_form_access' => 'TRUE',
));
$this
->processRoute($route);
return $route;
}
public function getConfigNames() {
return $this->pluginDefinition['names'];
}
public function addConfigName($name) {
$this->pluginDefinition['names'][] = $name;
}
public function getWeight() {
return $this->pluginDefinition['weight'];
}
public function populateFromRouteMatch(RouteMatchInterface $route_match) {
$this->langcode = $route_match
->getParameter('langcode');
}
public function getTypeLabel() {
return $this
->getTitle();
}
public function getLangcode() {
$config_factory = $this->configFactory;
$langcodes = array_map(function ($name) use ($config_factory) {
return $config_factory
->get($name)
->get('langcode') ?: 'en';
}, $this
->getConfigNames());
if (count(array_unique($langcodes)) > 1) {
throw new \RuntimeException('A config mapper can only contain configuration for a single language.');
}
return reset($langcodes);
}
public function setLangcode($langcode) {
$this->langcode = $langcode;
return $this;
}
public function getConfigData() {
$config_data = array();
foreach ($this
->getConfigNames() as $name) {
$config_data[$name] = $this->configFactory
->getEditable($name)
->get();
}
return $config_data;
}
public function hasSchema() {
foreach ($this
->getConfigNames() as $name) {
if (!$this->typedConfigManager
->hasConfigSchema($name)) {
return FALSE;
}
}
return TRUE;
}
public function hasTranslatable() {
foreach ($this
->getConfigNames() as $name) {
if ($this->configMapperManager
->hasTranslatable($name)) {
return TRUE;
}
}
return FALSE;
}
public function hasTranslation(LanguageInterface $language) {
foreach ($this
->getConfigNames() as $name) {
if ($this->localeConfigManager
->hasTranslation($name, $language
->getId())) {
return TRUE;
}
}
return FALSE;
}
public function getTypeName() {
return $this
->t('Settings');
}
public function getOperations() {
return array(
'translate' => array(
'title' => $this
->t('Translate'),
'url' => Url::fromRoute($this
->getOverviewRouteName(), $this
->getOverviewRouteParameters()),
),
);
}
public function getContextualLinkGroup() {
return NULL;
}
}