Commit 47cdba98 authored by catch's avatar catch
Browse files

Issue #1946462 by jibran, kim.pepper: Convert all confirm_form() in...

Issue #1946462 by jibran, kim.pepper: Convert all confirm_form() in content_translation.admin.inc and content_translation.pages.inc to the new form interface.
parent df7b6e69
......@@ -366,87 +366,6 @@ function _content_translation_update_field_translatability($settings) {
batch_set($batch);
}
/**
* Form constructor for the confirmation of translatability switching.
*/
function content_translation_translatable_form(array $form, array &$form_state, $field_name) {
$field = field_info_field($field_name);
$t_args = array('%name' => $field_name);
$warning = t('By submitting this form these changes will apply to the %name field everywhere it is used.', $t_args);
if ($field['translatable']) {
$title = t('Are you sure you want to disable translation for the %name field?', $t_args);
$warning .= "<br>" . t("<strong>All the existing translations of this field will be deleted.</strong><br>This action cannot be undone.");
}
else {
$title = t('Are you sure you want to enable translation for the %name field?', $t_args);
}
// We need to keep some information for later processing.
$form_state['field'] = $field;
// Store the 'translatable' status on the client side to prevent outdated form
// submits from toggling translatability.
$form['translatable'] = array(
'#type' => 'hidden',
'#default_value' => $field['translatable'],
);
return confirm_form($form, $title, '', $warning);
}
/**
* Form submission handler for content_translation_translatable_form().
*
* This submit handler maintains consistency between the translatability of an
* entity and the language under which the field data is stored. When a field is
* marked as translatable, all the data in
* $entity->{field_name}[Language::LANGCODE_NOT_SPECIFIED] is moved to
* $entity->{field_name}[$entity_language]. When a field is marked as
* untranslatable the opposite process occurs. Note that marking a field as
* untranslatable will cause all of its translations to be permanently removed,
* with the exception of the one corresponding to the entity language.
*/
function content_translation_translatable_form_submit(array $form, array $form_state) {
// This is the current state that we want to reverse.
$translatable = $form_state['values']['translatable'];
$field_name = $form_state['field']['field_name'];
$field = field_info_field($field_name);
if ($field['translatable'] !== $translatable) {
// Field translatability has changed since form creation, abort.
$t_args = array('%field_name');
$msg = $translatable ?
t('The field %field_name is already translatable. No change was performed.', $t_args):
t('The field %field_name is already untranslatable. No change was performed.', $t_args);
drupal_set_message($msg, 'warning');
return;
}
// If a field is untranslatable, it can have no data except under
// Language::LANGCODE_NOT_SPECIFIED. Thus we need a field to be translatable before we
// convert data to the entity language. Conversely we need to switch data back
// to Language::LANGCODE_NOT_SPECIFIED before making a field untranslatable lest we lose
// information.
$operations = array(
array('content_translation_translatable_batch', array(!$translatable, $field_name)),
array('content_translation_translatable_switch', array(!$translatable, $field_name)),
);
$operations = $translatable ? $operations : array_reverse($operations);
$t_args = array('%field' => $field_name);
$title = !$translatable ? t('Enabling translation for the %field field', $t_args) : t('Disabling translation for the %field field', $t_args);
$batch = array(
'title' => $title,
'operations' => $operations,
'finished' => 'content_translation_translatable_batch_done',
'file' => drupal_get_path('module', 'content_translation') . '/content_translation.admin.inc',
);
batch_set($batch);
}
/**
* Toggles translatability of the given field.
*
......
......@@ -203,10 +203,7 @@ function content_translation_menu() {
$items['admin/config/regional/content_translation/translatable/%'] = array(
'title' => 'Confirm change in translatability.',
'description' => 'Confirm page for changing field translatability.',
'page callback' => 'drupal_get_form',
'page arguments' => array('content_translation_translatable_form', 5),
'access arguments' => array('administer content translation'),
'file' => 'content_translation.admin.inc',
'route_name' => 'content_translation_translatable',
);
return $items;
......
content_translation_translatable:
pattern: 'admin/config/regional/content_translation/translatable/{field_name}'
defaults:
_form: 'Drupal\content_translation\Form\TranslatableForm'
requirements:
_permission: 'administer content translation'
<?php
/**
* @file
* Contains \Drupal\content_translation\Form\TranslatableForm.
*/
namespace Drupal\content_translation\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\field\Field;
/**
* Provides a confirm form for changing translatable status on translation
* fields.
*/
class TranslatableForm extends ConfirmFormBase {
/**
* The field info we are changing translatable status on.
*
* @var array.
*/
protected $fieldInfo;
/**
* The field name we are changing translatable
* status on.
*
* @var string.
*/
protected $fieldName;
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'content_translation_translatable_form';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
$action = $this->fieldInfo['translatable'] ? 'disable' : 'enable';
return t('Are you sure you want to %action translation for the %name field?',
array('%action' => $action, '%name' => $this->fieldName)
);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
$description = t('By submitting this form these changes will apply to the %name field everywhere it is used.',
array('%name' => $this->fieldName)
);
$description .= $this->fieldInfo['translatable'] ? "<br>" . t("<strong>All the existing translations of this field will be deleted.</strong><br>This action cannot be undone.") : '';
return $description;
}
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return '';
}
/**
* {@inheritdoc}
* @param string $field_name
* The field name.
*/
public function buildForm(array $form, array &$form_state, $field_name = NULL) {
$this->fieldName = $field_name;
$this->fieldInfo = Field::fieldInfo($field_name);
return parent::buildForm($form, $form_state);
}
/**
* Form submission handler.
*
* This submit handler maintains consistency between the translatability of an
* entity and the language under which the field data is stored. When a field
* is marked as translatable, all the data in
* $entity->{field_name}[Language::LANGCODE_NOT_SPECIFIED] is moved to
* $entity->{field_name}[$entity_language]. When a field is marked as
* untranslatable the opposite process occurs. Note that marking a field as
* untranslatable will cause all of its translations to be permanently
* removed, with the exception of the one corresponding to the entity
* language.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array $form_state
* An associative array containing the current state of the form.
*/
public function submitForm(array &$form, array &$form_state) {
// This is the current state that we want to reverse.
$translatable = $form_state['values']['translatable'];
if ($this->field['translatable'] !== $translatable) {
// Field translatability has changed since form creation, abort.
$t_args = array('%field_name');
$msg = $translatable ?
t('The field %field_name is already translatable. No change was performed.', $t_args):
t('The field %field_name is already untranslatable. No change was performed.', $t_args);
drupal_set_message($msg, 'warning');
return;
}
// If a field is untranslatable, it can have no data except under
// Language::LANGCODE_NOT_SPECIFIED. Thus we need a field to be translatable
// before we convert data to the entity language. Conversely we need to
// switch data back to Language::LANGCODE_NOT_SPECIFIED before making a
// field untranslatable lest we lose information.
$operations = array(
array(
'content_translation_translatable_batch', array(
!$translatable,
$this->fieldName,
),
),
array(
'content_translation_translatable_switch', array(
!$translatable,
$this->fieldName,
),
),
);
$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);
$batch = array(
'title' => $title,
'operations' => $operations,
'finished' => 'content_translation_translatable_batch_done',
'file' => drupal_get_path('module', 'content_translation') . '/content_translation.admin.inc',
);
batch_set($batch);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment