You are here

class ConfigSource in Translation Management Tool 8

Content entity source plugin controller.

Plugin annotation


@SourcePlugin(
  id = "config",
  label = @Translation("Config Entity"),
  description = @Translation("Source handler for config entities."),
  ui = "Drupal\tmgmt_config\ConfigSourcePluginUi"
)

Hierarchy

Expanded class hierarchy of ConfigSource

2 files declare their use of ConfigSource
ConfigSourcePluginUi.php in sources/tmgmt_config/src/ConfigSourcePluginUi.php
ConfigTranslateForm.php in sources/tmgmt_config/src/Form/ConfigTranslateForm.php

File

sources/tmgmt_config/src/Plugin/tmgmt/Source/ConfigSource.php, line 30

Namespace

Drupal\tmgmt_config\Plugin\tmgmt\Source
View source
class ConfigSource extends SourcePluginBase implements ContainerFactoryPluginInterface {

  /**
   * Item type for simple configuration.
   *
   * @var string
   */
  const SIMPLE_CONFIG = '_simple_config';

  /**
   * The configuration mapper manager.
   *
   * @var \Drupal\config_translation\ConfigMapperManagerInterface
   */
  protected $configMapperManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Configuration factory manager
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactoryManager;

  /**
   * @var \Drupal\language\ConfigurableLanguageManagerInterface
   */
  protected $languageManager;

  /**
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected $typedConfig;

  /**
   * Constructs a ConfigTranslationController.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
   *   The configuration mapper manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param  \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManagerInterface
   *   The typed config.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\language\ConfigurableLanguageManagerInterface
   *   Configurable language manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigMapperManagerInterface $config_mapper_manager, EntityTypeManagerInterface $entity_type_manager, TypedConfigManagerInterface $typedConfigManagerInterface, ConfigFactoryInterface $config_factory, ConfigurableLanguageManagerInterface $language_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configMapperManager = $config_mapper_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->typedConfig = $typedConfigManagerInterface;
    $this->configFactoryManager = $config_factory;
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.config_translation.mapper'), $container
      ->get('entity_type.manager'), $container
      ->get('config.typed'), $container
      ->get('config.factory'), $container
      ->get('language_manager'));
  }

  /**
   * Gets the mapper ID.
   *
   * @param \Drupal\tmgmt\JobItemInterface $job_item
   *   The job item.
   *
   * @return string
   *   The mapper ID to be used for the config mapper manager.
   */
  protected function getMapperId(JobItemInterface $job_item) {

    // @todo: Inject dependencies.
    if ($job_item
      ->getItemType() == static::SIMPLE_CONFIG) {
      return $job_item
        ->getItemId();
    }
    else {
      $mapper_id = $job_item
        ->getItemType();
      if ($mapper_id == 'field_config') {

        // Field configs are exposed as a different type for each entity type
        // to the config mapper manager.
        // @todo Consider doing the same for item types, would result in more
        //   item types.
        $id_parts = explode('.', $job_item
          ->getItemId());
        $mapper_id = $id_parts[2] . '_fields';
      }
      return $mapper_id;
    }
  }

