Projet

Général

Profil

Paste
Télécharger (7,16 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / modules / field.info.inc @ 7d7b5830

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
      // Since the label will be exposed via hook_token_info() and it is not
55
      // clearly defined if that should be sanitized already we prevent XSS
56
      // right here (field labels are user provided text).
57
      'label' => filter_xss_admin($instance['label']),
58
      'type' => $field_type['property_type'],
59
      'description' => t('Field "@name".', array('@name' => $name)),
60
      'getter callback' => 'entity_metadata_field_property_get',
61
      'setter callback' => 'entity_metadata_field_property_set',
62
      'access callback' => 'entity_metadata_field_access_callback',
63
      'query callback' => 'entity_metadata_field_query',
64
      'translatable' => !empty($field['translatable']),
65
      // Specify that this property stems from a field.
66
      'field' => TRUE,
67
      'required' => !empty($instance['required']),
68
    );
69
    // For field types of the list module add in the options list callback.
70
    if (strpos($field['type'], 'list') === 0) {
71
      $property['options list'] = 'entity_metadata_field_options_list';
72
    }
73
  }
74
}
75

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

    
92
    $property['getter callback'] = 'entity_metadata_field_verbatim_get';
93
    $property['setter callback'] = 'entity_metadata_field_verbatim_set';
94
    unset($property['query callback']);
95

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

    
108
    if ($field['type'] != 'text_with_summary') {
109
      unset($property['property info']['summary']);
110
    }
111
  }
112
}
113

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

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

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

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

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

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

    
167
  if (empty($instance['settings']['alt_field'])) {
168
    unset($property['property info']['alt']);
169
  }
170
  if (empty($instance['settings']['title_field'])) {
171
    unset($property['property info']['title']);
172
  }
173
}