Commit 607e9626 authored by Dries's avatar Dries

- Patch #361683by Barry, Yves, Karen, Moshe Weitzman, David Strauss, floriant, chx, David Rothstein: initial field API patch. More work to be done, but ... oh my!
parent d4867346
......@@ -490,8 +490,10 @@ function drupal_verify_profile($profile, $locale) {
*
* @param $module_list
* The modules to install.
* @param $disable_modules_installed_hook
* Normally just testing wants to set this to TRUE.
*/
function drupal_install_modules($module_list = array()) {
function drupal_install_modules($module_list = array(), $disable_modules_installed_hook = FALSE) {
$files = module_rebuild_cache();
$module_list = array_flip(array_values($module_list));
do {
......@@ -511,7 +513,7 @@ function drupal_install_modules($module_list = array()) {
asort($module_list);
$module_list = array_keys($module_list);
$modules_installed = array_filter($module_list, '_drupal_install_module');
if (!empty($modules_installed)) {
if (!$disable_modules_installed_hook && !empty($modules_installed)) {
module_invoke_all('modules_installed', $modules_installed);
}
module_enable($module_list);
......
......@@ -624,7 +624,9 @@ function theme() {
}
if (isset($info['function'])) {
// The theme call is a function.
$output = call_user_func_array($info['function'], $args);
if (drupal_function_exists($info['function'])) {
$output = call_user_func_array($info['function'], $args);
}
}
else {
// The theme call is a template.
......@@ -2004,7 +2006,11 @@ function template_preprocess_node(&$variables) {
}
// Clean up name so there are no underscores.
$variables['template_files'][] = 'node-' . str_replace('_', '-', $node->type);
$variables['template_files'][] = 'node-' . $node->nid;
$variables['template_files'][] = 'node-' . $node->nid;
// Add $FIELD_NAME_rendered variables for fields.
drupal_function_exists('field_attach_preprocess');
$variables += field_attach_preprocess('node', $node);
}
/**
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
<?php
/**
* @file
*
* Default 'implementations' of hook_field_*().
*
* Handles common field housekeeping.
* Those implementations are special, as field.module does not define any field
* types. Those functions take care of default stuff common to all field types.
*
* Storage ops ('load', 'insert', 'update', 'delete', 'delete revisions')
* are not executed field by field, and are thus handled separately.
*/
function field_default_validate($obj_type, $object, $field, $instance, $items, $form) {
// TODO: here we could validate that required fields are filled in (for programmatic save)
}
function field_default_submit($obj_type, &$object, $field, $instance, &$items, $form, &$form_state) {
// Get field values from the submitted form values. Assigning them to $items
// populates $object->field_name when we return from _field_invoke_default().
// TODO D7: Allow the values to be form_altered to another location, like we
// do for the form definition ($form['#fields'][$field_name]['form_path']) ?
if (isset($form_state['values'][$field['field_name']])) {
$items = $form_state['values'][$field['field_name']];
// Remove the 'value' of the 'add more' button.
unset($items[$field['field_name'] . '_add_more']);
// TODO: the above should be moved to validate time (and values saved back
// using form_set_value() ), so that hook_field_validate() works on clean data.
// Not sure we'll want what's below in validate too.
// Reorder items to account for drag-n-drop reordering.
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
$items = _field_sort_items($field, $items);
}
// Filter out empty values.
$items = field_set_empty($field, $items);
// _field_invoke() does not add back items for fields not present in the
// original $object, so add them manually.
$object->{$field['field_name']} = $items;
}
else {
// The form did not include this field, for instance because of access
// rules: make sure any existing value for the field stays unchanged.
unset($object->{$field['field_name']});
}
}
/**
* The 'view' operation constructs the $object in a way that you can use
* drupal_render() to display the formatted output for an individual field.
* i.e. print drupal_render($object->content['field_foo']);
*
* The code supports both single value formatters, which theme an individual
* item value, and multiple value formatters, which theme all values for the
* field in a single theme. The multiple value formatters could be used, for
* instance, to plot field values on a single map or display them in a graph.
* Single value formatters are the default, multiple value formatters can be
* designated as such in formatter_info().
*
* The $object array will look like:
* $object->content['field_foo']['wrapper'] = array(
* '#type' => 'field',
* '#title' => 'label'
* '#field_name' => 'field_name',
* '#object' => $object,
* '#object_type' => $obj_type,
* // Value of the $teaser param of hook_nodeapi('view').
* '#teaser' => $teaser,
* 'items' =>
* 0 => array(
* '#item' => $items[0],
* // Only for 'single-value' formatters
* '#theme' => $theme,
* '#field_name' => 'field_name',
* '#bundle' => $bundle,
* '#formatter' => $formatter_name,
* '#settings' => $formatter_settings,
* '#object' => $object,
* '#object_type' => $obj_type,
* '#delta' => 0,
* ),
* 1 => array(
* '#item' => $items[1],
* // Only for 'single-value' formatters
* '#theme' => $theme,
* '#field_name' => 'field_name',
* '#bundle' => $bundle_name,
* '#formatter' => $formatter_name,
* '#settings' => $formatter_settings,
* '#object' => $object,
* '#object_type' => $obj_type,
* '#delta' => 1,
* ),
* // Only for 'multiple-value' formatters
* '#theme' => $theme,
* '#field_name' => 'field_name',
* '#bundle' => $bundle_name,
* '#formatter' => $formatter_name,
* '#settings' => $formatter_settings,
* ),
* );
*/
function field_default_view($obj_type, $object, $field, $instance, $items, $teaser) {
list($id, $vid, $bundle) = field_attach_extract_ids($obj_type, $object);
$addition = array();
// Entities without build modes should provide a 'full' context.
// NODE_BUILD_NORMAL is 0, and ('whatever' == 0) is TRUE, so we need a ===.
if (!isset($object->build_mode)) {
$context = 'full';
}
elseif ($object->build_mode === NODE_BUILD_NORMAL
|| $object->build_mode == NODE_BUILD_PREVIEW) {
$context = $teaser ? 'teaser' : 'full';
}
else {
$context = $object->build_mode;
}
// If we don't have specific settings for the current build_mode, we use the
// (required) 'full' build_mode.
$display = isset($instance['display'][$context]) ? $instance['display'][$context] : $instance['display']['full'];
// Ensure we have a valid formatter and formatter settings.
$display = _field_get_formatter($display, $field);
if ($display['type'] && $display['type'] !== 'hidden') {
$theme = 'field_formatter_' . $display['type'];
$single = (field_behaviors_formatter('multiple values', $display) == FIELD_BEHAVIOR_DEFAULT);
$label_display = $display['label'];
if (isset($object->build_mode) && $object->build_mode == NODE_BUILD_SEARCH_INDEX) {
$label_display = 'hidden';
}
$info = array(
'#field_name' => $field['field_name'],
'#bundle' => $bundle,
'#object' => $object,
'#object_type' => $obj_type,
);
$element = $info + array(
'#type' => 'field',
'#title' => check_plain(t($instance['label'])),
'#access' => field_access('view', $field),
'#label_display' => $label_display,
'#teaser' => $teaser,
'#single' => $single,
'items' => array(),
);
// Fill-in items.
foreach ($items as $delta => $item) {
$element['items'][$delta] = array(
'#item' => $item,
'#weight' => $delta,
);
}
// Append formatter information either on each item ('single-value' formatter)
// or at the upper 'items' level ('multiple-value' formatter)
$format_info = $info + array(
'#formatter' => $display['type'],
'#settings' => $display['settings'],
'#theme' => $theme,
);
if ($single) {
foreach ($items as $delta => $item) {
$element['items'][$delta] += $format_info;
$element['items'][$delta]['#item']['#delta'] = $delta;
}
}
else {
$element['items'] += $format_info;
}
// The wrapper lets us get the themed output for the whole field
// to populate the $FIELD_NAME_rendered variable for templates,
// and hide it from the $content variable if needed.
// See 'preprocess' op and theme_content_field_wrapper()?
$wrapper = $info + array(
'field' => $element,
'#weight' => $instance['weight'],
'#post_render' => array('field_wrapper_post_render'),
'#context' => $context,
);
$addition = array($field['field_name'] => $wrapper);
}
return $addition;
}
/**
* Hide excluded fields from the $content variable in templates.
*/
function field_wrapper_post_render($content, $element) {
$instance = field_info_instance($element['#field_name'], $element['#bundle']);
if (theme('field_exclude', $content, $instance, $element['#context'])) {
return '';
}
return $content;
}
/**
* 'Theme' function for a field's addition to the combined template output,
* i.e. the node's $content or the user's $user_profile value.
* This allows more flexibility in templates : you can use custom markup
* around a few specific fields, and print the rest normally.
*
* This is a theme function, so it can be overridden in different
* themes to produce different results.
*
* The html for individual fields and groups are available in the
* $FIELD_NAME_rendered and $GROUP_NAME_rendered variables.
*
* @return
* Whether or not the field's content is to be added in this context.
* Uses the 'exclude' value from the field's display settings.
*/
function theme_field_exclude($content, $object, $context) {
if (empty($object['display'])
|| empty($object['display'][$context])
|| empty($object['display'][$context]['exclude'])) {
return FALSE;
}
else {
return TRUE;
}
}
function field_default_preprocess($obj_type, $object, $field, $instance, &$items) {
return array(
$field['field_name'] . '_rendered' => isset($object->content[$field['field_name']]['#children']) ? $object->content[$field['field_name']]['#children'] : '',
);
}
function field_default_prepare_translation($obj_type, $object, $field, $instance, &$items) {
$addition = array();
if (isset($object->translation_source->$field['field_name'])) {
$addition[$field['field_name']] = $object->translation_source->$field['field_name'];
}
return $addition;
}
\ No newline at end of file
<?php
// $Id$
// TODO : merge with field.default.inc ?
/**
* Create a separate form element for each field.
*/
function field_default_form($obj_type, $object, $field, $instance, $items, &$form, &$form_state, $get_delta = NULL) {
// This could be called with no object, as when a UI module creates a
// dummy form to set default values.
if ($object) {
list($id, ,) = field_attach_extract_ids($obj_type, $object);
}
$addition = array();
$field_name = $field['field_name'];
// If the field is not accessible, don't add anything. The field value will
// be left unchanged on update, or considered empty on insert.
// TODO : if/when field_attach_insert() takes care of default values,
// unaccessible fields will automatically get the default value on insert.
if (!field_access('edit', $field)) {
return $addition;
}
// Put field information at the top of the form, so that it can be easily
// retrieved.
// Note : widgets and other form handling code should *always* fetch
// field and instance information from $form['#fields'] rather than from
// field_info_field(). This lets us build forms for 'variants' of a field,
// for instance on admin screens.
$form['#fields'][$field_name] = array(
'field' => $field,
'instance' => $instance,
);
// TODO : why do we need this ?
$form['#cache'] = FALSE;
// Populate widgets with default values if we're creating a new object.
if (empty($items) && empty($id) && !empty($instance['default_value_function'])) {
$items = array();
$function = $instance['default_value_function'];
if (drupal_function_exists($function)) {
$items = $function($obj_type, $object, $field, $instance);
}
}
$form_element = array();
// If field module handles multiple values for this form element,
// and we are displaying an individual element, process the multiple value
// form.
if (!isset($get_delta) && field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
$form_element = field_multiple_value_form($field, $instance, $items, $form, $form_state);
}
// If the widget is handling multiple values (e.g Options),
// or if we are displaying an individual element, just get a single form
// element and make it the $delta value.
else {
$delta = isset($get_delta) ? $get_delta : 0;
$function = $instance['widget']['module'] . '_field_widget';
if (drupal_function_exists($function)) {
if ($element = $function($form, $form_state, $field, $instance, $items, $delta)) {
$defaults = array(
'#required' => $get_delta > 0 ? FALSE : $instance['required'],
'#columns' => array_keys($field['columns']),
'#title' => check_plain(t($instance['label'])),
'#description' => field_filter_xss($instance['description']),
'#delta' => $delta,
'#field_name' => $field['field_name'],
'#bundle' => $instance['bundle'],
);
$element = array_merge($element, $defaults);
// If we're processing a specific delta value for a field where the
// field module handles multiples, set the delta in the result.
// For fields that handle their own processing, we can't make assumptions
// about how the field is structured, just merge in the returned value.
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
$form_element[$delta] = $element;
}
else {
$form_element = $element;
}
}
}
}
if ($form_element) {
$defaults = array(
'#field_name' => $field['field_name'],
'#tree' => TRUE,
'#weight' => $instance['weight'],
);
$addition[$field['field_name']] = array_merge($form_element, $defaults);
$form['#fields'][$field['field_name']]['form_path'] = array($field['field_name']);
}
return $addition;
}
/**
* Special handling to create form elements for multiple values.
*
* Handles generic features for multiple fields:
* - number of widgets
* - AHAH-'add more' button
* - drag-n-drop value reordering
*/
function field_multiple_value_form($field, $instance, $items, &$form, &$form_state) {
$field = field_info_field($instance['field_name']);
$field_name = $field['field_name'];
switch ($field['cardinality']) {
case FIELD_CARDINALITY_UNLIMITED:
$filled_items = field_set_empty($field, $items);
$current_item_count = isset($form_state['field_item_count'][$field_name])
? $form_state['field_item_count'][$field_name]
: count($items);
// We always want at least one empty icon for the user to fill in.
$max = ($current_item_count > count($filled_items))
? $current_item_count - 1
: $current_item_count;
break;
default:
$max = $field['cardinality'] - 1;
break;
}
$title = check_plain(t($instance['label']));
$description = field_filter_xss(t($instance['description']));
$form_element = array(
'#theme' => 'field_multiple_value_form',
'#multiple' => $field['cardinality'],
'#title' => $title,
'#required' => $instance['required'],
'#description' => $description,
);
$function = $instance['widget']['module'] . '_field_widget';
if (drupal_function_exists($function)) {
for ($delta = 0; $delta <= $max; $delta++) {
if ($element = $function($form, $form_state, $field, $instance, $items, $delta)) {
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$defaults = array(
'#title' => $multiple ? '' : $title,
'#description' => $multiple ? '' : $description,
'#required' => $delta == 0 && $instance['required'],
'#weight' => $delta,
'#delta' => $delta,
'#columns' => array_keys($field['columns']),
'#field_name' => $field_name,
'#bundle' => $instance['bundle'],
);
// Add an input field for the delta (drag-n-drop reordering), which will
// be hidden by tabledrag js behavior.
if ($multiple) {
// We name the element '_weight' to avoid clashing with column names
// defined by field modules.
$element['_weight'] = array(
'#type' => 'weight',
'#delta' => $max, // this 'delta' is the 'weight' element's property
'#default_value' => isset($items[$delta]['_weight']) ? $items[$delta]['_weight'] : $delta,
'#weight' => 100,
);
}
$form_element[$delta] = array_merge($element, $defaults);
}
}
// Add AHAH add more button, if not working with a programmed form.
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form['#programmed'])) {
// Make sure the form is cached so ahah can work.
$form['#cache'] = TRUE;
$bundle_name_url_str = str_replace('_', '-', $instance['bundle']);
$field_name_url_str = str_replace('_', '-', $field_name);
$form_element[$field_name . '_add_more'] = array(
'#type' => 'submit',
'#name' => $field_name . '_add_more',
'#value' => t('Add another item'),
'#weight' => $instance['weight'] + $max + 1,
// Submit callback for disabled JavaScript.
'#submit' => array('field_add_more_submit'),
'#ahah' => array(
'path' => 'field/js_add_more/' . $bundle_name_url_str . '/' . $field_name_url_str,
'wrapper' => $field_name_url_str . '-items',
'method' => 'replace',
'effect' => 'fade',
),
// When JS is disabled, the field_add_more_submit handler will find
// the relevant field using these entries.
'#field_name' => $field_name,
'#bundle' => $instance['bundle'],
);
// Add wrappers for the fields and 'more' button.
$form_element['#prefix'] = '<div class="clear-block" id="' . $field_name_url_str . '-add-more-wrapper"><div id="' . $field_name_url_str . '-items">';
$form_element[$field_name . '_add_more']['#prefix'] = '<div class="field-add-more">';
$form_element[$field_name . '_add_more']['#suffix'] = '</div></div></div>';
}
}
return $form_element;
}
/**
* Theme an individual form element.
*
* Combine multiple values into a table with drag-n-drop reordering.
* TODO : convert to a template.
*/
function theme_field_multiple_value_form($element) {
$output = '';
if ($element['#multiple'] > 1 || $element['#multiple'] == FIELD_CARDINALITY_UNLIMITED) {
$table_id = $element['#field_name'] . '_values';
$order_class = $element['#field_name'] . '-delta-order';
$required = !empty($element['#required']) ? '<span class="form-required" title="' . t('This field is required. ') . '">*</span>' : '';
$header = array(
array(
'data' => t('!title: !required', array('!title' => $element['#title'], '!required' => $required)),
'colspan' => 2
),
t('Order'),
);
$rows = array();
// Sort items according to '_weight' (needed when the form comes back after
// preview or failed validation)
$items = array();
foreach (element_children($element) as $key) {
if ($key !== $element['#field_name'] . '_add_more') {
$items[] = &$element[$key];
}
}
usort($items, '_field_sort_items_value_helper');
// Add the items as table rows.
foreach ($items as $key => $item) {
$item['_weight']['#attributes']['class'] = $order_class;
$delta_element = drupal_render($item['_weight']);
$cells = array(
array('data' => '', 'class' => 'field-multiple-drag'),
drupal_render($item),
array('data' => $delta_element, 'class' => 'delta-order'),
);
$rows[] = array(
'data' => $cells,
'class' => 'draggable',
);
}
$output .= theme('table', $header, $rows, array('id' => $table_id, 'class' => 'field-multiple-table'));
$output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
$output .= drupal_render($element[$element['#field_name'] . '_add_more']);
drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
}
else {
foreach (element_children($element) as $key) {
$output .= drupal_render($element[$key]);
}
}
return $output;
}
/**
* Submit handler to add more choices to a field form. This handler is used when
* JavaScript is not available. It makes changes to the form state and the
* entire form is rebuilt during the page reload.
*/
function field_add_more_submit($form, &$form_state) {
// Set the form to rebuild and run submit handlers.
if (isset($form['#builder_function']) && drupal_function_exists($form['#builder_function'])) {
$form['#builder_function']($form, $form_state);
// Make the changes we want to the form state.
$field_name = $form_state['clicked_button']['#field_name'];
if ($form_state['values'][$field_name][$field_name . '_add_more']) {
$form_state['field_item_count'][$field_name] = count($form_state['values'][$field_name]);
}
}
}
/**
* Menu callback for AHAH addition of new empty widgets.
*/
function field_add_more_js($bundle_name, $field_name) {
// Arguments are coming from the url, so we translate back dashes.
$field_name = str_replace('-', '_', $field_name);
$invalid = FALSE;
if (empty($_POST['form_build_id'])) {
// Invalid request.
$invalid = TRUE;
}
// Retrieve the cached form.
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
if (!$form) {
// Invalid form_build_id.
$invalid = TRUE;
}
// Retrieve field information.
$field = $form['#fields'][$field_name]['field'];
$instance = $form['#fields'][$field_name]['instance'];
$form_path = $form['#fields'][$field_name]['form_path'];
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {