diff options
Diffstat (limited to 'modules/field/field.crud.inc')
-rw-r--r-- | modules/field/field.crud.inc | 89 |
1 files changed, 64 insertions, 25 deletions
diff --git a/modules/field/field.crud.inc b/modules/field/field.crud.inc index 904e69d26..4f58cd445 100644 --- a/modules/field/field.crud.inc +++ b/modules/field/field.crud.inc @@ -31,8 +31,7 @@ * field_attach_load() then loads the 'subtitle' and 'photo' fields * because they are both attached to the 'node' bundle 'article'. * - * Field definitions are (currently) represented as an array of key/value - * pairs. The array properties are: + * Field definitions are represented as an array of key/value pairs. * * @param array $field: * - id (integer, read-only) @@ -96,8 +95,7 @@ * A sub-array of key/value pairs of settings. Each storage backend * defines and documents its own settings. * - * Field Instance definitions are (currently) represented as an array of - * key/value pairs. The array properties are: + * Field instance definitions are represented as an array of key/value pairs. * * @param array $instance: * - id (integer, read-only) @@ -154,11 +152,20 @@ * - module (string, read-only) * The name of the module that implements the widget type. * - display (array) - * A sub-array of key/value pairs identifying view modes and the way the - * field values should be displayed in each mode. - * - full (array) - * A sub-array of key/value pairs of the display options to be used - * when the field is being displayed in the "full" view mode. + * A sub-array of key/value pairs identifying the way field values should + * be displayed in each of the entity type's view modes, plus the 'default' + * mode. For each view mode, Field UI lets site administrators define + * whether they want to use a dedicated set of display options or the + * 'default' options to reduce the number of displays to maintain as they + * add new fields. For nodes, on a fresh install, only the 'teaser' view + * mode is configured to use custom display options, all other view modes + * defined use the 'default' options by default. When programmatically + * adding field instances on nodes, it is therefore recommended to at least + * specify display options for 'default' and 'teaser'. + * - default (array) + * A sub-array of key/value pairs describing the display options to be + * used when the field is being displayed in view modes that are not + * configured to use dedicated display options. * - label (string) * Position of the label. 'inline', 'above' and 'hidden' are the * values recognized by the default 'field' theme implementation. @@ -172,7 +179,11 @@ * displayed in this view mode. * - module (string, read-only) * The name of the module which implements the display formatter. - * - teaser + * - some_mode + * A sub-array of key/value pairs describing the display options to be + * used when the field is being displayed in the 'some_mode' view mode. + * Those options will only be actually applied at run time if the view + * mode is not configured to use default settings for this bundle. * - ... * - other_mode * - ... @@ -607,12 +618,16 @@ function field_delete_field($field_name) { * - settings: each omitted setting is given the default value specified in * hook_field_widget_info(). * - display: - * Settings for the 'full' view mode will be added, and each view mode - * will be completed with the following default values: + * Settings for the 'default' view mode will be added if not present, and + * each view mode in the definition will be completed with the following + * default values: * - label: 'above' * - type: the default formatter specified in hook_field_info(). * - settings: each omitted setting is given the default value specified in * hook_field_formatter_info(). + * View modes not present in the definition are left empty, and the field + * will not be displayed in this mode. + * * @return * The $instance array with the id property filled in. * @throw @@ -730,7 +745,6 @@ function _field_write_instance($instance, $update = FALSE) { 'required' => FALSE, 'label' => $instance['field_name'], 'description' => '', - 'weight' => 0, 'deleted' => 0, ); @@ -742,30 +756,55 @@ function _field_write_instance($instance, $update = FALSE) { // TODO: what if no 'default_widget' specified ? 'type' => $field_type['default_widget'], 'settings' => array(), - 'weight' => 0, ); + // If no weight specified, make sure the field sinks at the bottom. + if (!isset($instance['widget']['weight'])) { + $weights = array(); + foreach (field_info_instances($instance['entity_type'], $instance['bundle']) as $existing_instance) { + if ($instance['field_name'] != $existing_instance['field_name']) { + $weights[] = $existing_instance['widget']['weight']; + } + } + foreach (field_extra_fields($instance['entity_type'], $instance['bundle'], 'form') as $extra) { + $weights[] = $extra['weight']; + } + $instance['widget']['weight'] = $weights ? max($weights) + 1 : 0; + } // Check widget module. $widget_type = field_info_widget_types($instance['widget']['type']); $instance['widget']['module'] = $widget_type['module']; $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']); - // Make sure there is at least display info for the 'full' view mode. + // Make sure there are at least display settings for the 'default' view mode, + // and fill in defaults for each view mode specified in the definition. $instance['display'] += array( - 'full' => array(), + 'default' => array(), ); - // Set default display settings for each view mode. foreach ($instance['display'] as $view_mode => $display) { - $instance['display'][$view_mode] += array( + $display += array( 'label' => 'above', - // TODO: what if no 'default_formatter' specified ? - 'type' => $field_type['default_formatter'], + 'type' => isset($field_type['default_formatter']) ? $field_type['default_formatter'] : 'hidden', 'settings' => array(), - 'weight' => 0, ); - $formatter_type = field_info_formatter_types($instance['display'][$view_mode]['type']); - // TODO : 'hidden' will raise PHP warnings. - $instance['display'][$view_mode]['module'] = $formatter_type['module']; - $instance['display'][$view_mode]['settings'] += field_info_formatter_settings($instance['display'][$view_mode]['type']); + if ($display['type'] != 'hidden') { + $formatter_type = field_info_formatter_types($display['type']); + $display['module'] = $formatter_type['module']; + $display['settings'] += field_info_formatter_settings($display['type']); + } + // If no weight specified, make sure the field sinks at the bottom. + if (!isset($display['weight'])) { + $weights = array(); + foreach (field_info_instances($instance['entity_type'], $instance['bundle']) as $existing_instance) { + if ($instance['field_name'] != $existing_instance['field_name']) { + $weights[] = $existing_instance['display'][$view_mode]['weight']; + } + } + foreach (field_extra_fields($instance['entity_type'], $instance['bundle'], 'display') as $extra) { + $weights[] = $extra['display'][$view_mode]['weight']; + } + $display['weight'] = $weights ? max($weights) + 1 : 0; + } + $instance['display'][$view_mode] = $display; } // The serialized 'data' column contains everything from $instance that does |