field.default.inc 5.44 KB
Newer Older
1
<?php
2
// $Id$
3 4 5

/**
 * @file
6
 * Default 'implementations' of hook_field_*(): common field housekeeping.
7 8 9
 *
 * 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.
10 11
 * They are called through the _field_invoke_default() iterator, generally in
 * the corresponding field_attach_[operation]() function.
12 13
 */

14
function field_default_extract_form_values($obj_type, $object, $field, $instance, $langcode, &$items, $form, &$form_state) {
15
  $field_name = $field['field_name'];
16

17 18
  if (isset($form_state['values'][$field_name][$langcode])) {
    $items = $form_state['values'][$field_name][$langcode];
19
    // Remove the 'value' of the 'add more' button.
20 21 22 23
    unset($items[$field_name . '_add_more']);
  }
}

24
function field_default_submit($obj_type, $object, $field, $instance, $langcode, &$items, $form, &$form_state) {
25 26 27 28 29
  $field_name = $field['field_name'];

  // 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);
30
  }
31 32 33

  // Filter out empty values.
  $items = field_set_empty($field, $items);
34 35
}

36 37 38 39 40 41 42
/**
 * Default field 'insert' operation.
 *
 * Insert default value if no $object->$field_name entry was provided.
 * This can happen with programmatic saves, or on form-based creation where
 * the current user doesn't have 'edit' permission for the field.
 */
43
function field_default_insert($obj_type, $object, $field, $instance, $langcode, &$items) {
44 45
  // _field_invoke() populates $items with an empty array if the $object has no
  // entry for the field, so we check on the $object itself.
46 47 48 49
  // We also check that the current field translation is actually defined before
  // assigning it a default value. This way we ensure that only the intended
  // languages get a default value. Otherwise we could have default values for
  // not yet open languages.
50
  if (empty($object) || !property_exists($object, $field['field_name']) ||
51 52
    (isset($object->{$field['field_name']}[$langcode]) && count($object->{$field['field_name']}[$langcode]) == 0)) {
    $items = field_get_default_value($obj_type, $object, $field, $instance, $langcode);
53 54
  }
}
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/**
 * Invoke hook_field_formatter_prepare_view() on the relavant formatters.
 */
function field_default_prepare_view($obj_type, $objects, $field, $instances, $langcode, &$items, $options, $build_mode) {
  // Group objects, instances and items by formatter module.
  $modules = array();
  foreach ($instances as $id => $instance) {
    $module = $instance['display'][$build_mode]['module'];
    $modules[$module] = $module;
    $grouped_objects[$module][$id] = $objects[$id];
    $grouped_instances[$module][$id] = $instance;
    // hook_field_formatter_prepare_view() alters $items by reference.
    $grouped_items[$module][$id] = &$items[$id];
  }

  foreach ($modules as $module) {
    // Invoke hook_field_formatter_prepare_view().
    $function = $module . '_field_formatter_prepare_view';
    if (function_exists($function)) {
      $function($obj_type, $grouped_objects[$module], $field, $grouped_instances[$module], $langcode, $grouped_items[$module], $build_mode);
    }
  }
}

80
/**
81
 * Default field 'view' operation.
82
 *
83
 * @see field_attach_view()
84
 */
85
function field_default_view($obj_type, $object, $field, $instance, $langcode, $items, $build_mode) {
86
  list($id, $vid, $bundle) = field_extract_ids($obj_type, $object);
87 88

  $addition = array();
89
  $display = $instance['display'][$build_mode];
90

91
  if ($display['type'] !== 'hidden') {
92 93 94 95
    $theme = 'field_formatter_' . $display['type'];
    $single = (field_behaviors_formatter('multiple values', $display) == FIELD_BEHAVIOR_DEFAULT);

    $label_display = $display['label'];
96
    if ($build_mode == 'search_index') {
97 98 99 100 101
      $label_display = 'hidden';
    }

    $info = array(
      '#field_name' => $field['field_name'],
102
      '#object_type' => $obj_type,
103 104 105 106 107 108
      '#bundle' => $bundle,
      '#object' => $object,
    );

    $element = $info + array(
      '#theme' => 'field',
109
      '#weight' => $display['weight'],
110
      '#title' => check_plain(t($instance['label'])),
111
      '#access' => field_access('view', $field, $obj_type, $object),
112
      '#label_display' => $label_display,
113
      '#build_mode' => $build_mode,
114
      '#language' => $langcode,
115
      '#formatter_single' => $single,
116 117 118 119 120 121 122 123 124 125 126 127 128 129
      '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(
130
      '#theme' => $theme,
131 132 133 134 135 136 137 138 139 140 141 142 143 144
      '#formatter' => $display['type'],
      '#settings' => $display['settings'],
    );

    if ($single) {
      foreach ($items as $delta => $item) {
        $element['items'][$delta] += $format_info;
        $element['items'][$delta]['#item']['#delta'] = $delta;
      }
    }
    else {
      $element['items'] += $format_info;
    }

145
    $addition = array($field['field_name'] => $element);
146
  }
147

148 149 150
  return $addition;
}

151
function field_default_prepare_translation($obj_type, $object, $field, $instance, $langcode, &$items) {
152 153 154 155 156
  $addition = array();
  if (isset($object->translation_source->$field['field_name'])) {
    $addition[$field['field_name']] = $object->translation_source->$field['field_name'];
  }
  return $addition;
157
}