Commit 7f99ae9e authored by alexpott's avatar alexpott

Issue #1982138 by tim.plunkett, amateescu, swentel, yched: Clean out field_ui().admin.inc.

parent c3938baa
......@@ -19,6 +19,13 @@
*/
class FormatterPluginManager extends PluginManagerBase {
/**
* An array of formatter options for each field type.
*
* @var array
*/
protected $formatterOptions;
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase:$defaults.
*/
......@@ -125,4 +132,34 @@ public function prepareConfiguration($field_type, array $configuration) {
return $configuration;
}
/**
* Returns an array of formatter options for a field type.
*
* @param string|null $field_type
* (optional) The name of a field type, or NULL to retrieve all formatters.
*
* @return array
* If no field type is provided, returns a nested array of all formatters,
* keyed by field type.
*/
public function getOptions($field_type = NULL) {
if (!isset($this->formatterOptions)) {
$field_types = field_info_field_types();
$options = array();
foreach ($this->getDefinitions() as $name => $formatter) {
foreach ($formatter['field_types'] as $formatter_field_type) {
// Check that the field type exists.
if (isset($field_types[$formatter_field_type])) {
$options[$formatter_field_type][$name] = $formatter['label'];
}
}
}
$this->formatterOptions = $options;
}
if ($field_type) {
return !empty($this->formatterOptions[$field_type]) ? $this->formatterOptions[$field_type] : array();
}
return $this->formatterOptions;
}
}
......@@ -18,6 +18,13 @@
*/
class WidgetPluginManager extends PluginManagerBase {
/**
* An array of widget options for each field type.
*
* @var array
*/
protected $widgetOptions;
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase:$defaults.
*/
......@@ -120,4 +127,39 @@ public function prepareConfiguration($field_type, array $configuration) {
return $configuration;
}
/**
* Returns an array of widget type options for a field type.
*
* @param string|null $field_type
* (optional) The name of a field type, or NULL to retrieve all widget
* options. Defaults to NULL.
*
* @return array
* If no field type is provided, returns a nested array of all widget types,
* keyed by field type human name.
*/
public function getOptions($field_type = NULL) {
if (!isset($this->widgetOptions)) {
$options = array();
$field_types = field_info_field_types();
$widget_types = $this->getDefinitions();
uasort($widget_types, 'drupal_sort_weight');
foreach ($widget_types as $name => $widget_type) {
foreach ($widget_type['field_types'] as $widget_field_type) {
// Check that the field type exists.
if (isset($field_types[$widget_field_type])) {
$options[$widget_field_type][$name] = $widget_type['label'];
}
}
}
$this->widgetOptions = $options;
}
if (isset($field_type)) {
return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : array();
}
return $this->widgetOptions;
}
}
This diff is collapsed.
......@@ -226,6 +226,7 @@ function field_ui_theme() {
return array(
'field_ui_table' => array(
'render element' => 'elements',
'file' => 'field_ui.admin.inc',
),
);
}
......@@ -237,7 +238,6 @@ function field_ui_element_info() {
return array(
'field_ui_table' => array(
'#theme' => 'field_ui_table',
'#pre_render' => array('field_ui_table_pre_render'),
'#regions' => array('' => array()),
),
);
......@@ -261,40 +261,6 @@ function field_ui_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
Drupal::state()->set('menu_rebuild_needed', TRUE);
}
/**
* Identifies inactive fields within a bundle.
*/
function field_ui_inactive_instances($entity_type, $bundle_name = NULL) {
$params = array('entity_type' => $entity_type);
if (empty($bundle_name)) {
$active = field_info_instances($entity_type);
$inactive = array();
}
else {
// Restrict to the specified bundle. For consistency with the case where
// $bundle_name is NULL, the $active and $inactive arrays are keyed by
// bundle name first.
$params['bundle'] = $bundle_name;
$active = array($bundle_name => field_info_instances($entity_type, $bundle_name));
$inactive = array($bundle_name => array());
}
// Iterate on existing definitions, and spot those that do not appear in the
// $active list collected earlier.
$all_instances = field_read_instances($params, array('include_inactive' => TRUE));
foreach ($all_instances as $instance) {
if (!isset($active[$instance['bundle']][$instance['field_name']])) {
$inactive[$instance['bundle']][$instance['field_name']] = $instance;
}
}
if (!empty($bundle_name)) {
return $inactive[$bundle_name];
}
return $inactive;
}
/**
* Implements hook_form_FORM_ID_alter().
*
......
......@@ -8,12 +8,46 @@
namespace Drupal\field_ui;
use Drupal\field_ui\OverviewBase;
use Drupal\Core\Entity\EntityManager;
use Drupal\field\Plugin\Type\Formatter\FormatterPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field UI display overview form.
*/
class DisplayOverview extends OverviewBase {
/**
* The formatter plugin manager.
*
* @var \Drupal\field\Plugin\Type\Formatter\FormatterPluginManager
*/
protected $formatterManager;
/**
* Constructs a new DisplayOverview.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\field\Plugin\Type\Formatter\FormatterPluginManager $formatter_manager
* The formatter plugin manager.
*/
public function __construct(EntityManager $entity_manager, FormatterPluginManager $formatter_manager) {
parent::__construct($entity_manager);
$this->formatterManager = $formatter_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity'),
$container->get('plugin.manager.field.formatter')
);
}
/**
* Implements Drupal\field_ui\OverviewBase::getRegions().
*/
......@@ -70,6 +104,7 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
$table = array(
'#type' => 'field_ui_table',
'#pre_render' => array(array($this, 'tablePreRender')),
'#tree' => TRUE,
'#header' => array(
t('Field'),
......@@ -107,7 +142,7 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
'#row_type' => 'field',
'#region_callback' => 'field_ui_display_overview_row_region',
'#region_callback' => array($this, 'getRowRegion'),
'#js_settings' => array(
'rowHandler' => 'field',
'defaultFormatter' => $field_types[$field['type']]['default_formatter'],
......@@ -148,7 +183,7 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
),
);
$formatter_options = field_ui_formatter_options($field['type']);
$formatter_options = $this->formatterManager->getOptions($field['type']);
$formatter_options['hidden'] = '<' . t('Hidden') . '>';
$table[$name]['format'] = array(
'type' => array(
......@@ -174,7 +209,7 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
// Get the corresponding formatter object.
if ($display_options && $display_options['type'] != 'hidden') {
$formatter = drupal_container()->get('plugin.manager.field.formatter')->getInstance(array(
$formatter = $this->formatterManager->getInstance(array(
'instance' => $instance,
'view_mode' => $this->mode,
'configuration' => $display_options
......@@ -294,7 +329,7 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
'#row_type' => 'extra_field',
'#region_callback' => 'field_ui_display_overview_row_region',
'#region_callback' => array($this, 'getRowRegion'),
'#js_settings' => array('rowHandler' => 'field'),
'human_name' => array(
'#markup' => check_plain($extra_field['label']),
......@@ -576,4 +611,22 @@ public function multistepAjax($form, &$form_state) {
// Return the whole table.
return $form['fields'];
}
/**
* Returns the region to which a row in the display overview belongs.
*
* @param array $row
* The row element.
*
* @return string|null
* The region name this row belongs to.
*/
public function getRowRegion($row) {
switch ($row['#row_type']) {
case 'field':
case 'extra_field':
return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'content');
}
}
}
<?php
/**
* @file
* Contains \Drupal\field_ui\FieldUI.
*/
namespace Drupal\field_ui;
/**
* Static service container wrapper for Field UI.
*/
class FieldUI {
/**
* Returns the next redirect path in a multipage sequence.
*
* @return array
* An array of redirect paths.
*/
public static function getNextDestination() {
$next_destination = array();
$destinations = !empty($_REQUEST['destinations']) ? $_REQUEST['destinations'] : array();
if (!empty($destinations)) {
unset($_REQUEST['destinations']);
$path = array_shift($destinations);
$options = drupal_parse_url($path);
if ($destinations) {
$options['query']['destinations'] = $destinations;
}
$next_destination = array($options['path'], $options);
}
return $next_destination;
}
}
......@@ -107,7 +107,7 @@ public function submitForm(array &$form, array &$form_state) {
}
$admin_path = $this->entityManager->getAdminPath($this->instance->entity_type, $this->instance->bundle);
$form_state['redirect'] = field_ui_get_destinations(array($admin_path . '/fields'));
$form_state['redirect'] = "$admin_path/fields";
// Fields are purged on cron. However field module prevents disabling modules
// when field types they provided are used in a field until it is fully
......
......@@ -7,22 +7,33 @@
namespace Drupal\field_ui\Form;
use Drupal\Core\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Form\FormInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
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 {
class FieldEditForm implements FormInterface, ControllerInterface {
/**
* The field instance being edited.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
* @var \Drupal\field\FieldInstanceInterface
*/
protected $instance;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* {@inheritdoc}
*/
......@@ -30,11 +41,32 @@ public function getFormID() {
return 'field_ui_field_edit_form';
}
/**
* Constructs a new FieldEditForm object.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(EntityManager $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, FieldInstance $field_instance = NULL) {
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, FieldInstanceInterface $field_instance = NULL) {
$this->instance = $form_state['instance'] = $field_instance;
form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
$field = $this->instance->getField();
$form['#field'] = $field;
......@@ -129,7 +161,6 @@ public function validateForm(array &$form, array &$form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
$form_values = $form_state['values'];
$field_values = $form_values['field'];
......@@ -152,9 +183,13 @@ public function submitForm(array &$form, array &$form_state) {
try {
$field->save();
drupal_set_message(t('Updated field %label field settings.', array('%label' => $this->instance->label())));
$form_state['redirect'] = field_ui_next_destination($this->instance->entity_type, $this->instance->bundle);
$next_destination = FieldUI::getNextDestination();
if (empty($next_destination)) {
$next_destination = $this->entityManager->getAdminPath($this->instance->entity_type, $this->instance->bundle) . '/fields';
}
$form_state['redirect'] = $next_destination;
}
catch (Exception $e) {
catch (\Exception $e) {
drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $this->instance->label(), '%message' => $e->getMessage())), 'error');
}
}
......
......@@ -7,50 +7,13 @@
namespace Drupal\field_ui\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\ControllerInterface;
use Drupal\field\FieldInstanceInterface;
use Drupal\Core\Language\Language;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\field\Plugin\Type\Widget\WidgetPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for the field instance settings form.
*/
class FieldInstanceEditForm implements FormInterface, ControllerInterface {
/**
* The field instance being edited.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
/**
* The field widget plugin manager.
*
* @var \Drupal\field\Plugin\Type\Widget\WidgetPluginManager
*/
protected $widgetManager;
/**
* Constructs a new FieldInstanceEditForm object.
*
* @param \Drupal\field\Plugin\Type\Widget\WidgetPluginManager $widget_manager
* The field widget plugin manager.
*/
public function __construct(WidgetPluginManager $widget_manager) {
$this->widgetManager = $widget_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.field.widget')
);
}
class FieldInstanceEditForm extends FieldInstanceFormBase {
/**
* {@inheritdoc}
......@@ -62,8 +25,8 @@ public function getFormID() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, FieldInstance $field_instance = NULL) {
$this->instance = $form_state['instance'] = $field_instance;
public function buildForm(array $form, array &$form_state, FieldInstanceInterface $field_instance = NULL) {
parent::buildForm($form, $form_state, $field_instance);
$bundle = $this->instance['bundle'];
$entity_type = $this->instance['entity_type'];
......@@ -208,7 +171,6 @@ public function validateForm(array &$form, array &$form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
$entity = $form['#entity'];
$entity_form_display = $form['#entity_form_display'];
......@@ -241,7 +203,7 @@ public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('Field %label is required and uses the "hidden" widget. You might want to configure a default value.', array('%label' => $this->instance['label'])), 'warning');
}
$form_state['redirect'] = field_ui_next_destination($this->instance['entity_type'], $this->instance['bundle']);
$form_state['redirect'] = $this->getNextDestination();
}
/**
......
<?php
/**
* @file
* Contains \Drupal\field_ui\Form\FieldInstanceFormBase.
*/
namespace Drupal\field_ui\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\field\Plugin\Type\Widget\WidgetPluginManager;
use Drupal\field\FieldInstanceInterface;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class FieldInstanceFormBase implements FormInterface, ControllerInterface {
/**
* The field instance being edited.
*
* @var \Drupal\field\FieldInstanceInterface
*/
protected $instance;
/**
* The field widget plugin manager.
*
* @var \Drupal\field\Plugin\Type\Widget\WidgetPluginManager
*/
protected $widgetManager;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs a new field instance form.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\field\Plugin\Type\Widget\WidgetPluginManager $widget_manager
* The field widget plugin manager.
*/
public function __construct(EntityManager $entity_manager, WidgetPluginManager $widget_manager) {
$this->entityManager = $entity_manager;
$this->widgetManager = $widget_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity'),
$container->get('plugin.manager.field.widget')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, FieldInstanceInterface $field_instance = NULL) {
$this->instance = $form_state['instance'] = $field_instance;
form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
}
/**
* Returns the next redirect path in a multipage sequence.
*
* @return string|array
* Either the next path, or an array of redirect paths.
*/
protected function getNextDestination() {
$next_destination = FieldUI::getNextDestination();
if (empty($next_destination)) {
$next_destination = $this->entityManager->getAdminPath($this->instance->entity_type, $this->instance->bundle) . '/fields';
}
return $next_destination;
}
}
......@@ -7,49 +7,12 @@
namespace Drupal\field_ui\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\ControllerInterface;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
use Drupal\field\Plugin\Type\Widget\WidgetPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field\FieldInstanceInterface;
/**
* Provides a form for the widget selection form.
*/
class FieldWidgetTypeForm implements FormInterface, ControllerInterface {
/**
* The field instance being edited.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
/**
* The field widget plugin manager.
*
* @var \Drupal\field\Plugin\Type\Widget\WidgetPluginManager
*/
protected $widgetManager;
/**
* Constructs a new FieldWidgetTypeForm object.
*
* @param \Drupal\field\Plugin\Type\Widget\WidgetPluginManager $widget_manager
* The field widget plugin manager.
*/
public function __construct(WidgetPluginManager $widget_manager) {
$this->widgetManager = $widget_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.field.widget')
);
}
class FieldWidgetTypeForm extends FieldInstanceFormBase {
/**
* {@inheritdoc}
......@@ -61,9 +24,9 @@ public function getFormID() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, FieldInstance $field_instance = NULL) {
$this->instance = $form_state['instance'] = $field_instance;
form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
public function buildForm(array $form, array &$form_state, FieldInstanceInterface $field_instance = NULL) {
parent::buildForm($form, $form_state, $field_instance);
drupal_set_title($this->instance['label']);
$bundle = $this->instance['bundle'];
......@@ -86,7 +49,7 @@ public function buildForm(array $form, array &$form_state, FieldInstance $field_
'#type' => 'select',
'#title' => t('Widget type'),
'#required' => TRUE,
'#options' => field_ui_widget_type_options($field['type']),
'#options' => $this->widgetManager->getOptions($field['type']),
'#default_value' => $entity_form_display->getWidget($field_name)->getPluginId(),
'#description' => t('The type of form element you would like to present to the user when creating this field in the %type type.', array('%type' => $bundle_label)),
);
......@@ -126,11 +89,11 @@ public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('Field %label is required and uses the "hidden" widget. You might want to configure a default value.', array('%label' => $instance['label'])), 'warning');
}
}
catch (Exception $e) {
catch (\Exception $e) {
drupal_set_message(t('There was a problem changing the widget for field %label.', array('%label' => $instance['label'])), 'error');
}
$form_state['redirect'] = field_ui_next_destination($entity_type, $bundle);
$form_state['redirect'] = $this->getNextDestination();
}
}
......@@ -8,9 +8,9 @@
namespace Drupal\field_ui;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Form\FormInterface;
/**
* Abstract base class for Field UI overview forms.
......@@ -80,7 +80,6 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL,
$bundle = $entity_info['bundle_prefix'] . $bundle;
}
form_load_include($form_state, 'inc', 'field_ui', 'field_ui.admin');
$this->entity_type = $entity_type;
$this->bundle = $bundle;
$this->adminPath = $this->entityManager->getAdminPath($this->entity_type, $this->bundle);
......@@ -130,4 +129,98 @@ public function getRegionOptions() {
return $options;
}
/**
* Performs pre-render tasks on field_ui_table elements.
*
* This function is assigned as a #pre_render callback in
* field_ui_element_info().
*
* @see drupal_render().
*/
public function tablePreRender($elements) {
$js_settings = array();
// For each region, build the tree structure from the weight and parenting
// data contained in the flat form structure, to determine row order and
// indentation.
$regions = $elements['#regions'];