You are here

class ConfigMapperManager in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config_translation/src/ConfigMapperManager.php \Drupal\config_translation\ConfigMapperManager

Manages plugins for configuration translation mappers.

Hierarchy

Expanded class hierarchy of ConfigMapperManager

1 file declares its use of ConfigMapperManager
ConfigMapperManagerTest.php in core/modules/config_translation/tests/src/Unit/ConfigMapperManagerTest.php
1 string reference to 'ConfigMapperManager'
config_translation.services.yml in core/modules/config_translation/config_translation.services.yml
core/modules/config_translation/config_translation.services.yml
1 service uses ConfigMapperManager
plugin.manager.config_translation.mapper in core/modules/config_translation/config_translation.services.yml
Drupal\config_translation\ConfigMapperManager

File

core/modules/config_translation/src/ConfigMapperManager.php, line 23

Namespace

Drupal\config_translation
View source
class ConfigMapperManager extends DefaultPluginManager implements ConfigMapperManagerInterface {

  /**
   * The typed config manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfigManager;

  /**
   * The theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * {@inheritdoc}
   */
  protected $defaults = [
    'title' => '',
    'names' => [],
    'weight' => 20,
    'class' => '\\Drupal\\config_translation\\ConfigNamesMapper',
  ];

  /**
   * Constructs a ConfigMapperManager.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   The cache backend.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
   *   The typed config manager.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   */
  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');

    // Let others alter definitions with hook_config_translation_info_alter().
    $this->moduleHandler = $module_handler;
    $this->themeHandler = $theme_handler;
    $this
      ->alterInfo('config_translation_info');