  /**
   * Gets the mapper.
   *
   * @param \Drupal\tmgmt\JobItemInterface $job_item
   *   Gets a job item as a parameter.
   *
   * @return \Drupal\config_translation\ConfigMapperInterface $config_mapper
   *   Returns the config mapper.
   *
   * @throws \Drupal\tmgmt\TMGMTException
   *   If there is no entity, throws an exception.
   */
  protected function getMapper(JobItemInterface $job_item) {
    $config_mapper = $this->configMapperManager
      ->createInstance($this
      ->getMapperId($job_item));
    if ($job_item
      ->getItemType() != static::SIMPLE_CONFIG) {

      /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
      $entity_type = $this->entityTypeManager
        ->getDefinition($config_mapper
        ->getType());
      $pos = strpos($job_item
        ->getItemId(), $entity_type
        ->getConfigPrefix());
      if ($pos !== FALSE) {
        $entity_id = str_replace($entity_type
          ->getConfigPrefix() . '.', '', $job_item
          ->getItemId());
      }
      else {
        throw new TMGMTException(t('Item ID does not contain the full config object name.'));
      }
      $entity = $this->entityTypeManager
        ->getStorage($config_mapper
        ->getType())
        ->load($entity_id);
      if (!$entity) {
        throw new TMGMTException(t('Unable to load entity %type with id %id', array(
          '%type' => $job_item
            ->getItemType(),
          '%id' => $entity_id,
        )));
      }
      $config_mapper
        ->setEntity($entity);
    }
    return $config_mapper;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel(JobItemInterface $job_item) {
    try {
      return $this
        ->getMapper($job_item)
        ->getTitle();
    } catch (TMGMTException $e) {

      // Don't throw an error here as it would clutter the UI.
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getUrl(JobItemInterface $job_item) {
    try {
      $config_mapper = $this
        ->getMapper($job_item);
      return Url::fromRoute($config_mapper
        ->getBaseRouteName(), $config_mapper
        ->getBaseRouteParameters());
    } catch (TMGMTException $e) {
      $this
        ->messenger()
        ->addError(t('Url can not be displayed, the entity does not exist: %error.', array(
        '%error' => $e
          ->getMessage(),
      )));
    }
    return NULL;
  }

  /**
   * Implements TMGMTEntitySourcePluginController::getData().
   *
   * Returns the data from the fields as a structure that can be processed by
   * the Translation Management system.
   */
  public function getData(JobItemInterface $job_item) {
    $config_mapper = $this
      ->getMapper($job_item);
    $data = array();
    foreach ($config_mapper
      ->getConfigData() as $config_id => $config_data) {
      $schema = $this->typedConfig
        ->get($config_id);
      $processor = $this
        ->getConfigProcessor($schema);
      $processor
        ->setConfigMapper($config_mapper);
      $config_id = str_replace('.', '__', $config_id);
      $data[$config_id] = $processor
        ->extractTranslatables($schema, $config_data);
    }

    // If there is only one, we simplify the data and return it.
    if (count($data) == 1) {
      return reset($data);
    }
    else {
      return $data;
    }
  }

  /**
   * Returns the config processor for a given configuration definition.
   *
   * @param \Drupal\Core\TypedData\TraversableTypedDataInterface $definition
   *   The field type.
   *
   * @return \Drupal\tmgmt_config\ConfigProcessorInterface
   *   The config processor for this configuration definition.
   */
  protected function getConfigProcessor(TraversableTypedDataInterface $definition) {
    $class = DefaultConfigProcessor::class;
    $data_definition = $definition
      ->getDataDefinition();
    if (method_exists($data_definition, 'toArray')) {
      $array_definition = $data_definition
        ->toArray();
      if (!empty($array_definition['tmgmt_config_processor'])) {
        $class = $array_definition['tmgmt_config_processor'];
      }
    }
    return \Drupal::service('class_resolver')
      ->getInstanceFromDefinition($class);
  }

  /**
   * {@inheritdoc}
   */
  public function saveTranslation(JobItemInterface $job_item, $target_langcode) {
    try {
      $config_mapper = $this
        ->getMapper($job_item);
    } catch (TMGMTException $e) {
      $job_item
        ->addMessage('The entity %id of type %type does not exist, the job can not be completed.', array(
        '%id' => $job_item
          ->getItemId(),
        '%type' => $job_item
          ->getItemType(),
      ), 'error');
      return FALSE;
    }
    $data = $job_item
      ->getData();
    $config_names = $config_mapper
      ->getConfigNames();

    // We need to refactor the array just as we did in getData.
    if (count($config_names) == 1) {
      $data[$config_names[0]] = $data;
    }
    else {

      // Replace the arrays keys back.
      foreach ($data as $key => $value) {
        $new_key = str_replace('__', '.', $key);
        $data[$new_key] = $value;
        unset($data[$key]);
      }
    }
    foreach ($config_mapper
      ->getConfigNames() as $name) {
      $schema = $this->typedConfig
        ->get($name);

      // Set configuration values based on form submission and source values.
      $base_config = $this->configFactoryManager
        ->getEditable($name);
      $config_translation = $this->languageManager
        ->getLanguageConfigOverride($target_langcode, $name);
      $element = ConfigTranslationFormBase::createFormElement($schema);
      $processor = $this
        ->getConfigProcessor($schema);
      $element
        ->setConfig($base_config, $config_translation, $processor
        ->convertToTranslation($data[$name]));

      // If no overrides, delete language specific configuration file.
      $saved_config = $config_translation
        ->get();
      if (empty($saved_config)) {
        $config_translation
          ->delete();
      }
      else {
        $config_translation
          ->save();
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getItemTypes() {

    // Only entity types are exposed as their own item type, all others are
    // grouped together in simple config.
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    $definitions = $this->configMapperManager
      ->getDefinitions();
    $types = array();
    foreach ($definitions as $definition_name => $definition) {
      if (isset($definition['entity_type'])) {
        $types[$definition['entity_type']] = (string) $entity_types[$definition['entity_type']]
          ->getLabel();
      }
    }
    $types[static::SIMPLE_CONFIG] = t('Simple configuration');
    return $types;
  }

  /**
   * {@inheritdoc}
   */
  public function getItemTypeLabel($type) {
    $item_types = $this
      ->getItemTypes();
    return $item_types[$type];
  }

  /**
   * {@inheritdoc}
   */
  public function getType(JobItemInterface $job_item) {
    $definition = $this->configMapperManager
      ->getDefinition($this
      ->getMapperId($job_item));
    return $definition['title'];
  }

  /**
   * {@inheritdoc}
   */
  public function getSourceLangCode(JobItemInterface $job_item) {
    $config_mapper = $this
      ->getMapper($job_item);
    return $config_mapper
      ->getLangcode();
  }

  /**
   * {@inheritdoc}
   */
  public function getExistingLangCodes(JobItemInterface $job_item) {

    // @todo Support additional source language codes.
    return [
      $this
        ->getSourceLangCode($job_item),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSource::$configFactoryManager protected property Configuration factory manager
ConfigSource::$configMapperManager protected property The configuration mapper manager.
ConfigSource::$entityTypeManager protected property The entity type manager.
ConfigSource::$languageManager protected property
ConfigSource::$typedConfig protected property
ConfigSource::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ConfigSource::getConfigProcessor protected function Returns the config processor for a given configuration definition.
ConfigSource::getData public function Implements TMGMTEntitySourcePluginController::getData(). Overrides SourcePluginInterface::getData
ConfigSource::getExistingLangCodes public function Gets existing translation language codes of the job item source. Overrides SourcePluginBase::getExistingLangCodes
ConfigSource::getItemTypeLabel public function Returns the label of a source item type. Overrides SourcePluginBase::getItemTypeLabel
ConfigSource::getItemTypes public function Returns an array of translatable source item types. Overrides SourcePluginBase::getItemTypes
ConfigSource::getLabel public function Return a title for this job item. Overrides SourcePluginBase::getLabel
ConfigSource::getMapper protected function Gets the mapper.
ConfigSource::getMapperId protected function Gets the mapper ID.
ConfigSource::getSourceLangCode public function Gets language code of the job item source. Overrides SourcePluginInterface::getSourceLangCode
ConfigSource::getType public function Returns the type of a job item. Overrides SourcePluginBase::getType
ConfigSource::getUrl public function Returns the Uri for this job item. Overrides SourcePluginBase::getUrl
ConfigSource::saveTranslation public function Saves a translation. Overrides SourcePluginInterface::saveTranslation
ConfigSource::SIMPLE_CONFIG constant Item type for simple configuration.
ConfigSource::__construct public function Constructs a ConfigTranslationController. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.