Commit 699680d2 authored by catch's avatar catch

Issue #1792600 by Stalski, fubhy, swentel: Refactor field_ui() so common...

Issue #1792600 by Stalski, fubhy, swentel: Refactor field_ui() so common behavior for fields and display overview screens are extracted.
parent 6323f118
......@@ -6,6 +6,8 @@
*/
use Drupal\field\FieldInstance;
use Drupal\field_ui\FieldOverview;
use Drupal\field_ui\DisplayOverview;
/**
* Page callback: Lists all defined fields for quick reference.
......@@ -100,18 +102,18 @@ function _field_ui_reduce_order($array, $a) {
* Returns the region to which a row in the 'Manage fields' screen belongs.
*
* This function is used as a #region_callback in
* field_ui_field_overview_form(). It is called during
* Drupal\field_ui\DisplayOverview::form(). It is called during
* field_ui_table_pre_render().
*/
function field_ui_field_overview_row_region($row) {
switch ($row['#row_type']) {
case 'field':
case 'extra_field':
return 'main';
return 'content';
case 'add_new_field':
// If no input in 'label', assume the row has not been dragged out of the
// 'add new' section.
return (!empty($row['label']['#value']) ? 'main' : 'add_new');
return (!empty($row['label']['#value']) ? 'content' : 'hidden');
}
}
......@@ -119,14 +121,14 @@ function field_ui_field_overview_row_region($row) {
* Returns the region to which a row in the 'Manage display' screen belongs.
*
* This function is used as a #region_callback in
* field_ui_field_overview_form(), and is called during
* Drupal\field_ui\FieldOverview::form(), and is called during
* field_ui_table_pre_render().
*/
function field_ui_display_overview_row_region($row) {
switch ($row['#row_type']) {
case 'field':
case 'extra_field':
return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'content');
}
}
......@@ -241,7 +243,7 @@ function theme_field_ui_table($variables) {
$region_name_class = drupal_html_class($region_name);
// Add region rows.
if (isset($region['title'])) {
if (isset($region['title']) && empty($region['invisible'])) {
$table['rows'][] = array(
'class' => array('region-title', 'region-' . $region_name_class . '-title'),
'no_striping' => TRUE,
......@@ -290,437 +292,34 @@ function theme_field_ui_table($variables) {
}
/**
* Form constructor for the 'Manage fields' form of a bundle.
* Returns the built and processed 'Manage fields' form of a bundle.
*
* The resulting form allows fields and pseudo-fields to be re-ordered.
*
* @param string $entity_type
* The entity type for the fieldable entity.
* @param string $bundle
* The bundle for the fieldable entity.
*
* @return
* The processed form for the given entity type and bundle.
*
* @see field_ui_menu()
* @see field_ui_field_overview_form_validate()
* @see field_ui_field_overview_form_submit()
* @ingroup forms
*/
function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle) {
function field_ui_field_overview($entity_type, $bundle) {
$bundle = field_extract_bundle($entity_type, $bundle);
field_ui_inactive_message($entity_type, $bundle);
$admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
// When displaying the form, make sure the list of fields is up-to-date.
if (empty($form_state['post'])) {
field_info_cache_clear();
}
// Gather bundle information.
$instances = field_info_instances($entity_type, $bundle);
$field_types = field_info_field_types();
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
$form += array(
'#entity_type' => $entity_type,
'#bundle' => $bundle,
'#fields' => array_keys($instances),
'#extra' => array_keys($extra_fields),
);
$field_overview = new FieldOverview($entity_type, $bundle);
$table = array(
'#type' => 'field_ui_table',
'#tree' => TRUE,
'#header' => array(
t('Label'),
t('Weight'),
t('Parent'),
t('Machine name'),
t('Field type'),
t('Widget'),
t('Operations'),
),
'#parent_options' => array(),
'#regions' => array(
'main' => array('message' => t('No fields are present yet.')),
'add_new' => array('title' => ' '),
),
'#attributes' => array(
'class' => array('field-ui-overview'),
'id' => 'field-overview',
),
);
$form_state = array();
$form_state['build_info']['callback'] = array($field_overview, 'form');
$form_state['build_info']['args'] = array($entity_type, $bundle);
// Fields.
foreach ($instances as $name => $instance) {
$field = field_info_field($instance['field_name']);
$admin_field_path = $admin_path . '/fields/' . $instance['field_name'];
$widget_definition = $instance->getWidget()->getDefinition();
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
'#row_type' => 'field',
'#region_callback' => 'field_ui_field_overview_row_region',
'label' => array(
'#markup' => check_plain($instance['label']),
),
'weight' => array(
'#type' => 'textfield',
'#title' => t('Weight for @title', array('@title' => $instance['label'])),
'#title_display' => 'invisible',
'#default_value' => $instance['widget']['weight'],
'#size' => 3,
'#attributes' => array('class' => array('field-weight')),
),
'parent_wrapper' => array(
'parent' => array(
'#type' => 'select',
'#title' => t('Parent for @title', array('@title' => $instance['label'])),
'#title_display' => 'invisible',
'#options' => $table['#parent_options'],
'#empty_value' => '',
'#attributes' => array('class' => array('field-parent')),
'#parents' => array('fields', $name, 'parent'),
),
'hidden_name' => array(
'#type' => 'hidden',
'#default_value' => $name,
'#attributes' => array('class' => array('field-name')),
),
),
'field_name' => array(
'#markup' => $instance['field_name'],
),
'type' => array(
'#type' => 'link',
'#title' => t($field_types[$field['type']]['label']),
'#href' => $admin_field_path . '/field-settings',
'#options' => array('attributes' => array('title' => t('Edit field settings.'))),
),
'widget_type' => array(
'#type' => 'link',
'#title' => $widget_definition['label'],
'#href' => $admin_field_path . '/widget-type',
'#options' => array('attributes' => array('title' => t('Change widget type.'))),
),
);
$links = array();
$links['edit'] = array(
'title' => t('edit'),
'href' => $admin_field_path,
'attributes' => array('title' => t('Edit instance settings.')),
);
$links['delete'] = array(
'title' => t('delete'),
'href' => "$admin_field_path/delete",
'attributes' => array('title' => t('Delete instance.')),
);
$table[$name]['operations']['data'] = array(
'#type' => 'operations',
'#links' => $links,
);
if (!empty($instance['locked'])) {
$table[$name]['operations'] = array('#value' => t('Locked'));
$table[$name]['#attributes']['class'][] = 'menu-disabled';
}
}
// Non-field elements.
foreach ($extra_fields as $name => $extra_field) {
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
'#row_type' => 'extra_field',
'#region_callback' => 'field_ui_field_overview_row_region',
'label' => array(
'#markup' => check_plain($extra_field['label']),
),
'weight' => array(
'#type' => 'textfield',
'#default_value' => $extra_field['weight'],
'#size' => 3,
'#attributes' => array('class' => array('field-weight')),
'#title_display' => 'invisible',
'#title' => t('Weight for @title', array('@title' => $extra_field['label'])),
),
'parent_wrapper' => array(
'parent' => array(
'#type' => 'select',
'#title' => t('Parent for @title', array('@title' => $extra_field['label'])),
'#title_display' => 'invisible',
'#options' => $table['#parent_options'],
'#empty_value' => '',
'#attributes' => array('class' => array('field-parent')),
'#parents' => array('fields', $name, 'parent'),
),
'hidden_name' => array(
'#type' => 'hidden',
'#default_value' => $name,
'#attributes' => array('class' => array('field-name')),
),
),
'field_name' => array(
'#markup' => $name,
),
'type' => array(
'#markup' => isset($extra_field['description']) ? $extra_field['description'] : '',
'#cell_attributes' => array('colspan' => 2),
),
'operations' => array(
'#markup' => '',
),
);
}
// Additional row: add new field.
$max_weight = field_info_max_weight($entity_type, $bundle, 'form');
$field_type_options = field_ui_field_type_options();
$widget_type_options = field_ui_widget_type_options(NULL, TRUE);
if ($field_type_options && $widget_type_options) {
$name = '_add_new_field';
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'tabledrag-leaf', 'add-new')),
'#row_type' => 'add_new_field',
'#region_callback' => 'field_ui_field_overview_row_region',
'label' => array(
'#type' => 'textfield',
'#title' => t('New field label'),
'#title_display' => 'invisible',
'#size' => 15,
'#description' => t('Label'),
'#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add new field') .'</div>',
'#suffix' => '</div>',
),
'weight' => array(
'#type' => 'textfield',
'#default_value' => $max_weight + 1,
'#size' => 3,
'#title_display' => 'invisible',
'#title' => t('Weight for new field'),
'#attributes' => array('class' => array('field-weight')),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
),
'parent_wrapper' => array(
'parent' => array(
'#type' => 'select',
'#title' => t('Parent for new field'),
'#title_display' => 'invisible',
'#options' => $table['#parent_options'],
'#empty_value' => '',
'#attributes' => array('class' => array('field-parent')),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
'#parents' => array('fields', $name, 'parent'),
),
'hidden_name' => array(
'#type' => 'hidden',
'#default_value' => $name,
'#attributes' => array('class' => array('field-name')),
),
),
'field_name' => array(
'#type' => 'machine_name',
'#title' => t('New field name'),
'#title_display' => 'invisible',
// This field should stay LTR even for RTL languages.
'#field_prefix' => '<span dir="ltr">field_',
'#field_suffix' => '</span>&lrm;',
'#size' => 15,
'#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
// 32 characters minus the 'field_' prefix.
'#maxlength' => 26,
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
'#machine_name' => array(
'source' => array('fields', $name, 'label'),
'exists' => '_field_ui_field_name_exists',
'standalone' => TRUE,
'label' => '',
),
'#required' => FALSE,
),
'type' => array(
'#type' => 'select',
'#title' => t('Type of new field'),
'#title_display' => 'invisible',
'#options' => $field_type_options,
'#empty_option' => t('- Select a field type -'),
'#description' => t('Type of data to store.'),
'#attributes' => array('class' => array('field-type-select')),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
),
'widget_type' => array(
'#type' => 'select',
'#title' => t('Widget for new field'),
'#title_display' => 'invisible',
'#options' => $widget_type_options,
'#empty_option' => t('- Select a widget -'),
'#description' => t('Form element to edit the data.'),
'#attributes' => array('class' => array('widget-type-select')),
'#cell_attributes' => array('colspan' => 3),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
),
// Place the 'translatable' property as an explicit value so that contrib
// modules can form_alter() the value for newly created fields.
'translatable' => array(
'#type' => 'value',
'#value' => FALSE,
),
);
}
// Additional row: re-use existing field.
$existing_fields = field_ui_existing_field_options($entity_type, $bundle);
if ($existing_fields && $widget_type_options) {
// Build list of options.
$existing_field_options = array();
foreach ($existing_fields as $field_name => $info) {
$text = t('@type: @field (@label)', array(
'@type' => $info['type_label'],
'@label' => $info['label'],
'@field' => $info['field'],
));
$existing_field_options[$field_name] = truncate_utf8($text, 80, FALSE, TRUE);
}
asort($existing_field_options);
$name = '_add_existing_field';
$table[$name] = array(
'#attributes' => array('class' => array('draggable', 'tabledrag-leaf', 'add-new')),
'#row_type' => 'add_new_field',
'#region_callback' => 'field_ui_field_overview_row_region',
'label' => array(
'#type' => 'textfield',
'#title' => t('Existing field label'),
'#title_display' => 'invisible',
'#size' => 15,
'#description' => t('Label'),
'#attributes' => array('class' => array('label-textfield')),
'#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Re-use existing field') .'</div>',
'#suffix' => '</div>',
),
'weight' => array(
'#type' => 'textfield',
'#default_value' => $max_weight + 2,
'#size' => 3,
'#title_display' => 'invisible',
'#title' => t('Weight for added field'),
'#attributes' => array('class' => array('field-weight')),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
),
'parent_wrapper' => array(
'parent' => array(
'#type' => 'select',
'#title' => t('Parent for existing field'),
'#title_display' => 'invisible',
'#options' => $table['#parent_options'],
'#empty_value' => '',
'#attributes' => array('class' => array('field-parent')),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
'#parents' => array('fields', $name, 'parent'),
),
'hidden_name' => array(
'#type' => 'hidden',
'#default_value' => $name,
'#attributes' => array('class' => array('field-name')),
),
),
'field_name' => array(
'#type' => 'select',
'#title' => t('Existing field to share'),
'#title_display' => 'invisible',
'#options' => $existing_field_options,
'#empty_option' => t('- Select an existing field -'),
'#description' => t('Field to share'),
'#attributes' => array('class' => array('field-select')),
'#cell_attributes' => array('colspan' => 2),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
),
'widget_type' => array(
'#type' => 'select',
'#title' => t('Widget for existing field'),
'#title_display' => 'invisible',
'#options' => $widget_type_options,
'#empty_option' => t('- Select a widget -'),
'#description' => t('Form element to edit the data.'),
'#attributes' => array('class' => array('widget-type-select')),
'#cell_attributes' => array('colspan' => 3),
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
),
);
}
$form['fields'] = $table;
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
$form['#attached']['library'][] = array('field_ui', 'drupal.field_ui');
// Add settings for the update selects behavior.
$js_fields = array();
foreach ($existing_fields as $field_name => $info) {
$js_fields[$field_name] = array('label' => $info['label'], 'type' => $info['type'], 'widget' => $info['widget_type']);
}
$form['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('fields' => $js_fields, 'fieldWidgetTypes' => field_ui_widget_type_options()),
);
// Add tabledrag behavior.
$form['#attached']['drupal_add_tabledrag'][] = array('field-overview', 'order', 'sibling', 'field-weight');
$form['#attached']['drupal_add_tabledrag'][] = array('field-overview', 'match', 'parent', 'field-parent', 'field-parent', 'field-name');
return $form;
}
/**
* Form validation handler for field_ui_field_overview_form().
*
* @see field_ui_field_overview_form_submit()
*/
function field_ui_field_overview_form_validate($form, &$form_state) {
_field_ui_field_overview_form_validate_add_new($form, $form_state);
_field_ui_field_overview_form_validate_add_existing($form, $form_state);
}
/**
* Validates the 'add new field' row of field_ui_field_overview_form().
*
* @see field_ui_field_overview_form_validate()
*/
function _field_ui_field_overview_form_validate_add_new($form, &$form_state) {
$field = $form_state['values']['fields']['_add_new_field'];
// Validate if any information was provided in the 'add new field' row.
if (array_filter(array($field['label'], $field['field_name'], $field['type'], $field['widget_type']))) {
// Missing label.
if (!$field['label']) {
form_set_error('fields][_add_new_field][label', t('Add new field: you need to provide a label.'));
}
// Missing field name.
if (!$field['field_name']) {
form_set_error('fields][_add_new_field][field_name', t('Add new field: you need to provide a field name.'));
}
// Field name validation.
else {
$field_name = $field['field_name'];
// Add the 'field_' prefix.
$field_name = 'field_' . $field_name;
form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state);
}
// Missing field type.
if (!$field['type']) {
form_set_error('fields][_add_new_field][type', t('Add new field: you need to select a field type.'));
}
// Missing widget type.
if (!$field['widget_type']) {
form_set_error('fields][_add_new_field][widget_type', t('Add new field: you need to select a widget.'));
}
// Wrong widget type.
elseif ($field['type']) {
$widget_types = field_ui_widget_type_options($field['type']);
if (!isset($widget_types[$field['widget_type']])) {
form_set_error('fields][_add_new_field][widget_type', t('Add new field: invalid widget.'));
}
}
}
return drupal_build_form('field_ui_field_overview_form', $form_state);
}
/**
......@@ -742,688 +341,36 @@ function _field_ui_field_name_exists($value) {
}
/**
* Validates the 're-use existing field' row of field_ui_field_overview_form().
* Returns the built and processed 'Manage display' form of a bundle.
*
* @see field_ui_field_overview_form_validate()
*/
function _field_ui_field_overview_form_validate_add_existing($form, &$form_state) {
// The form element might be absent if no existing fields can be added to
// this bundle.
if (isset($form_state['values']['fields']['_add_existing_field'])) {
$field = $form_state['values']['fields']['_add_existing_field'];
// Validate if any information was provided in the 're-use existing field' row.
if (array_filter(array($field['label'], $field['field_name'], $field['widget_type']))) {
// Missing label.
if (!$field['label']) {
form_set_error('fields][_add_existing_field][label', t('Re-use existing field: you need to provide a label.'));
}
// Missing existing field name.
if (!$field['field_name']) {
form_set_error('fields][_add_existing_field][field_name', t('Re-use existing field: you need to select a field.'));
}
// Missing widget type.
if (!$field['widget_type']) {
form_set_error('fields][_add_existing_field][widget_type', t('Re-use existing field: you need to select a widget.'));
}
// Wrong widget type.
elseif ($field['field_name'] && ($existing_field = field_info_field($field['field_name']))) {
$widget_types = field_ui_widget_type_options($existing_field['type']);
if (!isset($widget_types[$field['widget_type']])) {
form_set_error('fields][_add_existing_field][widget_type', t('Re-use existing field: invalid widget.'));
}
}
}
}
}
/**
* Form submission handler for field_ui_field_overview_form().
* The resulting form allows fields and pseudo-fields to be re-ordered.
*
* @see field_ui_field_overview_form_validate()
*/
function field_ui_field_overview_form_submit($form, &$form_state) {
$form_values = $form_state['values']['fields'];
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
$bundle_settings = field_bundle_settings($entity_type, $bundle);
// Update field weights.
foreach ($form_values as $key => $values) {
if (in_array($key, $form['#fields'])) {
$instance = field_read_instance($entity_type, $key, $bundle);
$instance['widget']['weight'] = $values['weight'];
field_update_instance($instance);
}
elseif (in_array($key, $form['#extra'])) {
$bundle_settings['extra_fields']['form'][$key]['weight'] = $values['weight'];
}
}
field_bundle_settings($entity_type, $bundle, $bundle_settings);
$destinations = array();
// Create new field.
$field = array();
if (!empty($form_values['_add_new_field']['field_name'])) {
$values = $form_values['_add_new_field'];
$field = array(
'field_name' => $values['field_name'],
'type' => $values['type'],
'translatable' => $values['translatable'],
);
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $values['label'],
'widget' => array(
'type' => $values['widget_type'],
'weight' => $values['weight'],
),
);
// Create the field and instance.
try {
field_create_field($field);
field_create_instance($instance);
$destinations[] = $admin_path . '/fields/' . $field['field_name'] . '/field-settings';
$destinations[] = $admin_path . '/fields/' . $field['field_name'];
// Store new field information for any additional submit handlers.
$form_state['fields_added']['_add_new_field'] = $field['field_name'];
}
catch (Exception $e) {
drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
}
}
// Re-use existing field.
if (!empty($form_values['_add_existing_field']['field_name'])) {
$values = $form_values['_add_existing_field'];
$field = field_info_field($values['field_name']);
if (!empty($field['locked'])) {