diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc
index 6d02b9d..320da77 100644
--- a/core/modules/content_translation/content_translation.admin.inc
+++ b/core/modules/content_translation/content_translation.admin.inc
@@ -368,7 +368,7 @@ function _content_translation_update_field_translatability($settings) {
array('content_translation_translatable_switch', array($translatable, $entity_type, $field_name)),
);
if ($field->hasData()) {
- $field_operations[] = array('content_translation_translatable_batch', array($translatable, $field_name));
+ $field_operations[] = array('content_translation_translatable_batch', array($translatable, $entity_type, $field_name));
$field_operations = $translatable ? $field_operations : array_reverse($field_operations);
}
$operations = array_merge($operations, $field_operations);
@@ -415,22 +415,15 @@ function content_translation_translatable_switch($translatable, $entity_type, $f
* @param bool $translatable
* Indicator of whether the field should be made translatable (TRUE) or
* untranslatble (FALSE).
+ * @param string $entity_type
+ * Field entity type.
* @param string $field_name
* Field machine name.
*/
-function content_translation_translatable_batch($translatable, $field_name, &$context) {
+function content_translation_translatable_batch($translatable, $entity_type, $field_name, &$context) {
// Determine the entity types to act on.
- $entity_types = array();
- foreach (field_info_instances() as $entity_type => $info) {
- foreach ($info as $bundle => $instances) {
- foreach ($instances as $instance_field_name => $instance) {
- if ($instance_field_name == $field_name) {
- $entity_types[] = $entity_type;
- break 2;
- }
- }
- }
- }
+ // @todo Clean-up implementation in https://drupal.org/node/2092573
+ $entity_types = array($entity_type);
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
@@ -496,31 +489,33 @@ function content_translation_translatable_batch($translatable, $field_name, &$co
// and only after we can remove the old ones. Otherwise we might have data
// loss, since the removal of the old translations might occur before the
// new ones are stored.
- if ($translatable && isset($entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED])) {
+ if ($translatable && !$entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name)->isEmpty()) {
// If the field is being switched to translatable and has data for
// Language::LANGCODE_NOT_SPECIFIED then we need to move the data to the right
// language.
- $entity->{$field_name}[$langcode] = $entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED];
+ $entity->getTranslation($langcode)
+ ->set($field_name, $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name)->getValue());
// Store the original value.
- _content_translation_update_field($entity_type, $entity, $field_name);
- $entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED] = array();
+ _content_translation_update_field($entity, $field_name);
// Remove the language neutral value.
- _content_translation_update_field($entity_type, $entity, $field_name);
+ $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->set($field_name, array());
+ _content_translation_update_field($entity, $field_name);
}
- elseif (!$translatable && isset($entity->{$field_name}[$langcode])) {
+ elseif (!$translatable && !$entity->getTranslation($langcode)->get($field_name)->isEmpty()) {
// The field has been marked untranslatable and has data in the entity
// language: we need to move it to Language::LANGCODE_NOT_SPECIFIED and drop the
// other translations.
- $entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED] = $entity->{$field_name}[$langcode];
+ $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)
+ ->set($field_name, $entity->getTranslation($langcode)->get($field_name)->getValue());
// Store the original value.
- _content_translation_update_field($entity_type, $entity, $field_name);
- // Remove translations.
- foreach ($entity->{$field_name} as $langcode => $items) {
- if ($langcode != Language::LANGCODE_NOT_SPECIFIED) {
- $entity->{$field_name}[$langcode] = array();
+ _content_translation_update_field($entity, $field_name);
+ // Remove all translations except Language::LANGCODE_NOT_SPECIFIED.
+ foreach ($entity->getTranslationLanguages() as $language) {
+ if ($language->id != Language::LANGCODE_NOT_SPECIFIED) {
+ $entity->getTranslation($language->id)->set($field_name, array());
}
}
- _content_translation_update_field($entity_type, $entity, $field_name);
+ _content_translation_update_field($entity, $field_name);
}
else {
// No need to save unchanged entities.
@@ -534,8 +529,13 @@ function content_translation_translatable_batch($translatable, $field_name, &$co
/**
* Stores the given field translations.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The entity to update.
+ * @param string $field_name
+ * The field to update.
*/
-function _content_translation_update_field($entity_type, EntityInterface $entity, $field_name) {
+function _content_translation_update_field(EntityInterface $entity, $field_name) {
$empty = 0;
$translations = $entity->getTranslationLanguages();
diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php b/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php
index 363cb1c..2b81acc 100644
--- a/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php
+++ b/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php
@@ -7,15 +7,23 @@
namespace Drupal\content_translation\Form;
+use Drupal\field\FieldInfo;
+use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\ConfirmFormBase;
-use Drupal\field\Entity\Field;
-use Drupal\field\Field as FieldInfo;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a confirm form for changing translatable status on translation
* fields.
*/
-class TranslatableForm extends ConfirmFormBase {
+class TranslatableForm extends ConfirmFormBase implements ContainerInjectionInterface {
+
+ /**
+ * The field info service.
+ *
+ * @var \Drupal\field\FieldInfo
+ */
+ protected $fieldInfo;
/**
* The field info we are changing translatable status on.
@@ -33,6 +41,25 @@ class TranslatableForm extends ConfirmFormBase {
protected $fieldName;
/**
+ * Creates a new TranslatableForm.
+ *
+ * @param \Drupal\field\FieldInfo $field_info
+ * The field info service.
+ */
+ public function __construct(FieldInfo $field_info) {
+ $this->fieldInfo = $field_info;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static (
+ $container->get('field.info')
+ );
+ }
+
+ /**
* {@inheritdoc}
*/
public function getFormId() {
@@ -47,7 +74,7 @@ public function getQuestion() {
$question = t('Are you sure you want to disable translation for the %name field?', array('%name' => $this->fieldName));
}
else {
- $question = t('Are you sure you want to enable translation for the %name field?', array('%name' => $this->fieldName));
+ $question = $this->t('Are you sure you want to enable translation for the %name field?', array('%name' => $this->fieldName));
}
return $question;
}
@@ -56,7 +83,7 @@ public function getQuestion() {
* {@inheritdoc}
*/
public function getDescription() {
- $description = t('By submitting this form these changes will apply to the %name field everywhere it is used.',
+ $description = $this->t('By submitting this form these changes will apply to the %name field everywhere it is used.',
array('%name' => $this->fieldName)
);
$description .= $this->field->isFieldTranslatable() ? "
" . t("All the existing translations of this field will be deleted.
This action cannot be undone.") : '';
@@ -81,7 +108,7 @@ public function getCancelRoute() {
*/
public function buildForm(array $form, array &$form_state, $entity_type = NULL, $field_name = NULL) {
$this->fieldName = $field_name;
- $this->fieldInfo = FieldInfo::fieldInfo()->getField($entity_type, $field_name);
+ $this->field = $this->fieldInfo->getField($entity_type, $field_name);
return parent::buildForm($form, $form_state);
}
@@ -126,13 +153,14 @@ public function submitForm(array &$form, array &$form_state) {
array(
'content_translation_translatable_batch', array(
!$translatable,
+ $this->field->entity_type,
$this->fieldName,
),
),
array(
'content_translation_translatable_switch', array(
!$translatable,
- $this->field['entity_type'],
+ $this->field->entity_type,
$this->fieldName,
),
),
@@ -140,7 +168,7 @@ public function submitForm(array &$form, array &$form_state) {
$operations = $translatable ? $operations : array_reverse($operations);
$t_args = array('%field' => $this->fieldName);
- $title = !$translatable ? t('Enabling translation for the %field field', $t_args) : t('Disabling translation for the %field field', $t_args);
+ $title = !$translatable ? $this->t('Enabling translation for the %field field', $t_args) : $this->t('Disabling translation for the %field field', $t_args);
$batch = array(
'title' => $title,
diff --git a/core/modules/language/language.admin.inc b/core/modules/language/language.admin.inc
index f9443c7..20b1269 100644
--- a/core/modules/language/language.admin.inc
+++ b/core/modules/language/language.admin.inc
@@ -337,104 +337,6 @@ function theme_language_negotiation_configure_browser_form_table($variables) {
}
/**
- * Returns the content language settings form.
- *
- * @deprecated Use \Drupal\language\Controller\LanguageController::contentSettings()
- */
-function language_content_settings_page() {
- return drupal_get_form('language_content_settings_form', language_entity_supported());
-}
-
-/**
- * Form constructor for the content language settings form.
- *
- * @param array $supported
- * Entity types with language support.
- *
- * @see language_content_settings_form_submit()
- *
- * @ingroup forms
- */
-function language_content_settings_form(array $form, array $form_state, array $supported) {
- $entity_info = entity_get_info();
- $labels = array();
- $default = array();
-
- foreach ($supported as $entity_type) {
- $labels[$entity_type] = isset($entity_info[$entity_type]['label']) ? $entity_info[$entity_type]['label'] : $entity_type;
- $default[$entity_type] = FALSE;
-
- // Check whether we have any custom setting.
- foreach (entity_get_bundles($entity_type) as $bundle => $bundle_info) {
- $conf = language_get_default_configuration($entity_type, $bundle);
- if (!empty($conf['language_show']) || $conf['langcode'] != 'site_default') {
- $default[$entity_type] = $entity_type;
- }
- $language_configuration[$entity_type][$bundle] = $conf;
- }
- }
-
- asort($labels);
-
- $path = drupal_get_path('module', 'language');
- $form = array(
- '#labels' => $labels,
- '#attached' => array(
- 'css' => array($path . '/css/language.admin.css'),
- ),
- );
-
- $form['entity_types'] = array(
- '#title' => t('Custom language settings'),
- '#type' => 'checkboxes',
- '#options' => $labels,
- '#default_value' => $default,
- );
-
- $form['settings'] = array('#tree' => TRUE);
-
- foreach ($labels as $entity_type => $label) {
- $info = $entity_info[$entity_type];
-
- $form['settings'][$entity_type] = array(
- '#title' => $label,
- '#type' => 'container',
- '#entity_type' => $entity_type,
- '#theme' => 'language_content_settings_table',
- '#bundle_label' => isset($info['bundle_label']) ? $info['bundle_label'] : $label,
- '#states' => array(
- 'visible' => array(
- ':input[name="entity_types[' . $entity_type . ']"]' => array('checked' => TRUE),
- ),
- ),
- );
-
- foreach (entity_get_bundles($entity_type) as $bundle => $bundle_info) {
- $form['settings'][$entity_type][$bundle]['settings'] = array(
- '#type' => 'item',
- '#label' => $bundle_info['label'],
- 'language' => array(
- '#type' => 'language_configuration',
- '#entity_information' => array(
- 'entity_type' => $entity_type,
- 'bundle' => $bundle,
- ),
- '#default_value' => $language_configuration[$entity_type][$bundle],
- ),
- );
- }
- }
-
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- );
-
- return $form;
-}
-
-/**
* Implements hook_preprocess_HOOK() for theme_language_content_settings_table().
*/
function template_preprocess_language_content_settings_table(&$variables) {
@@ -496,19 +398,6 @@ function theme_language_content_settings_table($variables) {
}
/**
- * Form submission handler for language_content_settings_form().
- */
-function language_content_settings_form_submit(array $form, array &$form_state) {
- $settings = &$form_state['values']['settings'];
- foreach ($settings as $entity_type => $entity_settings) {
- foreach ($entity_settings as $bundle => $bundle_settings) {
- language_save_default_configuration($entity_type, $bundle, $bundle_settings['settings']['language']);
- }
- }
- drupal_set_message(t('Settings successfully updated.'));
-}
-
-/**
* Helper function to disable the language switcher blocks.
*
* @param array $language_types
diff --git a/core/modules/language/language.module b/core/modules/language/language.module
index e5aa96a..32db222 100644
--- a/core/modules/language/language.module
+++ b/core/modules/language/language.module
@@ -124,22 +124,6 @@ function language_theme() {
}
/**
- * Returns a list of supported entity types.
- *
- * @return array
- * An array of entity type names.
- */
-function language_entity_supported() {
- $supported = array();
- foreach (entity_get_info() as $entity_type => $info) {
- if (!empty($info['translatable'])) {
- $supported[$entity_type] = $entity_type;
- }
- }
- return $supported;
-}
-
-/**
* Implements hook_element_info_alter().
*/
function language_element_info_alter(&$type) {
@@ -268,7 +252,7 @@ function language_configuration_element_process($element, &$form_state, &$form)
// Do not add the submit callback for the language content settings page,
// which is handled separately.
- if (array_search('language_content_settings_form_submit', $form['#submit']) === FALSE) {
+ if ($form['#form_id'] != 'language_content_settings_form') {
// Determine where to attach the language_configuration element submit handler.
// @todo Form API: Allow form widgets/sections to declare #submit handlers.
if (isset($form['actions']['submit']['#submit']) && array_search('language_configuration_element_submit', $form['actions']['submit']['#submit']) === FALSE) {
diff --git a/core/modules/language/language.routing.yml b/core/modules/language/language.routing.yml
index 3b76d56..7721db2 100644
--- a/core/modules/language/language.routing.yml
+++ b/core/modules/language/language.routing.yml
@@ -75,6 +75,6 @@ language.content_settings_page:
path: '/admin/config/regional/content-language'
defaults:
_title: 'Content language'
- _content: '\Drupal\language\Controller\LanguageController::contentSettings'
+ _form: 'Drupal\language\Form\ContentLanguageSettingsForm'
requirements:
_permission: 'administer languages'
diff --git a/core/modules/language/lib/Drupal/language/Controller/LanguageController.php b/core/modules/language/lib/Drupal/language/Controller/LanguageController.php
deleted file mode 100644
index 0724471..0000000
--- a/core/modules/language/lib/Drupal/language/Controller/LanguageController.php
+++ /dev/null
@@ -1,23 +0,0 @@
-entityManager->getDefinitions() as $entity_type => $info) {
- if (!empty($info['translatable'])) {
- $supported[$entity_type] = $entity_type;
- }
- }
- return $supported;
- }
-
- /**
* {@inheritdoc}
*/
public function getFormId() {
@@ -83,14 +67,17 @@ public function buildForm(array $form, array &$form_state) {
$labels = array();
$default = array();
- $bundles = entity_get_bundles();
+ $bundles = $this->entityManager->getAllBundleInfo();
$language_configuration = array();
- foreach ($this->entitySupported() as $entity_type) {
- $labels[$entity_type] = isset($entity_info[$entity_type]['label']) ? $entity_info[$entity_type]['label'] : $entity_type;
+ foreach ($entity_info as $entity_type =>$info) {
+ if (empty($info['translatable'])) {
+ continue;
+ }
+ $labels[$entity_type] = isset($info['label']) ? $info['label'] : $entity_type;
$default[$entity_type] = FALSE;
// Check whether we have any custom setting.
- foreach ($bundles as $bundle => $bundle_info) {
+ foreach ($bundles[$entity_type] as $bundle => $bundle_info) {
$conf = language_get_default_configuration($entity_type, $bundle);
if (!empty($conf['language_show']) || $conf['langcode'] != 'site_default') {
$default[$entity_type] = $entity_type;
@@ -135,7 +122,7 @@ public function buildForm(array $form, array &$form_state) {
),
);
- foreach ($bundles as $bundle => $bundle_info) {
+ foreach ($bundles[$entity_type] as $bundle => $bundle_info) {
$form['settings'][$entity_type][$bundle]['settings'] = array(
'#type' => 'item',
'#label' => $bundle_info['label'],
@@ -151,13 +138,11 @@ public function buildForm(array $form, array &$form_state) {
}
}
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => $this->t('Save'),
- );
-
- return parent::buildForm($form, $form_state);
+ $form = parent::buildForm($form, $form_state);
+ // @todo Remove this override. There are tests that check for explicitly for
+ // the button label which need to be adapted for that.
+ $form['actions']['submit']['#value'] = $this->t('Save');
+ return $form;
}
/**
@@ -167,16 +152,14 @@ public function submitForm(array &$form, array &$form_state) {
$config = $this->configFactory->get('language.settings');
foreach ($form_state['values']['settings'] as $entity_type => $entity_settings) {
foreach ($entity_settings as $bundle => $bundle_settings) {
- $config->set(language_get_default_configuration_settings_key($entity_type, $bundle),
- array(
- 'langcode' => $bundle_settings['settings']['language']['langcode'],
- 'language_show' => $bundle_settings['settings']['langcode']['language_show'],
- )
- );
+ $config->set(language_get_default_configuration_settings_key($entity_type, $bundle), array(
+ 'langcode' => $bundle_settings['settings']['language']['langcode'],
+ 'language_show' => $bundle_settings['settings']['language']['language_show'],
+ ));
}
}
$config->save();
- parent::submitForm($form, $form_state);
+ drupal_set_message($this->t('Settings successfully updated.'));
}
}