Commit 5ab08474 authored by catch's avatar catch

Issue #2071493 by tim.plunkett: Modernize field_ui().module forms.

parent 7e1dbf11
......@@ -8,7 +8,6 @@
namespace Drupal\field_ui;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\entity\EntityDisplayBaseInterface;
use Drupal\field\FieldInstanceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -16,7 +15,7 @@
/**
* Field UI display overview form.
*/
class DisplayOverview extends DisplayOverviewBase implements ControllerInterface {
class DisplayOverview extends DisplayOverviewBase {
/**
* {@inheritdoc}
......@@ -24,6 +23,7 @@ class DisplayOverview extends DisplayOverviewBase implements ControllerInterface
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity'),
$container->get('plugin.manager.entity.field.field_type'),
$container->get('plugin.manager.field.formatter')
);
}
......@@ -46,7 +46,7 @@ protected function buildFieldRow($field_id, FieldInstanceInterface $instance, En
$label = array(
'label' => array(
'#type' => 'select',
'#title' => t('Label display for @title', array('@title' => $instance['label'])),
'#title' => $this->t('Label display for @title', array('@title' => $instance['label'])),
'#title_display' => 'invisible',
'#options' => $this->getFieldLabelOptions(),
'#default_value' => $display_options ? $display_options['label'] : 'above',
......@@ -57,10 +57,10 @@ protected function buildFieldRow($field_id, FieldInstanceInterface $instance, En
$field_row = array_slice($field_row, 0, $label_position, TRUE) + $label + array_slice($field_row, $label_position, count($field_row) - 1, TRUE);
// Update the (invisible) title of the 'plugin' column.
$field_row['plugin']['#title'] = t('Formatter for @title', array('@title' => $instance['label']));
$field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $instance['label']));
if (!empty($field_row['plugin']['settings_edit_form'])) {
$plugin_type_info = $entity_display->getRenderer($field_id)->getPluginDefinition();
$field_row['plugin']['settings_edit_form']['label']['#markup'] = t('Format settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
$field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Format settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
}
return $field_row;
......@@ -119,7 +119,7 @@ protected function getPlugin($instance, $configuration) {
* {@inheritdoc}
*/
protected function getPluginOptions($field_type) {
return parent::getPluginOptions($field_type) + array('hidden' => '- ' . t('Hidden') . ' -');
return parent::getPluginOptions($field_type) + array('hidden' => '- ' . $this->t('Hidden') . ' -');
}
/**
......@@ -157,11 +157,11 @@ protected function saveDisplayModeSettings($display_mode_settings) {
*/
protected function getTableHeader() {
return array(
t('Field'),
t('Weight'),
t('Parent'),
t('Label'),
array('data' => t('Format'), 'colspan' => 3),
$this->t('Field'),
$this->t('Weight'),
$this->t('Parent'),
$this->t('Label'),
array('data' => $this->t('Format'), 'colspan' => 3),
);
}
......@@ -180,9 +180,9 @@ protected function getOverviewPath($mode) {
*/
protected function getFieldLabelOptions() {
return array(
'above' => t('Above'),
'inline' => t('Inline'),
'hidden' => '- ' . t('Hidden') . ' -',
'above' => $this->t('Above'),
'inline' => $this->t('Inline'),
'hidden' => '- ' . $this->t('Hidden') . ' -',
);
}
......
......@@ -46,14 +46,14 @@ public static function create(ContainerInterface $container) {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete the field %field?', array('%field' => $this->entity->label()));
return $this->t('Are you sure you want to delete the field %field?', array('%field' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
return $this->t('Delete');
}
/**
......@@ -73,10 +73,10 @@ public function submit(array $form, array &$form_state) {
if ($field && !$field['locked']) {
$this->entity->delete();
drupal_set_message(t('The field %field has been deleted from the %type content type.', array('%field' => $this->entity->label(), '%type' => $bundle_label)));
drupal_set_message($this->t('The field %field has been deleted from the %type content type.', array('%field' => $this->entity->label(), '%type' => $bundle_label)));
}
else {
drupal_set_message(t('There was a problem removing the %field from the %type content type.', array('%field' => $this->entity->label(), '%type' => $bundle_label)), 'error');
drupal_set_message($this->t('There was a problem removing the %field from the %type content type.', array('%field' => $this->entity->label(), '%type' => $bundle_label)), 'error');
}
$admin_path = $this->entityManager->getAdminPath($this->entity->entity_type, $this->entity->bundle);
......
......@@ -7,19 +7,20 @@
namespace Drupal\field_ui\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Entity\EntityNG;
use Drupal\Core\Form\FormBase;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\field\FieldInfo;
use Drupal\field\FieldInstanceInterface;
use Drupal\field\Field;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for the field settings edit page.
*/
class FieldEditForm implements FormInterface, ControllerInterface {
class FieldEditForm extends FormBase {
/**
* The field instance being edited.
......@@ -35,6 +36,20 @@ class FieldEditForm implements FormInterface, ControllerInterface {
*/
protected $entityManager;
/**
* The field info service.
*
* @var \Drupal\field\FieldInfo
*/
protected $fieldInfo;
/**
* The typed data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected $typedData;
/**
* {@inheritdoc}
*/
......@@ -47,9 +62,15 @@ public function getFormID() {
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\field\FieldInfo $field_info
* The field info service.
* @param \Drupal\Core\TypedData\TypedDataManager $typed_data
* The typed data manager.
*/
public function __construct(EntityManager $entity_manager) {
public function __construct(EntityManager $entity_manager, FieldInfo $field_info, TypedDataManager $typed_data) {
$this->entityManager = $entity_manager;
$this->fieldInfo = $field_info;
$this->typedData = $typed_data;
}
/**
......@@ -57,7 +78,9 @@ public function __construct(EntityManager $entity_manager) {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity')
$container->get('plugin.manager.entity'),
$container->get('field.info'),
$container->get('typed_data')
);
}
......@@ -72,7 +95,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
drupal_set_title($this->instance->label());
$description = '<p>' . t('These settings apply to the %field field everywhere it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.', array('%field' => $this->instance->label())) . '</p>';
$description = '<p>' . $this->t('These settings apply to the %field field everywhere it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.', array('%field' => $this->instance->label())) . '</p>';
// Create a form structure for the field values.
$form['field'] = array(
......@@ -83,7 +106,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
// See if data already exists for this field.
// If so, prevent changes to the field settings.
if ($field->hasData()) {
$form['field']['#prefix'] = '<div class="messages messages--error">' . t('There is data for this field in the database. The field settings can no longer be changed.') . '</div>' . $form['field']['#prefix'];
$form['field']['#prefix'] = '<div class="messages messages--error">' . $this->t('There is data for this field in the database. The field settings can no longer be changed.') . '</div>' . $form['field']['#prefix'];
}
// Build the configurable field values.
......@@ -96,15 +119,15 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
'#parents' => array('field'),
'#field_prefix' => '<div class="container-inline">',
'#field_suffix' => '</div>',
'#title' => t('Allowed number of values'),
'#title' => $this->t('Allowed number of values'),
);
$form['field']['cardinality_container']['cardinality'] = array(
'#type' => 'select',
'#title' => t('Allowed number of values'),
'#title' => $this->t('Allowed number of values'),
'#title_display' => 'invisible',
'#options' => array(
'number' => t('Limited'),
FIELD_CARDINALITY_UNLIMITED => t('Unlimited'),
'number' => $this->t('Limited'),
FIELD_CARDINALITY_UNLIMITED => $this->t('Unlimited'),
),
'#default_value' => ($cardinality == FIELD_CARDINALITY_UNLIMITED) ? FIELD_CARDINALITY_UNLIMITED : 'number',
);
......@@ -112,7 +135,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
'#type' => 'number',
'#default_value' => $cardinality != FIELD_CARDINALITY_UNLIMITED ? $cardinality : 1,
'#min' => 1,
'#title' => t('Limit'),
'#title' => $this->t('Limit'),
'#title_display' => 'invisible',
'#size' => 2,
'#states' => array(
......@@ -141,7 +164,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
$form['field']['settings'] += $this->getFieldItem($entity, $field['field_name'])->settingsForm($form, $form_state, $field->hasData());
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save field settings'));
$form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save field settings'));
return $form;
}
......@@ -153,7 +176,7 @@ public function validateForm(array &$form, array &$form_state) {
$cardinality = $form_state['values']['field']['cardinality'];
$cardinality_number = $form_state['values']['field']['cardinality_number'];
if ($cardinality === 'number' && empty($cardinality_number)) {
form_error($form['field']['cardinality_container']['cardinality_number'], t('Number of values is required.'));
form_error($form['field']['cardinality_container']['cardinality_number'], $this->t('Number of values is required.'));
}
}
......@@ -174,7 +197,7 @@ public function submitForm(array &$form, array &$form_state) {
unset($field_values['container']);
// Merge incoming form values into the existing field.
$field = Field::fieldInfo()->getField($field_values['field_name']);
$field = $this->fieldInfo->getField($field_values['field_name']);
foreach ($field_values as $key => $value) {
$field[$key] = $value;
}
......@@ -182,7 +205,7 @@ public function submitForm(array &$form, array &$form_state) {
// Update the field.
try {
$field->save();
drupal_set_message(t('Updated field %label field settings.', array('%label' => $this->instance->label())));
drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $this->instance->label())));
$next_destination = FieldUI::getNextDestination();
if (empty($next_destination)) {
$next_destination = $this->entityManager->getAdminPath($this->instance->entity_type, $this->instance->bundle) . '/fields';
......@@ -190,7 +213,7 @@ public function submitForm(array &$form, array &$form_state) {
$form_state['redirect'] = $next_destination;
}
catch (\Exception $e) {
drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $this->instance->label(), '%message' => $e->getMessage())), 'error');
drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $this->instance->label(), '%message' => $e->getMessage())), 'error');
}
}
......@@ -208,12 +231,12 @@ public function submitForm(array &$form, array &$form_state) {
* The field item object.
*/
protected function getFieldItem(EntityInterface $entity, $field_name) {
if ($entity instanceof \Drupal\Core\Entity\EntityNG) {
if ($entity instanceof EntityNG) {
$item = $entity->get($field_name)->offsetGet(0);
}
else {
$definitions = \Drupal::entityManager()->getFieldDefinitions($entity->entityType(), $entity->bundle());
$item = \Drupal::typedData()->create($definitions[$field_name], array(), $field_name, $entity)->offsetGet(0);
$definitions = $this->entityManager->getFieldDefinitions($entity->entityType(), $entity->bundle());
$item = $this->typedData->create($definitions[$field_name], array(), $field_name, $entity)->offsetGet(0);
}
return $item;
}
......
......@@ -7,12 +7,11 @@
namespace Drupal\field_ui\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityNG;
use Drupal\Core\Entity\Field\FieldTypePluginManager;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Language\Language;
use Drupal\field\FieldInstanceInterface;
use Drupal\field\Plugin\Type\Widget\WidgetPluginManager;
......@@ -22,7 +21,7 @@
/**
* Provides a form for the field instance settings form.
*/
class FieldInstanceEditForm implements FormInterface, ControllerInterface {
class FieldInstanceEditForm extends FormBase {
/**
* The field instance being edited.
......@@ -98,7 +97,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
$entity_form_display = entity_get_form_display($entity_type, $bundle, 'default');
$bundles = entity_get_bundles();
drupal_set_title(t('%instance settings for %bundle', array(
drupal_set_title($this->t('%instance settings for %bundle', array(
'%instance' => $this->instance->label(),
'%bundle' => $bundles[$entity_type][$bundle]['label'],
)), PASS_THROUGH);
......@@ -112,7 +111,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
if (!empty($field['locked'])) {
$form['locked'] = array(
'#markup' => t('The field %field is locked and cannot be edited.', array('%field' => $this->instance->label())),
'#markup' => $this->t('The field %field is locked and cannot be edited.', array('%field' => $this->instance->label())),
);
return $form;
}
......@@ -139,7 +138,7 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
// Build the configurable instance values.
$form['instance']['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#title' => $this->t('Label'),
'#default_value' => $this->instance->label() ?: $field['field_name'],
'#required' => TRUE,
'#weight' => -20,
......@@ -147,16 +146,16 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
$form['instance']['description'] = array(
'#type' => 'textarea',
'#title' => t('Help text'),
'#title' => $this->t('Help text'),
'#default_value' => !empty($this->instance['description']) ? $this->instance['description'] : '',
'#rows' => 5,
'#description' => t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '<br />' . t('This field supports tokens.'),
'#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '<br />' . $this->t('This field supports tokens.'),
'#weight' => -10,
);
$form['instance']['required'] = array(
'#type' => 'checkbox',
'#title' => t('Required field'),
'#title' => $this->t('Required field'),
'#default_value' => !empty($this->instance['required']),
'#weight' => -5,
);
......@@ -173,11 +172,11 @@ public function buildForm(array $form, array &$form_state, FieldInstanceInterfac
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings')
'#value' => $this->t('Save settings')
);
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete field'),
'#value' => $this->t('Delete field'),
'#submit' => array(array($this, 'delete')),
);
return $form;
......@@ -239,7 +238,7 @@ public function submitForm(array &$form, array &$form_state) {
}
$this->instance->save();
drupal_set_message(t('Saved %label configuration.', array('%label' => $this->instance->label())));
drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->instance->label())));
$form_state['redirect'] = $this->getNextDestination();
}
......@@ -265,9 +264,9 @@ protected function getDefaultValueWidget($field, array &$form, &$form_state) {
$element = array(
'#type' => 'details',
'#title' => t('Default value'),
'#title' => $this->t('Default value'),
'#tree' => TRUE,
'#description' => t('The default value for this field, used when creating new content.'),
'#description' => $this->t('The default value for this field, used when creating new content.'),
// Stick to an empty 'parents' on this form in order not to breaks widgets
// that do not use field_widget_[field|instance]() and still access
// $form_state['field'] directly.
......
......@@ -8,7 +8,6 @@
namespace Drupal\field_ui;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\entity\EntityDisplayBaseInterface;
use Drupal\field\FieldInstanceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -16,7 +15,7 @@
/**
* Field UI form display overview form.
*/
class FormDisplayOverview extends DisplayOverviewBase implements ControllerInterface {
class FormDisplayOverview extends DisplayOverviewBase {
/**
* {@inheritdoc}
......@@ -24,6 +23,7 @@ class FormDisplayOverview extends DisplayOverviewBase implements ControllerInter
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity'),
$container->get('plugin.manager.entity.field.field_type'),
$container->get('plugin.manager.field.widget')
);
}
......@@ -42,10 +42,10 @@ protected function buildFieldRow($field_id, FieldInstanceInterface $instance, En
$field_row = parent::buildFieldRow($field_id, $instance, $entity_display, $form, $form_state);
// Update the (invisible) title of the 'plugin' column.
$field_row['plugin']['#title'] = t('Formatter for @title', array('@title' => $instance['label']));
$field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $instance['label']));
if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $entity_display->getRenderer($field_id))) {
$plugin_type_info = $plugin->getPluginDefinition();
$field_row['plugin']['settings_edit_form']['label']['#markup'] = t('Widget settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
$field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Widget settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
}
return $field_row;
......@@ -117,10 +117,10 @@ protected function saveDisplayModeSettings($display_mode_settings) {
*/
protected function getTableHeader() {
return array(
t('Field'),
t('Weight'),
t('Parent'),
array('data' => t('Widget'), 'colspan' => 3),
$this->t('Field'),
$this->t('Weight'),
$this->t('Parent'),
array('data' => $this->t('Widget'), 'colspan' => 3),
);
}
......
......@@ -7,15 +7,14 @@
namespace Drupal\field_ui;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Form\FormInterface;
/**
* Abstract base class for Field UI overview forms.
*/
abstract class OverviewBase implements FormInterface, ControllerInterface {
abstract class OverviewBase extends FormBase {
/**
* The name of the entity type.
......@@ -90,12 +89,6 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
}
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
*/
public function validateForm(array &$form, array &$form_state) {
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
*/
......@@ -111,12 +104,12 @@ public function submitForm(array &$form, array &$form_state) {
* return array(
* 'content' => array(
* // label for the region.
* 'title' => t('Content'),
* 'title' => $this->t('Content'),
* // Indicates if the region is visible in the UI.
* 'invisible' => TRUE,
* // A mesage to indicate that there is nothing to be displayed in
* // the region.
* 'message' => t('No field is displayed.'),
* 'message' => $this->t('No field is displayed.'),
* ),
* );
* @endcode
......
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