Projet

Général

Profil

Paste
Télécharger (6,94 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / modules / field.info.inc @ 503b3f7b

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides info for fields.
6
 */
7

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

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

    
28
          if ($instance && empty($instance['deleted'])) {
29
            foreach ($field_type['property_callbacks'] as $callback) {
30
              $callback($info, $entity_type, $field, $instance, $field_type);
31
            }
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
function entity_metadata_field_default_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
45
  if (!empty($field_type['property_type'])) {
46
    if ($field['cardinality'] != 1) {
47
      $field_type['property_type'] = 'list<' . $field_type['property_type'] . '>';
48
    }
49
    // 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
    $instance += array('property info' => array());
53
    $property = $instance['property info'] + array(
54
      'label' => $instance['label'],
55
      'type' => $field_type['property_type'],
56
      'description' => t('Field "@name".', array('@name' => $name)),
57
      'getter callback' => 'entity_metadata_field_property_get',
58
      '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
      // Specify that this property stems from a field.
63
      'field' => TRUE,
64
      'required' => !empty($instance['required']),
65
    );
66
    // For field types of the list module add in the options list callback.
67
    if (strpos($field['type'], 'list') === 0) {
68
      $property['options list'] = 'entity_metadata_field_options_list';
69
    }
70
  }
71
}
72

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

    
89
    $property['getter callback'] = 'entity_metadata_field_verbatim_get';
90
    $property['setter callback'] = 'entity_metadata_field_verbatim_set';
91
    unset($property['query callback']);
92

    
93
    if (empty($instance['settings']['text_processing'])) {
94
      $property['property info'] =  entity_property_field_item_textsummary_info();
95
    }
96
    else {
97
      // For formatted text we use the type name 'text_formatted'.
98
      $property['type'] = ($field['cardinality'] != 1) ? 'list<text_formatted>' : 'text_formatted';
99
      $property['property info'] = entity_property_text_formatted_info();
100
    }
101
    // Enable auto-creation of the item, so that it is possible to just set
102
    // the textual or summary value.
103
    $property['auto creation'] = 'entity_property_create_array';
104

    
105
    if ($field['type'] != 'text_with_summary') {
106
      unset($property['property info']['summary']);
107
    }
108
  }
109
}
110

    
111
/**
112
 * Additional callback to adapt the property info for term reference fields.
113
 * @see entity_metadata_field_entity_property_info().
114
 */
115
function entity_metadata_field_term_reference_callback(&$info, $entity_type, $field, $instance, $field_type) {
116
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
117
  if (count($field['settings']['allowed_values']) == 1) {
118
    $settings = reset($field['settings']['allowed_values']);
119
    $property['bundle'] = $settings['vocabulary'];
120
  }
121
  // Only add the options list callback for controlled vocabularies, thus
122
  // vocabularies not using the autocomplete widget.
123
  if ($instance['widget']['type'] != 'taxonomy_autocomplete') {
124
    $property['options list'] = 'entity_metadata_field_options_list';
125
  }
126
}
127

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

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

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

    
145
  if (empty($instance['settings']['description_field'])) {
146
    unset($property['property info']['description']);
147
  }
148
  if (empty($field['settings']['display_field'])) {
149
    unset($property['property info']['display']);
150
  }
151
  unset($property['query callback']);
152
}
153

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

    
164
  if (empty($instance['settings']['alt_field'])) {
165
    unset($property['property info']['alt']);
166
  }
167
  if (empty($field['settings']['title_field'])) {
168
    unset($property['property info']['title']);
169
  }
170
}