    // Config translation only uses an info hook discovery, cache by language.
    $cache_key = 'config_translation_info_plugins' . ':' . $language_manager
      ->getCurrentLanguage()
      ->getId();
    $this
      ->setCacheBackend($cache_backend, $cache_key, [
      'config_translation_info_plugins',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDiscovery() {
    if (!isset($this->discovery)) {

      // Look at all themes and modules.
      // @todo If the list of installed modules and themes is changed, new
      //   definitions are not picked up immediately and obsolete definitions
      //   are not removed, because the list of search directories is only
      //   compiled once in this constructor. The current code only works due to
      //   coincidence: The request that installs (for instance, a new theme)
      //   does not instantiate this plugin manager at the beginning of the
      //   request; when routes are being rebuilt at the end of the request,
      //   this service only happens to get instantiated with the updated list
      //   of installed themes.
      $directories = [];
      foreach ($this->moduleHandler
        ->getModuleList() as $name => $module) {
        $directories[$name] = $module
          ->getPath();
      }
      foreach ($this->themeHandler
        ->listInfo() as $theme) {
        $directories[$theme
          ->getName()] = $theme
          ->getPath();
      }

      // Check for files named MODULE.config_translation.yml and
      // THEME.config_translation.yml in module/theme roots.
      $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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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.");
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildDataDefinition(array $definition, $value = NULL, $name = NULL, $parent = NULL) {
    return $this->typedConfigManager
      ->buildDataDefinition($definition, $value, $name, $parent);
  }

  /**
   * {@inheritdoc}
   */
  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);
    }

    // If this plugin was provided by a module that does not exist, remove the
    // plugin definition.
    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;
  }

  /**
   * {@inheritdoc}
   */
  public function hasTranslatable($name) {
    return $this
      ->findTranslatable($this->typedConfigManager
      ->get($name));
  }

  /**
   * Returns TRUE if at least one translatable element is found.
   *
   * @param \Drupal\Core\TypedData\TypedDataInterface $element
   *   Configuration schema element.
   *
   * @return bool
   *   A boolean indicating if there is at least one translatable element.
   */
  protected function findTranslatable(TypedDataInterface $element) {

    // In case this is a sequence or a mapping check whether any child element
    // is translatable.
    if ($element instanceof TraversableTypedDataInterface) {
      foreach ($element as $child_element) {
        if ($this
          ->findTranslatable($child_element)) {
          return TRUE;
        }
      }

      // If none of the child elements are translatable, return FALSE.
      return FALSE;
    }
    else {
      $definition = $element
        ->getDataDefinition();
      return isset($definition['translatable']) && $definition['translatable'];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigMapperManager::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. Overrides DefaultPluginManager::$defaults
ConfigMapperManager::$themeHandler protected property The theme handler.
ConfigMapperManager::$typedConfigManager protected property The typed config manager.
ConfigMapperManager::buildDataDefinition public function
ConfigMapperManager::findDefinitions protected function Finds plugin definitions. Overrides DefaultPluginManager::findDefinitions
ConfigMapperManager::findTranslatable protected function Returns TRUE if at least one translatable element is found.
ConfigMapperManager::getDiscovery protected function Gets the plugin discovery. Overrides DefaultPluginManager::getDiscovery
ConfigMapperManager::getMappers public function Returns an array of all mappers. Overrides ConfigMapperManagerInterface::getMappers
ConfigMapperManager::hasTranslatable public function Returns TRUE if the configuration data has translatable items. Overrides ConfigMapperManagerInterface::hasTranslatable
ConfigMapperManager::processDefinition public function Performs extra processing on plugin definitions. Overrides DefaultPluginManager::processDefinition
ConfigMapperManager::__construct public function Constructs a ConfigMapperManager. Overrides DefaultPluginManager::__construct
DefaultPluginManager::$additionalAnnotationNamespaces protected property Additional namespaces the annotation discovery mechanism should scan for annotation definitions.
DefaultPluginManager::$alterHook protected property Name of the alter hook if one should be invoked.
DefaultPluginManager::$cacheKey protected property The cache key.
DefaultPluginManager::$cacheTags protected property An array of cache tags to use for the cached definitions.
DefaultPluginManager::$moduleHandler protected property The module handler to invoke the alter hook. 1
DefaultPluginManager::$namespaces protected property An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations.
DefaultPluginManager::$pluginDefinitionAnnotationName protected property The name of the annotation that contains the plugin definition.
DefaultPluginManager::$pluginInterface protected property The interface each plugin should implement. 1
DefaultPluginManager::$subdir protected property The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace.
DefaultPluginManager::alterDefinitions protected function Invokes the hook to alter the definitions if the alter hook is set. 1
DefaultPluginManager::alterInfo protected function Sets the alter hook name.
DefaultPluginManager::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions 5
DefaultPluginManager::extractProviderFromDefinition protected function Extracts the provider from a plugin definition.
DefaultPluginManager::fixContextAwareDefinitions private function Fix the definitions of context-aware plugins.
DefaultPluginManager::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
DefaultPluginManager::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
DefaultPluginManager::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
DefaultPluginManager::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
DefaultPluginManager::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions 2
DefaultPluginManager::getFactory protected function Gets the plugin factory. Overrides PluginManagerBase::getFactory
DefaultPluginManager::providerExists protected function Determines if the provider of a definition exists. 3
DefaultPluginManager::setCacheBackend public function Initialize the cache backend.
DefaultPluginManager::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
DefaultPluginManager::useCaches public function Disable the use of caches. Overrides CachedDiscoveryInterface::useCaches 1
DiscoveryCachedTrait::$definitions protected property Cached definitions array. 1
DiscoveryCachedTrait::getDefinition public function Overrides DiscoveryTrait::getDefinition 3
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::hasDefinition public function
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::createInstance public function Creates a pre-configured instance of a plugin. Overrides FactoryInterface::createInstance 12
PluginManagerBase::getInstance public function Gets a preconfigured instance of a plugin. Overrides MapperInterface::getInstance 7
PluginManagerBase::handlePluginNotFound protected function Allows plugin managers to specify custom behavior if a plugin is not found. 1
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag. 1
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.