diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-09-11 00:03:42 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-09-11 00:03:42 +0000 |
commit | 7cc3d92d65cccf7e09943312b7fa28642372d4c9 (patch) | |
tree | 0bc09ca5d76dc48f67935304bb1a9c24c0ea8b73 /modules/field_ui/field_ui.admin.inc | |
parent | 3b21e82a3770c61ad64062204c33911c42e19d7e (diff) | |
download | brdo-7cc3d92d65cccf7e09943312b7fa28642372d4c9.tar.gz brdo-7cc3d92d65cccf7e09943312b7fa28642372d4c9.tar.bz2 |
#616240 follow-up by yched, dereine, marvil07: Make Field UI screens extensible from contrib - part II.
Diffstat (limited to 'modules/field_ui/field_ui.admin.inc')
-rw-r--r-- | modules/field_ui/field_ui.admin.inc | 660 |
1 files changed, 403 insertions, 257 deletions
diff --git a/modules/field_ui/field_ui.admin.inc b/modules/field_ui/field_ui.admin.inc index 85bcce0d8..cf3e8c3e9 100644 --- a/modules/field_ui/field_ui.admin.inc +++ b/modules/field_ui/field_ui.admin.inc @@ -80,18 +80,53 @@ function _field_ui_reduce_order($array, $a) { } /** - * Theme preprocess function for theme_field_ui_table(). + * Returns the region to which a row in the 'Manage fields' screen belongs. * - * @see theme_field_ui_table() + * This function is used as a #row_callback in field_ui_field_overview_form(), + * and is called during field_ui_table_pre_render(). */ -function template_preprocess_field_ui_table(&$variables) { - $elements = &$variables['elements']; +function field_ui_field_overview_row_region($row) { + switch ($row['#row_type']) { + case 'field': + case 'extra_field': + return 'main'; + 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'); + } +} + +/** + * Returns the region to which a row in the 'Manage display' screen belongs. + * + * This function is used as a #row_callback in field_ui_field_overview_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'); + } +} + +/** + * Pre-render callback for field_ui_table elements. + */ +function field_ui_table_pre_render($elements) { + $js_settings = array(); - // Build the tree structure from the weight and parenting data contained in - // the flat form structure, to determine row order and indentation. + // 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']; $tree = array('' => array('name' => '', 'children' => array())); + $trees = array_fill_keys(array_keys($regions), $tree); + $parents = array(); $list = drupal_map_assoc(element_children($elements)); + // Iterate on rows until we can build a known tree path for all of them. while ($list) { foreach ($list as $name) { @@ -99,12 +134,16 @@ function template_preprocess_field_ui_table(&$variables) { $parent = $row['parent_wrapper']['parent']['#value']; // Proceed if parent is known. if (empty($parent) || isset($parents[$parent])) { - // Remove from the next iteration. + // Grab parent, and remove the row from the next iteration. $parents[$name] = $parent ? array_merge($parents[$parent], array($parent)) : array(); unset($list[$name]); + // Determine the region for the row. + $function = $row['#region_callback']; + $region_name = $function($row); + // Add the element in the tree. - $target = &$tree['']; + $target = &$trees[$region_name]['']; foreach ($parents[$name] as $key) { $target = &$target['children'][$key]; } @@ -115,12 +154,35 @@ function template_preprocess_field_ui_table(&$variables) { $cell = current(element_children($row)); $row[$cell]['#prefix'] = theme('indentation', array('size' => $depth)) . (isset($row[$cell]['#prefix']) ? $row[$cell]['#prefix'] : ''); } + + // Add row id and associate JS settings. + $id = drupal_html_class($name); + $row['#attributes']['id'] = $id; + $row += array( + '#js_settings' => array(), + ); + $row['#js_settings'] += array( + 'rowHandler' => $row['#row_type'], + 'name' => $name, + 'region' => $region_name, + ); + $js_settings[$id] = $row['#js_settings']; } } } + // Determine rendering order from the tree structure. + foreach ($regions as $region_name => $region) { + $elements['#regions'][$region_name]['rows_order'] = array_reduce($trees[$region_name], '_field_ui_reduce_order'); + } - // Determine rendering order for the tree. - $variables['row_order'] = array_reduce($tree, '_field_ui_reduce_order'); + drupal_add_js(array('fieldUIRowsData' => $js_settings), 'setting'); + // @todo : use #attached instead when http://drupal.org/node/561858 is fixed. +// $elements['#attached']['js'][] = array( +// 'type' => 'setting', +// 'data' => array('fieldRowsData' => $js_settings), +// ); + + return $elements; } /** @@ -136,26 +198,65 @@ function template_preprocess_field_ui_table(&$variables) { function theme_field_ui_table($variables) { $elements = $variables['elements']; $table = array(); + $js_settings = array(); + // Add table headers and attributes. foreach (array('header', 'attributes') as $key) { if (isset($elements["#$key"])) { $table[$key] = $elements["#$key"]; } } - foreach ($variables['row_order'] as $key) { - $element = $elements[$key]; - $row = array('data' => array()); - $row += $element['#attributes']; + // Determine the colspan to use for region rows, by checking the number of + // columns in the headers. + $colums_count = 0; + foreach ($table['header'] as $header) { + $colums_count += (is_array($header) && isset($header['colspan']) ? $header['colspan'] : 1); + } + + // Render rows, region by region. + foreach ($elements['#regions'] as $region_name => $region) { + $region_name_class = drupal_html_class($region_name); + + // Add region rows. + if (isset($region['title'])) { + $table['rows'][] = array( + 'class' => array('region-title', 'region-' . $region_name_class . '-title'), + 'no_striping' => TRUE, + 'data' => array( + array('data' => $region['title'], 'colspan' => $colums_count), + ), + ); + } + if (isset($region['message'])) { + $class = (empty($region['rows_order']) ? 'region-empty' : 'region-populated'); + $table['rows'][] = array( + 'class' => array('region-message', 'region-' . $region_name_class . '-message', $class), + 'no_striping' => TRUE, + 'data' => array( + array('data' => $region['message'], 'colspan' => $colums_count), + ), + ); + } + + // Add form rows, in the order determined at pre-render time. + foreach ($region['rows_order'] as $name) { + $element = $elements[$name]; + + $row = array('data' => array()); + if (isset($element['#attributes'])) { + $row += $element['#attributes']; + } - foreach (element_children($element) as $cell_key) { - $cell = array('data' => drupal_render($element[$cell_key])); - if (isset($element[$cell_key]['#cell_attributes'])) { - $cell += $element[$cell_key]['#cell_attributes']; + foreach (element_children($element) as $cell_key) { + $cell = array('data' => drupal_render($element[$cell_key])); + if (isset($element[$cell_key]['#cell_attributes'])) { + $cell += $element[$cell_key]['#cell_attributes']; + } + $row['data'][] = $cell; } - $row['data'][] = $cell; + $table['rows'][] = $row; } - $table['rows'][] = $row; } return theme('table', $table); @@ -184,9 +285,6 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle $extra_fields = field_info_extra_fields($entity_type, $bundle, 'form'); - // Store each default weight so that we can add the 'add new' rows after them. - $weights = array(); - $form += array( '#entity_type' => $entity_type, '#bundle' => $bundle, @@ -195,7 +293,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle ); $table = array( - '#type' => 'table', + '#type' => 'field_ui_table', '#tree' => TRUE, '#header' => array( t('Label'), @@ -206,35 +304,40 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle t('Widget'), array('data' => t('Operations'), 'colspan' => 2), ), - '#attributes' => array('id' => 'field-overview'), + '#parent_options' => array('' => t('<none>')), + '#regions' => array( + 'main' => array(), + 'add_new' => array('title' => ' '), + ), + '#attributes' => array( + 'class' => array('field-ui-overview'), + 'id' => 'field-overview', + ), ); - $parent_options = array('' => t('<none>')); - // Fields. foreach ($instances as $name => $instance) { $field = field_info_field($instance['field_name']); $admin_field_path = $admin_path . '/fields/' . $instance['field_name']; - $weight = $instance['widget']['weight']; - $weights[] = $weight; $table[$name] = array( - '#parents' => array($name), - '#attributes' => array('class' => array('draggable tabledrag-leaf')), + '#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', - '#default_value' => $weight, + '#default_value' => $instance['widget']['weight'], '#size' => 3, '#attributes' => array('class' => array('field-weight')), ), 'parent_wrapper' => array( 'parent' => array( '#type' => 'select', - '#options' => $parent_options, + '#options' => $table['#parent_options'], '#attributes' => array('class' => array('field-parent')), - '#parents' => array($name, 'parent'), + '#parents' => array('fields', $name, 'parent'), ), 'hidden_name' => array( '#type' => 'hidden', @@ -280,17 +383,16 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle // Non-field elements. foreach ($extra_fields as $name => $extra_field) { - $weight = $extra_field['weight']; - $weights[] = $weight; $table[$name] = array( - '#parents' => array($name), - '#attributes' => array('class' => array('draggable', 'tabledrag-leaf', 'menu-disabled')), + '#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' => $weight, + '#default_value' => $extra_field['weight'], '#size' => 3, '#attributes' => array('class' => array('field-weight')), '#title_display' => 'invisible', @@ -299,9 +401,9 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle 'parent_wrapper' => array( 'parent' => array( '#type' => 'select', - '#options' => $parent_options, + '#options' => $table['#parent_options'], '#attributes' => array('class' => array('field-parent')), - '#parents' => array($name, 'parent'), + '#parents' => array('fields', $name, 'parent'), ), 'hidden_name' => array( '#type' => 'hidden', @@ -326,7 +428,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle } // Additional row: add new field. - $weight = !empty($weights) ? max($weights) + 1 : 0; + $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) { @@ -334,8 +436,9 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle array_unshift($widget_type_options, t('- Select a widget -')); $name = '_add_new_field'; $table[$name] = array( - '#parents' => array($name), '#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', '#size' => 15, @@ -345,7 +448,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle ), 'weight' => array( '#type' => 'textfield', - '#default_value' => $weight, + '#default_value' => $max_weight + 1, '#size' => 3, '#title_display' => 'invisible', '#title' => t('Weight for new field'), @@ -355,10 +458,10 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle 'parent_wrapper' => array( 'parent' => array( '#type' => 'select', - '#options' => $parent_options, + '#options' => $table['#parent_options'], '#attributes' => array('class' => array('field-parent')), '#prefix' => '<div class="add-new-placeholder"> </div>', - '#parents' => array($name, 'parent'), + '#parents' => array('fields', $name, 'parent'), ), 'hidden_name' => array( '#type' => 'hidden', @@ -372,7 +475,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle '#field_prefix' => '<span dir="ltr">field_', '#field_suffix' => '</span>‎', '#attributes' => array('dir'=>'ltr'), - '#size' => 15, + '#size' => 10, '#description' => t('Field name (a-z, 0-9, _)'), '#prefix' => '<div class="add-new-placeholder"> </div>', ), @@ -397,12 +500,12 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle // Additional row: add existing field. $existing_field_options = field_ui_existing_field_options($entity_type, $bundle); if ($existing_field_options && $widget_type_options) { - $weight++; array_unshift($existing_field_options, t('- Select an existing field -')); $name = '_add_existing_field'; $table[$name] = array( - '#parents' => array($name), '#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', '#size' => 15, @@ -413,7 +516,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle ), 'weight' => array( '#type' => 'textfield', - '#default_value' => $weight, + '#default_value' => $max_weight + 2, '#size' => 3, '#title_display' => 'invisible', '#title' => t('Weight for added field'), @@ -423,10 +526,10 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle 'parent_wrapper' => array( 'parent' => array( '#type' => 'select', - '#options' => $parent_options, + '#options' => $table['#parent_options'], '#attributes' => array('class' => array('field-parent')), '#prefix' => '<div class="add-new-placeholder"> </div>', - '#parents' => array($name, 'parent'), + '#parents' => array('fields', $name, 'parent'), ), 'hidden_name' => array( '#type' => 'hidden', @@ -452,14 +555,14 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle ), ); } - - $form['table'] = $table; + $form['fields'] = $table; $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save')); $form['#attached']['css'][] = drupal_get_path('module', 'field_ui') . '/field_ui.css'; $form['#attached']['js'][] = drupal_get_path('module', 'field_ui') . '/field_ui.js'; + // Add settings for the update selects behavior. $js_fields = array(); foreach ($existing_field_options as $field_name => $fields) { @@ -470,8 +573,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle $form['#attached']['js'][] = array( 'type' => 'setting', - 'data' => array('fields' => $js_fields, 'fieldWidgetTypes' => field_ui_widget_type_options(), - ), + 'data' => array('fields' => $js_fields, 'fieldWidgetTypes' => field_ui_widget_type_options()), ); // Add tabledrag behavior. @@ -495,18 +597,18 @@ function field_ui_field_overview_form_validate($form, &$form_state) { * Validate the 'add new field' row. */ function _field_ui_field_overview_form_validate_add_new($form, &$form_state) { - $field = $form_state['values']['_add_new_field']; + $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('_add_new_field][label', t('Add new field: you need to provide a 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('_add_new_field][field_name', t('Add new field: you need to provide a 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 { @@ -515,39 +617,39 @@ function _field_ui_field_overview_form_validate_add_new($form, &$form_state) { // Add the 'field_' prefix. if (substr($field_name, 0, 6) != 'field_') { $field_name = 'field_' . $field_name; - form_set_value($form['table']['_add_new_field']['field_name'], $field_name, $form_state); + form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state); } // Invalid field name. if (!preg_match('!^field_[a-z0-9_]+$!', $field_name)) { - form_set_error('_add_new_field][field_name', t('Add new field: the field name %field_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%field_name' => $field_name))); + form_set_error('fields][_add_new_field][field_name', t('Add new field: the field name %field_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%field_name' => $field_name))); } if (strlen($field_name) > 32) { - form_set_error('_add_new_field][field_name', t("Add new field: the field name %field_name is too long. The name is limited to 32 characters, including the 'field_' prefix.", array('%field_name' => $field_name))); + form_set_error('fields][_add_new_field][field_name', t("Add new field: the field name %field_name is too long. The name is limited to 32 characters, including the 'field_' prefix.", array('%field_name' => $field_name))); } // Field name already exists. We need to check inactive fields as well, so // we can't use field_info_fields(). $fields = field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE)); if ($fields) { - form_set_error('_add_new_field][field_name', t('Add new field: the field name %field_name already exists.', array('%field_name' => $field_name))); + form_set_error('fields][_add_new_field][field_name', t('Add new field: the field name %field_name already exists.', array('%field_name' => $field_name))); } } // Missing field type. if (!$field['type']) { - form_set_error('_add_new_field][type', t('Add new field: you need to select a 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('_add_new_field][widget_type', t('Add new field: you need to select a widget.')); + 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('_add_new_field][widget_type', t('Add new field: invalid widget.')); + form_set_error('fields][_add_new_field][widget_type', t('Add new field: invalid widget.')); } } } @@ -561,30 +663,30 @@ function _field_ui_field_overview_form_validate_add_new($form, &$form_state) { 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']['_add_existing_field'])) { - $field = $form_state['values']['_add_existing_field']; + 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 'add existing field' row. if (array_filter(array($field['label'], $field['field_name'], $field['widget_type']))) { // Missing label. if (!$field['label']) { - form_set_error('_add_existing_field][label', t('Add existing field: you need to provide a label.')); + form_set_error('fields][_add_existing_field][label', t('Add existing field: you need to provide a label.')); } // Missing existing field name. if (!$field['field_name']) { - form_set_error('_add_existing_field][field_name', t('Add existing field: you need to select a field.')); + form_set_error('fields][_add_existing_field][field_name', t('Add existing field: you need to select a field.')); } // Missing widget type. if (!$field['widget_type']) { - form_set_error('_add_existing_field][widget_type', t('Add existing field: you need to select a widget.')); + form_set_error('fields][_add_existing_field][widget_type', t('Add 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('_add_existing_field][widget_type', t('Add existing field: invalid widget.')); + form_set_error('fields][_add_existing_field][widget_type', t('Add existing field: invalid widget.')); } } } @@ -595,7 +697,7 @@ function _field_ui_field_overview_form_validate_add_existing($form, &$form_state * Submit handler for the field overview form. */ function field_ui_field_overview_form_submit($form, &$form_state) { - $form_values = $form_state['values']; + $form_values = $form_state['values']['fields']; $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle); @@ -711,6 +813,10 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund $field_types = field_info_field_types(); $extra_fields = field_info_extra_fields($entity_type, $bundle, 'display'); + $form_state += array( + 'formatter_settings_edit' => NULL, + ); + $form += array( '#entity_type' => $entity_type, '#bundle' => $bundle, @@ -724,14 +830,28 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund return $form; } - $form_state += array( - 'formatter_settings_edit' => NULL, - ); - $table = array( - '#theme' => 'field_ui_display_overview_table', - '#field_rows' => array(), + '#type' => 'field_ui_table', '#tree' => TRUE, + '#header' => array( + t('Field'), + t('Weight'), + t('Parent'), + t('Label'), + array('data' => t('Format'), 'colspan' => 3), + ), + '#regions' => array( + 'visible' => array('message' => t('No field is displayed.')), + 'hidden' => array('title' => t('Hidden'), 'message' => t('No field is hidden.')), + ), + '#parent_options' => array('' => t('<none>')), + '#attributes' => array( + 'class' => array('field-ui-overview'), + 'id' => 'field-display-overview', + ), + // Add AJAX wrapper. + '#prefix' => '<div id="field-display-overview-wrapper">', + '#suffix' => '</div>', ); $field_label_options = array( @@ -744,50 +864,67 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund 'hidden' => t('Hidden'), ); + // Field rows. foreach ($instances as $name => $instance) { + $field = field_info_field($instance['field_name']); $display = $instance['display'][$view_mode]; - $table[$name]['human_name'] = array( - '#markup' => check_plain($instance['label']), - ); - $table[$name]['weight'] = array( - '#type' => 'textfield', - '#default_value' => $display['weight'], - '#size' => 3, - ); - $table[$name]['hidden_name'] = array( - '#type' => 'hidden', - '#default_value' => $name, - ); - $table[$name]['label'] = array( - '#type' => 'select', - '#options' => $field_label_options, - '#default_value' => $display['label'], + $table[$name] = array( + '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')), + '#row_type' => 'field', + '#region_callback' => 'field_ui_display_overview_row_region', + '#js_settings' => array( + 'rowHandler' => 'field', + 'defaultFormatter' => $field_types[$field['type']]['default_formatter'], + ), + 'human_name' => array( + '#markup' => check_plain($instance['label']), + ), + 'weight' => array( + '#type' => 'textfield', + '#default_value' => $display['weight'], + '#size' => 3, + '#attributes' => array('class' => array('field-weight')), + ), + 'parent_wrapper' => array( + 'parent' => array( + '#type' => 'select', + '#options' => $table['#parent_options'], + '#default_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')), + ), + ), + 'label' => array( + '#type' => 'select', + '#options' => $field_label_options, + '#default_value' => $display['label'], + ), ); - $field = field_info_field($instance['field_name']); - // The handling of the 'hidden' region in field_ui.js relies on the - // 'formatter type' select, so it is present in all cases, and hidden with - // CSS when the row is in 'edit settings' mode. $formatter_options = field_ui_formatter_options($field['type']); $formatter_options['hidden'] = t('<Hidden>'); - $table[$name]['type'] = array( - '#type' => 'select', - '#options' => $formatter_options, - '#default_value' => $display['type'], - '#ajax' => array( - 'callback' => 'field_ui_formatter_settings_js', - 'wrapper' => 'field-display-overview-wrapper', - 'effect' => 'fade', + $table[$name]['format'] = array( + 'type' => array( + '#type' => 'select', + '#options' => $formatter_options, + '#default_value' => $display['type'], + '#parents' => array('fields', $name, 'type'), + '#attributes' => array('class' => array('field-formatter-type')), ), - '#field_name' => $name, - '#op' => 'change_type', + 'settings_edit_form' => array(), ); + // Formatter settings. // Check the currently selected formatter, and merge persisted values for // formatter settings. - if (isset($form_state['values']['settings'][$name]['type'])) { - $formatter_type = $form_state['values']['settings'][$name]['type']; + if (isset($form_state['values']['fields'][$name]['type'])) { + $formatter_type = $form_state['values']['fields'][$name]['type']; } else { $formatter_type = $display['type']; @@ -807,9 +944,9 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund // Base button element for the various formatter settings actions. $base_button = array( - '#submit' => array('field_ui_formatter_settings_submit'), + '#submit' => array('field_ui_display_overview_multistep_submit'), '#ajax' => array( - 'callback' => 'field_ui_formatter_settings_js', + 'callback' => 'field_ui_display_overview_multistep_js', 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', ), @@ -820,38 +957,43 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund // We are currently editing this field's formatter settings. Display the // settings form and submit buttons. $table[$name]['settings_edit_form'] = array(); + + $settings_form = array(); $function = $formatter['module'] . '_field_formatter_settings_form'; - $additions = $function($field, $instance, $view_mode, $form, $form_state); - if (is_array($additions)) { - $table[$name]['settings_edit_form'] = array( + if (function_exists($function)) { + $settings_form = $function($field, $instance, $view_mode, $form, $form_state); + } + + if ($settings_form) { + $table[$name]['format']['#cell_attributes'] = array('colspan' => 3); + $table[$name]['format']['settings_edit_form'] = array( '#type' => 'container', '#attributes' => array('class' => array('field-formatter-settings-edit-form')), + '#parents' => array('fields', $name, 'settings_edit_form'), + 'label' => array( + '#markup' => t('Format settings:') . ' <span class="formatter-name">' . $formatter['label'] . '</span>', + ), + 'settings' => $settings_form, + 'actions' => array( + '#type' => 'actions', + 'save_settings' => $base_button + array( + '#type' => 'submit', + '#name' => $name . '_formatter_settings_update', + '#value' => t('Update'), + '#op' => 'update', + ), + 'cancel_settings' => $base_button + array( + '#type' => 'submit', + '#name' => $name . '_formatter_settings_cancel', + '#value' => t('Cancel'), + '#op' => 'cancel', + // Do not check errors for the 'Cancel' button, but make sure we + // get the value of the 'formatter type' select. + '#limit_validation_errors' => array(array('fields', $name, 'type')), + ), + ), ); - $table[$name]['settings_edit_form']['label'] = array( - '#markup' => t('Format settings:') . ' <span class="formatter-name">' . $formatter['label'] . '</span>', - ); - $table[$name]['settings_edit_form']['settings'] = $additions; - $table[$name]['settings_edit_form']['actions'] = array('#type' => 'actions'); - $table[$name]['settings_edit_form']['actions']['save_settings'] = $base_button + array( - '#type' => 'submit', - '#name' => $name . '_formatter_settings_update', - '#value' => t('Update'), - '#op' => 'update', - ); - $table[$name]['settings_edit_form']['actions']['cancel_settings'] = $base_button + array( - '#type' => 'submit', - '#name' => $name . '_formatter_settings_cancel', - '#value' => t('Cancel'), - '#op' => 'cancel', - // Do not check errors for the 'Cancel' button. We still need the - // value of the 'formatter type' select in $form_state['values']. - '#limit_validation_errors' => array(array('settings', $name, 'type')) - ); - $table[$name]['#settings_editing'] = TRUE; - // When formatter is changed, cancel the currently edited settings. The - // select 'formatter type' input is hidden in editing mode, so this only - // happens is the row is dragged into the 'hidden' section. - $table[$name]['type']['#ajax']['trigger_as'] = array('name' => $name . '_formatter_settings_cancel'); + $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing'; } } else { @@ -862,6 +1004,7 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund if ($summary) { $table[$name]['settings_summary'] = array( '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>', + '#cell_attributes' => array('class' => array('field-formatter-summary-cell')), ); $table[$name]['settings_edit'] = $base_button + array( '#type' => 'image_button', @@ -869,47 +1012,65 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund '#src' => 'misc/configure.png', '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')), '#op' => 'edit', - // Do not check errors for the 'Edit' button. We still need the value - // of the 'formatter type' select in $form_state['values']. - '#limit_validation_errors' => array(array('settings', $name, 'type')), + // Do not check errors for the 'Edit' button, but make sure we get + // the value of the 'formatter type' select. + '#limit_validation_errors' => array(array('fields', $name, 'type')), '#prefix' => '<div class="field-formatter-settings-edit-wrapper">', '#suffix' => '</div>', ); } } - $table['#field_rows'][] = $name; - - // Collect default formatters for the JS script. - $field_type_info = field_info_field_types($field['type']); - $default_formatters[$name] = $field_type_info['default_formatter']; } // Non-field elements. foreach ($extra_fields as $name => $extra_field) { $display = $extra_field['display'][$view_mode]; - $table[$name]['human_name'] = array( - '#markup' => check_plain($extra_field['label']), - ); - $table[$name]['weight'] = array( - '#type' => 'textfield', - '#default_value' => $display['weight'], - '#size' => 3, - ); - $table[$name]['hidden_name'] = array( - '#type' => 'hidden', - '#default_value' => $name, - ); - $table[$name]['type'] = array( - '#type' => 'select', - '#options' => $extra_visibility_options, - '#default_value' => $display['visible'] ? 'visible' : 'hidden', + $table[$name] = array( + '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')), + '#row_type' => 'extra_field', + '#region_callback' => 'field_ui_display_overview_row_region', + '#js_settings' => array('rowHandler' => 'field'), + 'human_name' => array( + '#markup' => check_plain($extra_field['label']), + ), + 'weight' => array( + '#type' => 'textfield', + '#default_value' => $display['weight'], + '#size' => 3, + '#attributes' => array('class' => array('field-weight')), + ), + 'parent_wrapper' => array( + 'parent' => array( + '#type' => 'select', + '#options' => $table['#parent_options'], + '#default_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')), + ), + ), + 'empty_cell' => array( + '#markup' => ' ', + ), + 'format' => array( + 'type' => array( + '#type' => 'select', + '#options' => $extra_visibility_options, + '#default_value' => $display['visible'] ? 'visible' : 'hidden', + '#parents' => array('fields', $name, 'type'), + '#attributes' => array('class' => array('field-formatter-type')), + ), + ), + 'settings_summary' => array(), + 'settings_edit' => array(), ); - $table[$name]['settings_summary'] = array(); - $table[$name]['settings_edit'] = array(); - $table['#field_rows'][] = $name; } - $form['settings'] = $table; + $form['fields'] = $table; // Custom display settings. if ($view_mode == 'default') { @@ -940,40 +1101,82 @@ function field_ui_display_overview_form($form, &$form_state, $entity_type, $bund ); } + // In overviews involving nested rows from contributed modules (i.e + // field_group), the 'format type' selects can trigger a series of changes in + // child rows. The #ajax behavior is therefore not attached directly to the + // selects, but triggered by the client-side script through a hidden #ajax + // 'Refresh' button. A hidden 'refresh_rows' input tracks the name of + // affected rows. + $form['refresh_rows'] = array('#type' => 'hidden'); + $form['refresh'] = array( + '#type' => 'submit', + '#value' => t('Refresh'), + '#op' => 'refresh_table', + // Do not check errors, but make sure we get the values of the + // 'refresh_rows' input. + '#limit_validation_errors' => array('refresh_rows'), + '#submit' => array('field_ui_display_overview_multistep_submit'), + '#ajax' => array( + 'callback' => 'field_ui_display_overview_multistep_js', + 'wrapper' => 'field-display-overview-wrapper', + 'effect' => 'fade', + // The button stays hidden, so we hide the AJAX spinner too. Ad-hoc + // spinners will be added manually by the client-side script. + 'progress' => 'none', + ), + ); + $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save')); $form['#attached']['js'][] = drupal_get_path('module', 'field_ui') . '/field_ui.js'; $form['#attached']['css'][] = drupal_get_path('module', 'field_ui') . '/field_ui.css'; - drupal_add_js(array('fieldDefaultFormatters' => $default_formatters), 'setting'); + // Add tabledrag behavior. + drupal_add_tabledrag('field-display-overview', 'order', 'sibling', 'field-weight'); + drupal_add_tabledrag('field-display-overview', 'match', 'parent', 'field-parent', 'field-parent', 'field-name'); +// @todo : use #attached instead when http://drupal.org/node/561858 is fixed. +// $form['#attached']['drupal_add_tabledrag'][] = array('field-display-overview', 'order', 'sibling', 'field-weight'); +// $form['#attached']['drupal_add_tabledrag'][] = array('field-display-overview', 'match', 'parent', 'field-parent', 'field-parent', 'field-name'); return $form; } /** - * Form submit handler for the formatter settings buttons. + * Form submit handler for multistep buttons on the 'Manage display' screen. */ -function field_ui_formatter_settings_submit($form, &$form_state) { +function field_ui_display_overview_multistep_submit($form, &$form_state) { $trigger = $form_state['triggering_element']; - $field_name = $trigger['#field_name']; $op = $trigger['#op']; switch ($op) { case 'edit': // Store the field whose settings are currently being edited. + $field_name = $trigger['#field_name']; $form_state['formatter_settings_edit'] = $field_name; break; case 'update': - // Store the saved settings. - $values = $form_state['values']['settings'][$field_name]['settings_edit_form']['settings']; + // Store the saved settings, and set the field back to 'non edit' mode. + $field_name = $trigger['#field_name']; + $values = $form_state['values']['fields'][$field_name]['settings_edit_form']['settings']; $form_state['formatter_settings'][$field_name] = $values; - // Fall-through to the 'cancel' case. + unset($form_state['formatter_settings_edit']); + break; + case 'cancel': - // Unset the field as being currently edited. - $form_state['formatter_settings_edit'] = NULL; + // Set the field back to 'non edit' mode. + unset($form_state['formatter_settings_edit']); + break; + + case 'refresh_table': + // If the currently edited field is one of the rows to be refreshed, set + // it back to 'non edit' mode. + $updated_rows = explode(' ', $form_state['values']['refresh_rows']); + if (isset($form_state['formatter_settings_edit']) && in_array($form_state['formatter_settings_edit'], $updated_rows)) { + unset($form_state['formatter_settings_edit']); + } break; } @@ -981,79 +1184,41 @@ function field_ui_formatter_settings_submit($form, &$form_state) { } /** - * AJAX handler for the formatter settings buttons. + * AJAX handler for multistep buttons on the 'Manage display' screen. */ -function field_ui_formatter_settings_js($form, &$form_state) { +function field_ui_display_overview_multistep_js($form, &$form_state) { $trigger = $form_state['triggering_element']; - $field_name = $trigger['#field_name']; $op = $trigger['#op']; - // Apply the AJAX effect to updated elements. + // Pick the elements that need ro receive the ajax-new-content effect. switch ($op) { - case 'change_type': - $updated = array('settings_summary'); - break; - case 'edit': - $updated = array('settings_edit_form'); + $updated_rows = array($trigger['#field_name']); + $updated_columns = array('format'); break; case 'update': case 'cancel': - $updated = array('type', 'settings_summary', 'settings_edit'); + $updated_rows = array($trigger['#field_name']); + $updated_columns = array('format', 'settings_summary', 'settings_edit'); break; - } - foreach ($updated as $key) { - $element = &$form['settings'][$field_name][$key]; - $element['#prefix'] = '<div class="ajax-new-content">' . (isset($element['#prefix']) ? $element['#prefix'] : ''); - $element['#suffix'] = (isset($element['#suffix']) ? $element['#suffix'] : '') . '</div>'; - } - return $form['settings']; -} - -/** - * Theme preprocess function for field_ui-display-overview-table.tpl.php. - */ -function template_preprocess_field_ui_display_overview_table(&$vars) { - $elements = &$vars['elements']; - - $rows = array( - 'visible' => array(), - 'hidden' => array(), - ); - - if (!empty($elements['#field_rows'])) { - drupal_add_tabledrag('field-display-overview', 'order', 'sibling', 'field-weight'); - - $order = _field_ui_overview_order($elements, $elements['#field_rows']); - foreach ($order as $key) { - $element = &$elements[$key]; - $visibility = $element['type']['#value'] == 'hidden' ? 'hidden' : 'visible'; - - // Add target classes for the tabledrag behavior. - $element['weight']['#attributes']['class'][] = 'field-weight'; - $element['hidden_name']['#attributes']['class'][] = 'field-name'; - $element['type']['#attributes']['class'][] = 'field-formatter-type'; - $element['type']['#attributes']['class'][] = "field-display-$visibility"; - $element['type']['#attributes']['class'][] = "field-name-$key"; + case 'refresh_table': + $updated_rows = array_values(explode(' ', $form_state['values']['refresh_rows'])); + $updated_columns = array('settings_summary', 'settings_edit'); + break; + } - $row = new stdClass(); - foreach (element_children($element) as $child) { - $row->{$child} = drupal_render($element[$child]); - } - $row->settings_class = (!empty($element['#settings_class']) ? $element['#settings_class'] : ''); - $row->class = 'draggable'; - if (isset($element['#settings_editing'])) { - $row->class .= ' field-formatter-settings-editing'; - } - $row->label_class = 'label-field'; - $row->id = 'row-' . strtr($key, '_', '-'); - $rows[$visibility][] = $row; + foreach ($updated_rows as $name) { + foreach ($updated_columns as $key) { + $element = &$form['fields'][$name][$key]; + $element['#prefix'] = '<div class="ajax-new-content">' . (isset($element['#prefix']) ? $element['#prefix'] : ''); + $element['#suffix'] = (isset($element['#suffix']) ? $element['#suffix'] : '') . '</div>'; } } - $vars['rows'] = $rows; + // Return the whole table. + return $form['fields']; } /** @@ -1068,7 +1233,7 @@ function field_ui_display_overview_form_submit($form, &$form_state) { // Save data for 'regular' fields. foreach ($form['#fields'] as $field_name) { $instance = field_info_instance($entity_type, $field_name, $bundle); - $values = $form_values['settings'][$field_name]; + $values = $form_values['fields'][$field_name]; // Get formatter settings. They lie either directly in submitted form // values (if the whole form was submitted while some formatter // settings were being edited), or have been persisted in @@ -1100,8 +1265,8 @@ function field_ui_display_overview_form_submit($form, &$form_state) { // Save data for 'extra' fields. foreach ($form['#extra'] as $name) { $bundle_settings['extra_fields']['display'][$name][$view_mode] = array( - 'weight' => $form_values['settings'][$name]['weight'], - 'visible' => $form_values['settings'][$name]['type'] == 'visible', + 'weight' => $form_values['fields'][$name]['weight'], + 'visible' => $form_values['fields'][$name]['type'] == 'visible', ); } @@ -1740,25 +1905,6 @@ function field_ui_next_destination($entity_type, $bundle) { } /** - * Helper function to order fields when theming overview forms. - * @todo Remove when 'Manage display' screen is done. - */ -function _field_ui_overview_order(&$form, $field_rows) { - // Put weight and parenting values into a $dummy render structure and let - // drupal_render() figure out the corresponding row order. - $dummy = array(); - // Field rows: account for weight. - foreach ($field_rows as $name) { - $dummy[$name] = array( - '#markup' => $name . ' ', - '#type' => 'markup', - '#weight' => $form[$name]['weight']['#value'], - ); - } - return $dummy ? explode(' ', trim(drupal_render($dummy))) : array(); -} - -/** * Helper form element validator: integer. */ function _element_validate_integer($element, &$form_state) { |