field.info.inc 6.73 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Provides info for fields.
 */

/**
9
 * Implements hook_entity_property_info() on top of field module.
10
 *
11 12
 * @see entity_field_info_alter()
 * @see entity_entity_property_info()
13
 */
14 15
function entity_metadata_field_entity_property_info() {
  $info = array();
16 17 18
  // Loop over all field instances and add them as property.
  foreach (field_info_fields() as $field_name => $field) {
    $field += array('bundles' => array());
19 20 21 22
    if ($field_type = field_info_field_types($field['type'])) {
      // Add in our default callback as the first one.
      $field_type += array('property_callbacks' => array());
      array_unshift($field_type['property_callbacks'], 'entity_metadata_field_default_property_callback');
23

24 25 26
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {
          $instance = field_info_instance($entity_type, $field_name, $bundle);
27

28 29 30 31
          if ($instance && empty($instance['deleted'])) {
            foreach ($field_type['property_callbacks'] as $callback) {
              $callback($info, $entity_type, $field, $instance, $field_type);
            }
32 33 34 35 36
          }
        }
      }
    }
  }
37
  return $info;
38 39 40 41
}

/**
 * Callback to add in property info defaults per field instance.
42
 * @see entity_metadata_field_entity_property_info().
43 44 45
 */
function entity_metadata_field_default_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  if (!empty($field_type['property_type'])) {
46
    if ($field['cardinality'] != 1) {
47 48 49
      $field_type['property_type'] = 'list<' . $field_type['property_type'] . '>';
    }
    // Add in instance specific property info, if given and apply defaults.
50
    $name = $field['field_name'];
51
    $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
52 53 54 55
    $instance += array('property info' => array());
    $property = $instance['property info'] + array(
      'label' => $instance['label'],
      'type' => $field_type['property_type'],
56 57 58
      'description' => t('Field %name', array('%name' => $name)),
      'getter callback' => 'entity_metadata_field_property_get',
      'setter callback' => 'entity_metadata_field_property_set',
59
      'access callback' => 'entity_metadata_field_access_callback',
60
      'query callback' => 'entity_metadata_field_query',
61
      'translatable' => !empty($field['translatable']),
62 63
      // Specify that this property stems from a field.
      'field' => TRUE,
64
    );
65 66 67 68
    // For field types of the list module add in the options list callback.
    if (strpos($field['type'], 'list') === 0) {
      $property['options list'] = 'entity_metadata_field_options_list';
    }
69 70 71 72 73
  }
}

/**
 * Additional callback to adapt the property info for text fields. If a text
74 75 76
 * field is processed we make use of a separate data structure so that format
 * filters are available too. For the text value the sanitized, thus processed
 * value is returned by default.
77
 *
78
 * @see entity_metadata_field_entity_property_info()
79
 * @see entity_field_info_alter()
80
 * @see entity_property_text_formatted_info()
81 82
 */
function entity_metadata_field_text_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
83 84 85
  if (!empty($instance['settings']['text_processing']) || $field['type'] == 'text_with_summary') {
    // Define a data structure for dealing with text that is formatted or has
    // a summary.
86
    $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
87

88 89
    $property['getter callback'] = 'entity_metadata_field_verbatim_get';
    $property['setter callback'] = 'entity_metadata_field_verbatim_set';
90
    unset($property['query callback']);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    if (empty($instance['settings']['text_processing'])) {
      $property['property info'] =  entity_property_field_item_textsummary_info();
      // Enable auto-creation of the item, so that it is possible to just set
      // the textual or summary value.
      $property['auto creation'] = 'entity_property_create_array';
    }
    else {
      // For formatted text we use the type name 'text_formatted'.
      $property['type'] = ($field['cardinality'] != 1) ? 'list<text_formatted>' : 'text_formatted';
      $property['property info'] = entity_property_text_formatted_info();
    }
    if ($field['type'] != 'text_with_summary') {
      unset($property['property info']['summary']);
    }
106 107
  }
}
108 109 110

/**
 * Additional callback to adapt the property info for term reference fields.
111
 * @see entity_metadata_field_entity_property_info().
112 113
 */
function entity_metadata_field_term_reference_callback(&$info, $entity_type, $field, $instance, $field_type) {
114
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
115 116 117 118
  if (count($field['settings']['allowed_values']) == 1) {
    $settings = reset($field['settings']['allowed_values']);
    $property['bundle'] = $settings['vocabulary'];
  }
119
  $property['options list'] = 'entity_metadata_field_options_list';
120
  unset($property['query callback']);
121
}
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

/**
 * Additional callback to adapt the property info for file fields.
 * @see entity_metadata_field_entity_property_info().
 */
function entity_metadata_field_file_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  // Define a data structure so it's possible to deal with files and their
  // descriptions.
  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';

  // Auto-create the field $items as soon as a property is set.
  $property['auto creation'] = 'entity_metadata_field_file_create_item';
  $property['validation callback'] = 'entity_metadata_field_file_validate_item';

  $property['property info'] = entity_property_field_item_file_info();

  if (empty($instance['settings']['description_field'])) {
    unset($property['property info']['description']);
  }
  if (empty($field['settings']['display_field'])) {
    unset($property['property info']['display']);
  }
  unset($property['query callback']);
}

/**
 * Additional callback to adapt the property info for image fields.
 * This callback gets invoked after entity_metadata_field_file_callback().
 * @see entity_metadata_field_entity_property_info().
 */
function entity_metadata_field_image_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  // Update the property info with the info for image fields.
  $property['property info'] = entity_property_field_item_image_info();

  if (empty($instance['settings']['alt_field'])) {
    unset($property['property info']['alt']);
  }
  if (empty($field['settings']['title_field'])) {
    unset($property['property info']['title']);
  }
}