public function ConfigSource::saveTranslation

Saves a translation.

Parameters

\Drupal\tmgmt\JobItemInterface $job_item: The job item entity.

string $target_langcode: The target language code.

Return value

bool TRUE if the translation was saved successfully, FALSE otherwise.

Overrides SourcePluginInterface::saveTranslation

File

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

Class

ConfigSource
Content entity source plugin controller.

Namespace

Drupal\tmgmt_config\Plugin\tmgmt\Source

Code

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;
}