View source
<?php
namespace Drupal\config_translation;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Factory\ContainerFactory;
use Drupal\Core\TypedData\TraversableTypedDataInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Symfony\Component\Routing\RouteCollection;
class ConfigMapperManager extends DefaultPluginManager implements ConfigMapperManagerInterface {
protected $typedConfigManager;
protected $themeHandler;
protected $defaults = [
'title' => '',
'names' => [],
'weight' => 20,
'class' => '\\Drupal\\config_translation\\ConfigNamesMapper',
];
public function __construct(CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, TypedConfigManagerInterface $typed_config_manager, ThemeHandlerInterface $theme_handler) {
$this->typedConfigManager = $typed_config_manager;
$this->factory = new ContainerFactory($this, '\\Drupal\\config_translation\\ConfigMapperInterface');
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this
->alterInfo('config_translation_info');
$cache_key = 'config_translation_info_plugins' . ':' . $language_manager
->getCurrentLanguage()
->getId();
$this
->setCacheBackend($cache_backend, $cache_key, [
'config_translation_info_plugins',
]);
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$directories = [];
foreach ($this->moduleHandler
->getModuleList() as $name => $module) {
$directories[$name] = $module
->getPath();
}
foreach ($this->themeHandler
->listInfo() as $theme) {
$directories[$theme
->getName()] = $theme
->getPath();
}
$this->discovery = new YamlDiscovery('config_translation', $directories);
$this->discovery = new InfoHookDecorator($this->discovery, 'config_translation_info');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
public function getMappers(RouteCollection $collection = NULL) {
$mappers = [];
foreach ($this
->getDefinitions() as $id => $definition) {
$mappers[$id] = $this
->createInstance($id);
if ($collection) {
$mappers[$id]
->setRouteCollection($collection);
}
}
return $mappers;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (!isset($definition['base_route_name'])) {
throw new InvalidPluginDefinitionException($plugin_id, "The plugin definition of the mapper '{$plugin_id}' does not contain a base_route_name.");
}
}
public function buildDataDefinition(array $definition, $value = NULL, $name = NULL, $parent = NULL) {
return $this->typedConfigManager
->buildDataDefinition($definition, $value, $name, $parent);
}
protected function findDefinitions() {
$definitions = $this
->getDiscovery()
->getDefinitions();
foreach ($definitions as $plugin_id => &$definition) {
$this
->processDefinition($definition, $plugin_id);
}
if ($this->alterHook) {
$this->moduleHandler
->alter($this->alterHook, $definitions);
}
foreach ($definitions as $plugin_id => $plugin_definition) {
if (isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], [
'core',
'component',
]) && (!$this->moduleHandler
->moduleExists($plugin_definition['provider']) && !in_array($plugin_definition['provider'], array_keys($this->themeHandler
->listInfo())))) {
unset($definitions[$plugin_id]);
}
}
return $definitions;
}
public function hasTranslatable($name) {
return $this
->findTranslatable($this->typedConfigManager
->get($name));
}
protected function findTranslatable(TypedDataInterface $element) {
if ($element instanceof TraversableTypedDataInterface) {
foreach ($element as $child_element) {
if ($this
->findTranslatable($child_element)) {
return TRUE;
}
}
return FALSE;
}
else {
$definition = $element
->getDataDefinition();
return isset($definition['translatable']) && $definition['translatable'];
}
}
}