/**
  * Deletes a language override.
  *
  * This will invoke LocaleConfigSubscriber through the event dispatcher. To
  * make sure the configuration was persisted correctly, the configuration
  * value is checked. Because LocaleConfigSubscriber temporarily disables the
  * override state of the configuration factory we check that the correct value
  * is restored afterwards.
  *
  * @param string $config_name
  *   The configuration name.
  * @param string $key
  *   The configuration key.
  * @param string $source_value
  *   The source configuration value to verify the correct value is returned
  *   from the configuration factory after the deletion.
  */
 protected function deleteLanguageOverride($config_name, $key, $source_value)
 {
     $translation_override = $this->languageManager->getLanguageConfigOverride($this->langcode, $config_name);
     $translation_override->clear($key)->save();
     $this->configFactory->reset($config_name);
     $this->assertConfigValue($config_name, $key, $source_value);
 }
Example #2
0
 /**
  * Updates all configuration translations for the names / languages provided.
  *
  * To be used when interface translation changes result in the need to update
  * configuration translations to keep them in sync.
  *
  * @param array $names
  *   Array of names of configuration objects to update.
  * @param array $langcodes
  *   (optional) Array of language codes to update. Defaults to all
  *   configurable languages.
  *
  * @return int
  *   Total number of configuration override and active configuration objects
  *   updated (saved or removed).
  */
 public function updateConfigTranslations(array $names, array $langcodes = array())
 {
     $langcodes = $langcodes ? $langcodes : array_keys($this->languageManager->getLanguages());
     $count = 0;
     foreach ($names as $name) {
         $translatable = $this->getTranslatableDefaultConfig($name);
         if (empty($translatable)) {
             // If there is nothing translatable in this configuration or not
             // supported, skip it.
             continue;
         }
         $active_langcode = $this->getActiveConfigLangcode($name);
         $active = $this->configStorage->read($name);
         foreach ($langcodes as $langcode) {
             $processed = $this->processTranslatableData($name, $active, $translatable, $langcode);
             if ($langcode != $active_langcode) {
                 // If the language code is not the same as the active storage
                 // language, we should update a configuration override.
                 if (!empty($processed)) {
                     // Update translation data in configuration override.
                     $this->saveTranslationOverride($name, $langcode, $processed);
                     $count++;
                 } else {
                     $override = $this->languageManager->getLanguageConfigOverride($langcode, $name);
                     if (!$override->isNew()) {
                         $data = $this->filterOverride($override->get(), $translatable);
                         if (empty($data)) {
                             // Delete language override if there is no data left at all.
                             // This means all prior translations in the override were locale
                             // managed.
                             $this->deleteTranslationOverride($name, $langcode);
                             $count++;
                         } else {
                             // If there were translatable elements besides locale managed
                             // items, save with only those, and remove the ones managed
                             // by locale only.
                             $this->saveTranslationOverride($name, $langcode, $data);
                             $count++;
                         }
                     }
                 }
             } elseif (locale_is_translatable($langcode)) {
                 // If the language code is the active storage language, we should
                 // update. If it is English, we should only update if English is also
                 // translatable.
                 $active = NestedArray::mergeDeepArray(array($active, $processed), TRUE);
                 $this->saveTranslationActive($name, $active);
                 $count++;
             }
         }
     }
     return $count;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     foreach ($this->mapper->getConfigNames() as $name) {
         $this->languageManager->getLanguageConfigOverride($this->language->id, $name)->delete();
     }
     // Flush all persistent caches.
     $this->moduleHandler->invokeAll('cache_flush');
     foreach (Cache::getBins() as $service_id => $cache_backend) {
         $cache_backend->deleteAll();
     }
     drupal_set_message($this->t('@language translation of %label was deleted', array('%label' => $this->mapper->getTitle(), '@language' => $this->language->name)));
     $form_state['redirect_route'] = $this->getCancelUrl();
 }
 /**
  * Updates all configuration translations for the names / languages provided.
  *
  * To be used when interface translation changes result in the need to update
  * configuration translations to keep them in sync.
  *
  * @param array $names
  *   Array of names of configuration objects to update.
  * @param array $langcodes
  *   (optional) Array of language codes to update. Defaults to all
  *   configurable languages.
  *
  * @return int
  *   Total number of configuration override and active configuration objects
  *   updated (saved or removed).
  */
 public function updateConfigTranslations(array $names, array $langcodes = array())
 {
     $langcodes = $langcodes ? $langcodes : array_keys($this->languageManager->getLanguages());
     $count = 0;
     foreach ($names as $name) {
         $translatable = $this->getTranslatableDefaultConfig($name);
         if (empty($translatable)) {
             // If there is nothing translatable in this configuration or not
             // supported, skip it.
             continue;
         }
         $active_langcode = $this->getActiveConfigLangcode($name);
         $active = $this->configStorage->read($name);
         foreach ($langcodes as $langcode) {
             $processed = $this->processTranslatableData($name, $active, $translatable, $langcode);
             // If the language code is not the same as the active storage
             // language, we should update the configuration override.
             if ($langcode != $active_langcode) {
                 $override = $this->languageManager->getLanguageConfigOverride($langcode, $name);
                 // Filter out locale managed configuration keys so that translations
                 // removed from Locale will be reflected in the config override.
                 $data = $this->filterOverride($override->get(), $translatable);
                 if (!empty($processed)) {
                     // Merge in the Locale managed translations with existing data.
                     $data = NestedArray::mergeDeepArray(array($data, $processed), TRUE);
                 }
                 if (empty($data) && !$override->isNew()) {
                     // The configuration override contains Locale overrides that no
                     // longer exist.
                     $this->deleteTranslationOverride($name, $langcode);
                     $count++;
                 } elseif (!empty($data)) {
                     // Update translation data in configuration override.
                     $this->saveTranslationOverride($name, $langcode, $data);
                     $count++;
                 }
             } elseif (locale_is_translatable($langcode)) {
                 // If the language code is the active storage language, we should
                 // update. If it is English, we should only update if English is also
                 // translatable.
                 $active = NestedArray::mergeDeepArray(array($active, $processed), TRUE);
                 $this->saveTranslationActive($name, $active);
                 $count++;
             }
         }
     }
     return $count;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $form_values = $form_state->getValue(array('translation', 'config_names'));
     foreach ($this->mapper->getConfigNames() as $name) {
         $schema = $this->typedConfigManager->get($name);
         // Set configuration values based on form submission and source values.
         $base_config = $this->configFactory()->getEditable($name);
         $config_translation = $this->languageManager->getLanguageConfigOverride($this->language->getId(), $name);
         $element = $this->createFormElement($schema);
         $element->setConfig($base_config, $config_translation, $form_values[$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();
         }
     }
     $form_state->setRedirect($this->mapper->getOverviewRoute(), $this->mapper->getOverviewRouteParameters());
 }
Example #6
0
 /**
  * {@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);
         $element->setConfig($base_config, $config_translation, $this->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 submitForm(array &$form, array &$form_state)
 {
     $form_values = $form_state['values']['config_names'];
     // For the form submission handling, use the raw data.
     $config_factory = $this->configFactory();
     $old_state = $config_factory->getOverrideState();
     $config_factory->setOverrideState(FALSE);
     foreach ($this->mapper->getConfigNames() as $name) {
         // Set configuration values based on form submission and source values.
         $base_config = $config_factory->get($name);
         $config_translation = $this->languageManager->getLanguageConfigOverride($this->language->id, $name);
         $locations = $this->localeStorage->getLocations(array('type' => 'configuration', 'name' => $name));
         $this->setConfig($this->language, $base_config, $config_translation, $form_values[$name], !empty($locations));
         // If no overrides, delete language specific configuration file.
         $saved_config = $config_translation->get();
         if (empty($saved_config)) {
             $config_translation->delete();
         } else {
             $config_translation->save();
         }
     }
     $config_factory->setOverrideState($old_state);
     $form_state['redirect_route'] = array('route_name' => $this->mapper->getOverviewRoute(), 'route_parameters' => $this->mapper->getOverviewRouteParameters());
 }
 /**
  * Ensures configuration was saved correctly.
  *
  * @param string $config_name
  *   The configuration name.
  * @param string $key
  *   The configuration key.
  * @param string $value
  *   The configuration value.
  * @param string $langcode
  *   The language code.
  *
  * @return bool
  *   TRUE if the assertion succeeded, FALSE otherwise.
  */
 protected function assertConfigOverride($config_name, $key, $value, $langcode)
 {
     $config_langcode = $this->configFactory->getEditable($config_name)->get('langcode');
     $override = $this->languageManager->getLanguageConfigOverride($langcode, $config_name);
     return $this->assertNotEqual($config_langcode, $langcode) && $this->assertEqual($override->get($key), $value);
 }
 /**
  * Checks whether a language has configuration translation.
  *
  * @param string $name
  *   Configuration name.
  * @param \Drupal\Core\Language\LanguageInterface $language
  *   A language object.
  *
  * @return bool
  *   A boolean indicating if a language has configuration translations.
  */
 public function hasTranslation($name, LanguageInterface $language)
 {
     $translation = $this->languageManager->getLanguageConfigOverride($language->id, $name);
     return !$translation->isNew();
 }