1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Field Info API, providing information about available fields and field types.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Retrieves the FieldInfo object for the current request.
|
10 |
|
|
*
|
11 |
|
|
* @return FieldInfo
|
12 |
|
|
* An instance of the FieldInfo class.
|
13 |
|
|
*/
|
14 |
|
|
function _field_info_field_cache() {
|
15 |
|
|
// Use the advanced drupal_static() pattern, since this is called very often.
|
16 |
|
|
static $drupal_static_fast;
|
17 |
|
|
|
18 |
|
|
if (!isset($drupal_static_fast)) {
|
19 |
|
|
$drupal_static_fast['field_info_field_cache'] = &drupal_static(__FUNCTION__);
|
20 |
|
|
}
|
21 |
|
|
$field_info = &$drupal_static_fast['field_info_field_cache'];
|
22 |
|
|
|
23 |
|
|
if (!isset($field_info)) {
|
24 |
|
|
// @todo The registry should save the need for an explicit include, but not
|
25 |
|
|
// a couple upgrade tests (DisabledNodeTypeTestCase,
|
26 |
|
|
// FilterFormatUpgradePathTestCase...) break in a strange way without it.
|
27 |
|
|
include_once dirname(__FILE__) . '/field.info.class.inc';
|
28 |
|
|
$field_info = new FieldInfo();
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
return $field_info;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* @defgroup field_info Field Info API
|
36 |
|
|
* @{
|
37 |
|
|
* Obtain information about Field API configuration.
|
38 |
|
|
*
|
39 |
|
|
* The Field Info API exposes information about field types, fields,
|
40 |
|
|
* instances, bundles, widget types, display formatters, behaviors,
|
41 |
|
|
* and settings defined by or with the Field API.
|
42 |
|
|
*
|
43 |
|
|
* See @link field Field API @endlink for information about the other parts of
|
44 |
|
|
* the Field API.
|
45 |
|
|
*/
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* Clears the field info cache without clearing the field data cache.
|
49 |
|
|
*
|
50 |
|
|
* This is useful when deleted fields or instances are purged. We
|
51 |
|
|
* need to remove the purged records, but no actual field data items
|
52 |
|
|
* are affected.
|
53 |
|
|
*/
|
54 |
|
|
function field_info_cache_clear() {
|
55 |
|
|
drupal_static_reset('field_view_mode_settings');
|
56 |
|
|
drupal_static_reset('field_available_languages');
|
57 |
|
|
|
58 |
|
|
// @todo: Remove this when field_attach_*_bundle() bundle management
|
59 |
|
|
// functions are moved to the entity API.
|
60 |
|
|
entity_info_cache_clear();
|
61 |
|
|
|
62 |
|
|
_field_info_collate_types(TRUE);
|
63 |
|
|
_field_info_field_cache()->flush();
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* Collates all information on existing fields and instances.
|
68 |
|
|
*
|
69 |
|
|
* Deprecated. This function is kept to ensure backwards compatibility, but has
|
70 |
|
|
* a serious performance impact, and should be absolutely avoided.
|
71 |
|
|
* See http://drupal.org/node/1915646.
|
72 |
|
|
*
|
73 |
|
|
* Use the regular field_info_*() API functions to access the information, or
|
74 |
|
|
* field_info_cache_clear() to clear the cached data.
|
75 |
|
|
*/
|
76 |
|
|
function _field_info_collate_fields($reset = FALSE) {
|
77 |
|
|
if ($reset) {
|
78 |
|
|
_field_info_field_cache()->flush();
|
79 |
|
|
return;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
$cache = _field_info_field_cache();
|
83 |
|
|
|
84 |
|
|
// Collect fields, and build the array of IDs keyed by field_name.
|
85 |
|
|
$fields = $cache->getFields();
|
86 |
|
|
$field_ids = array();
|
87 |
|
|
foreach ($fields as $id => $field) {
|
88 |
|
|
if (!$field['deleted']) {
|
89 |
|
|
$field_ids[$field['field_name']] = $id;
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
// Collect extra fields for all entity types.
|
94 |
|
|
$extra_fields = array();
|
95 |
|
|
foreach (field_info_bundles() as $entity_type => $bundles) {
|
96 |
|
|
foreach ($bundles as $bundle => $info) {
|
97 |
|
|
$extra_fields[$entity_type][$bundle] = $cache->getBundleExtraFields($entity_type, $bundle);
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
return array(
|
102 |
|
|
'fields' => $fields,
|
103 |
|
|
'field_ids' => $field_ids,
|
104 |
|
|
'instances' => $cache->getInstances(),
|
105 |
|
|
'extra_fields' => $extra_fields,
|
106 |
|
|
);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Collates all information on field types, widget types and related structures.
|
111 |
|
|
*
|
112 |
|
|
* @param $reset
|
113 |
|
|
* If TRUE, clear the cache. The information will be rebuilt from the database
|
114 |
|
|
* next time it is needed. Defaults to FALSE.
|
115 |
|
|
*
|
116 |
|
|
* @return
|
117 |
|
|
* If $reset is TRUE, nothing.
|
118 |
|
|
* If $reset is FALSE, an array containing the following elements:
|
119 |
|
|
* - 'field types': Array of hook_field_info() results, keyed by field_type.
|
120 |
|
|
* Each element has the following components: label, description, settings,
|
121 |
|
|
* instance_settings, default_widget, default_formatter, and behaviors
|
122 |
|
|
* from hook_field_info(), as well as module, giving the module that exposes
|
123 |
|
|
* the field type.
|
124 |
|
|
* - 'widget types': Array of hook_field_widget_info() results, keyed by
|
125 |
|
|
* widget_type. Each element has the following components: label, field
|
126 |
|
|
* types, settings, weight, and behaviors from hook_field_widget_info(),
|
127 |
|
|
* as well as module, giving the module that exposes the widget type.
|
128 |
|
|
* - 'formatter types': Array of hook_field_formatter_info() results, keyed by
|
129 |
|
|
* formatter_type. Each element has the following components: label, field
|
130 |
|
|
* types, and behaviors from hook_field_formatter_info(), as well as
|
131 |
|
|
* module, giving the module that exposes the formatter type.
|
132 |
|
|
* - 'storage types': Array of hook_field_storage_info() results, keyed by
|
133 |
|
|
* storage type names. Each element has the following components: label,
|
134 |
|
|
* description, and settings from hook_field_storage_info(), as well as
|
135 |
|
|
* module, giving the module that exposes the storage type.
|
136 |
|
|
* - 'fieldable types': Array of hook_entity_info() results, keyed by
|
137 |
|
|
* entity_type. Each element has the following components: name, id key,
|
138 |
|
|
* revision key, bundle key, cacheable, and bundles from hook_entity_info(),
|
139 |
|
|
* as well as module, giving the module that exposes the entity type.
|
140 |
|
|
*/
|
141 |
|
|
function _field_info_collate_types($reset = FALSE) {
|
142 |
|
|
global $language;
|
143 |
|
|
static $info;
|
144 |
|
|
|
145 |
|
|
// The _info() hooks invoked below include translated strings, so each
|
146 |
|
|
// language is cached separately.
|
147 |
|
|
$langcode = $language->language;
|
148 |
|
|
|
149 |
|
|
if ($reset) {
|
150 |
|
|
$info = NULL;
|
151 |
|
|
// Clear all languages.
|
152 |
|
|
cache_clear_all('field_info_types:', 'cache_field', TRUE);
|
153 |
|
|
return;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
if (!isset($info)) {
|
157 |
|
|
if ($cached = cache_get("field_info_types:$langcode", 'cache_field')) {
|
158 |
|
|
$info = $cached->data;
|
159 |
|
|
}
|
160 |
|
|
else {
|
161 |
|
|
$info = array(
|
162 |
|
|
'field types' => array(),
|
163 |
|
|
'widget types' => array(),
|
164 |
|
|
'formatter types' => array(),
|
165 |
|
|
'storage types' => array(),
|
166 |
|
|
);
|
167 |
|
|
|
168 |
|
|
// Populate field types.
|
169 |
|
|
foreach (module_implements('field_info') as $module) {
|
170 |
|
|
$field_types = (array) module_invoke($module, 'field_info');
|
171 |
|
|
foreach ($field_types as $name => $field_info) {
|
172 |
|
|
// Provide defaults.
|
173 |
|
|
$field_info += array(
|
174 |
|
|
'settings' => array(),
|
175 |
|
|
'instance_settings' => array(),
|
176 |
|
|
);
|
177 |
|
|
$info['field types'][$name] = $field_info;
|
178 |
|
|
$info['field types'][$name]['module'] = $module;
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
drupal_alter('field_info', $info['field types']);
|
182 |
|
|
|
183 |
|
|
// Populate widget types.
|
184 |
|
|
foreach (module_implements('field_widget_info') as $module) {
|
185 |
|
|
$widget_types = (array) module_invoke($module, 'field_widget_info');
|
186 |
|
|
foreach ($widget_types as $name => $widget_info) {
|
187 |
|
|
// Provide defaults.
|
188 |
|
|
$widget_info += array(
|
189 |
|
|
'settings' => array(),
|
190 |
|
|
);
|
191 |
|
|
$info['widget types'][$name] = $widget_info;
|
192 |
|
|
$info['widget types'][$name]['module'] = $module;
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
drupal_alter('field_widget_info', $info['widget types']);
|
196 |
|
|
uasort($info['widget types'], 'drupal_sort_weight');
|
197 |
|
|
|
198 |
|
|
// Populate formatter types.
|
199 |
|
|
foreach (module_implements('field_formatter_info') as $module) {
|
200 |
|
|
$formatter_types = (array) module_invoke($module, 'field_formatter_info');
|
201 |
|
|
foreach ($formatter_types as $name => $formatter_info) {
|
202 |
|
|
// Provide defaults.
|
203 |
|
|
$formatter_info += array(
|
204 |
|
|
'settings' => array(),
|
205 |
|
|
);
|
206 |
|
|
$info['formatter types'][$name] = $formatter_info;
|
207 |
|
|
$info['formatter types'][$name]['module'] = $module;
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
drupal_alter('field_formatter_info', $info['formatter types']);
|
211 |
|
|
|
212 |
|
|
// Populate storage types.
|
213 |
|
|
foreach (module_implements('field_storage_info') as $module) {
|
214 |
|
|
$storage_types = (array) module_invoke($module, 'field_storage_info');
|
215 |
|
|
foreach ($storage_types as $name => $storage_info) {
|
216 |
|
|
// Provide defaults.
|
217 |
|
|
$storage_info += array(
|
218 |
|
|
'settings' => array(),
|
219 |
|
|
);
|
220 |
|
|
$info['storage types'][$name] = $storage_info;
|
221 |
|
|
$info['storage types'][$name]['module'] = $module;
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
drupal_alter('field_storage_info', $info['storage types']);
|
225 |
|
|
|
226 |
|
|
cache_set("field_info_types:$langcode", $info, 'cache_field');
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
return $info;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/**
|
234 |
|
|
* Prepares a field definition for the current run-time context.
|
235 |
|
|
*
|
236 |
|
|
* The functionality has moved to the FieldInfo class. This function is kept as
|
237 |
|
|
* a backwards-compatibility layer. See http://drupal.org/node/1915646.
|
238 |
|
|
*
|
239 |
|
|
* @see FieldInfo::prepareField()
|
240 |
|
|
*/
|
241 |
|
|
function _field_info_prepare_field($field) {
|
242 |
|
|
$cache = _field_info_field_cache();
|
243 |
|
|
return $cache->prepareField($field);
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
/**
|
247 |
|
|
* Prepares an instance definition for the current run-time context.
|
248 |
|
|
*
|
249 |
|
|
* The functionality has moved to the FieldInfo class. This function is kept as
|
250 |
|
|
* a backwards-compatibility layer. See http://drupal.org/node/1915646.
|
251 |
|
|
*
|
252 |
|
|
* @see FieldInfo::prepareInstance()
|
253 |
|
|
*/
|
254 |
|
|
function _field_info_prepare_instance($instance, $field) {
|
255 |
|
|
$cache = _field_info_field_cache();
|
256 |
|
|
return $cache->prepareInstance($instance, $field['type']);
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
/**
|
260 |
|
|
* Adapts display specifications to the current run-time context.
|
261 |
|
|
*
|
262 |
|
|
* The functionality has moved to the FieldInfo class. This function is kept as
|
263 |
|
|
* a backwards-compatibility layer. See http://drupal.org/node/1915646.
|
264 |
|
|
*
|
265 |
|
|
* @see FieldInfo::prepareInstanceDisplay()
|
266 |
|
|
*/
|
267 |
|
|
function _field_info_prepare_instance_display($field, $display) {
|
268 |
|
|
$cache = _field_info_field_cache();
|
269 |
|
|
return $cache->prepareInstanceDisplay($display, $field['type']);
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/**
|
273 |
|
|
* Prepares widget specifications for the current run-time context.
|
274 |
|
|
*
|
275 |
|
|
* The functionality has moved to the FieldInfo class. This function is kept as
|
276 |
|
|
* a backwards-compatibility layer. See http://drupal.org/node/1915646.
|
277 |
|
|
*
|
278 |
|
|
* @see FieldInfo::prepareInstanceWidget()
|
279 |
|
|
*/
|
280 |
|
|
function _field_info_prepare_instance_widget($field, $widget) {
|
281 |
|
|
$cache = _field_info_field_cache();
|
282 |
|
|
return $cache->prepareInstanceWidget($widget, $field['type']);
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/**
|
286 |
|
|
* Prepares 'extra fields' for the current run-time context.
|
287 |
|
|
*
|
288 |
|
|
* The functionality has moved to the FieldInfo class. This function is kept as
|
289 |
|
|
* a backwards-compatibility layer. See http://drupal.org/node/1915646.
|
290 |
|
|
*
|
291 |
|
|
* @see FieldInfo::prepareExtraFields()
|
292 |
|
|
*/
|
293 |
|
|
function _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle) {
|
294 |
|
|
$cache = _field_info_field_cache();
|
295 |
|
|
return $cache->prepareExtraFields($extra_fields, $entity_type, $bundle);
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
/**
|
299 |
|
|
* Determines the behavior of a widget with respect to an operation.
|
300 |
|
|
*
|
301 |
|
|
* @param $op
|
302 |
|
|
* The name of the operation. Currently supported: 'default value',
|
303 |
|
|
* 'multiple values'.
|
304 |
|
|
* @param $instance
|
305 |
|
|
* The field instance array.
|
306 |
|
|
*
|
307 |
|
|
* @return
|
308 |
|
|
* One of these values:
|
309 |
|
|
* - FIELD_BEHAVIOR_NONE: Do nothing for this operation.
|
310 |
|
|
* - FIELD_BEHAVIOR_CUSTOM: Use the widget's callback function.
|
311 |
|
|
* - FIELD_BEHAVIOR_DEFAULT: Use field.module default behavior.
|
312 |
|
|
*/
|
313 |
|
|
function field_behaviors_widget($op, $instance) {
|
314 |
|
|
$info = field_info_widget_types($instance['widget']['type']);
|
315 |
|
|
return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
/**
|
319 |
|
|
* Returns information about field types from hook_field_info().
|
320 |
|
|
*
|
321 |
|
|
* @param $field_type
|
322 |
|
|
* (optional) A field type name. If omitted, all field types will be
|
323 |
|
|
* returned.
|
324 |
|
|
*
|
325 |
|
|
* @return
|
326 |
|
|
* Either a field type description, as provided by hook_field_info(), or an
|
327 |
|
|
* array of all existing field types, keyed by field type name.
|
328 |
|
|
*/
|
329 |
|
|
function field_info_field_types($field_type = NULL) {
|
330 |
|
|
$info = _field_info_collate_types();
|
331 |
|
|
$field_types = $info['field types'];
|
332 |
|
|
if ($field_type) {
|
333 |
|
|
if (isset($field_types[$field_type])) {
|
334 |
|
|
return $field_types[$field_type];
|
335 |
|
|
}
|
336 |
|
|
}
|
337 |
|
|
else {
|
338 |
|
|
return $field_types;
|
339 |
|
|
}
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
/**
|
343 |
|
|
* Returns information about field widgets from hook_field_widget_info().
|
344 |
|
|
*
|
345 |
|
|
* @param $widget_type
|
346 |
|
|
* (optional) A widget type name. If omitted, all widget types will be
|
347 |
|
|
* returned.
|
348 |
|
|
*
|
349 |
|
|
* @return
|
350 |
|
|
* Either a single widget type description, as provided by
|
351 |
|
|
* hook_field_widget_info(), or an array of all existing widget types, keyed
|
352 |
|
|
* by widget type name.
|
353 |
|
|
*/
|
354 |
|
|
function field_info_widget_types($widget_type = NULL) {
|
355 |
|
|
$info = _field_info_collate_types();
|
356 |
|
|
$widget_types = $info['widget types'];
|
357 |
|
|
if ($widget_type) {
|
358 |
|
|
if (isset($widget_types[$widget_type])) {
|
359 |
|
|
return $widget_types[$widget_type];
|
360 |
|
|
}
|
361 |
|
|
}
|
362 |
|
|
else {
|
363 |
|
|
return $widget_types;
|
364 |
|
|
}
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
/**
|
368 |
|
|
* Returns information about field formatters from hook_field_formatter_info().
|
369 |
|
|
*
|
370 |
|
|
* @param $formatter_type
|
371 |
|
|
* (optional) A formatter type name. If omitted, all formatter types will be
|
372 |
|
|
* returned.
|
373 |
|
|
*
|
374 |
|
|
* @return
|
375 |
|
|
* Either a single formatter type description, as provided by
|
376 |
|
|
* hook_field_formatter_info(), or an array of all existing formatter types,
|
377 |
|
|
* keyed by formatter type name.
|
378 |
|
|
*/
|
379 |
|
|
function field_info_formatter_types($formatter_type = NULL) {
|
380 |
|
|
$info = _field_info_collate_types();
|
381 |
|
|
$formatter_types = $info['formatter types'];
|
382 |
|
|
if ($formatter_type) {
|
383 |
|
|
if (isset($formatter_types[$formatter_type])) {
|
384 |
|
|
return $formatter_types[$formatter_type];
|
385 |
|
|
}
|
386 |
|
|
}
|
387 |
|
|
else {
|
388 |
|
|
return $formatter_types;
|
389 |
|
|
}
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
/**
|
393 |
|
|
* Returns information about field storage from hook_field_storage_info().
|
394 |
|
|
*
|
395 |
|
|
* @param $storage_type
|
396 |
|
|
* (optional) A storage type name. If omitted, all storage types will be
|
397 |
|
|
* returned.
|
398 |
|
|
*
|
399 |
|
|
* @return
|
400 |
|
|
* Either a storage type description, as provided by
|
401 |
|
|
* hook_field_storage_info(), or an array of all existing storage types,
|
402 |
|
|
* keyed by storage type name.
|
403 |
|
|
*/
|
404 |
|
|
function field_info_storage_types($storage_type = NULL) {
|
405 |
|
|
$info = _field_info_collate_types();
|
406 |
|
|
$storage_types = $info['storage types'];
|
407 |
|
|
if ($storage_type) {
|
408 |
|
|
if (isset($storage_types[$storage_type])) {
|
409 |
|
|
return $storage_types[$storage_type];
|
410 |
|
|
}
|
411 |
|
|
}
|
412 |
|
|
else {
|
413 |
|
|
return $storage_types;
|
414 |
|
|
}
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
|
|
* Returns information about existing bundles.
|
419 |
|
|
*
|
420 |
|
|
* @param $entity_type
|
421 |
|
|
* The type of entity; e.g. 'node' or 'user'.
|
422 |
|
|
*
|
423 |
|
|
* @return
|
424 |
|
|
* An array of bundles for the $entity_type keyed by bundle name,
|
425 |
|
|
* or, if no $entity_type was provided, the array of all existing bundles,
|
426 |
|
|
* keyed by entity type.
|
427 |
|
|
*/
|
428 |
|
|
function field_info_bundles($entity_type = NULL) {
|
429 |
|
|
$info = entity_get_info();
|
430 |
|
|
|
431 |
|
|
if ($entity_type) {
|
432 |
|
|
return isset($info[$entity_type]['bundles']) ? $info[$entity_type]['bundles'] : array();
|
433 |
|
|
}
|
434 |
|
|
|
435 |
|
|
$bundles = array();
|
436 |
|
|
foreach ($info as $type => $entity_info) {
|
437 |
|
|
$bundles[$type] = $entity_info['bundles'];
|
438 |
|
|
}
|
439 |
|
|
return $bundles;
|
440 |
|
|
}
|
441 |
|
|
|
442 |
|
|
/**
|
443 |
|
|
* Returns a lightweight map of fields across bundles.
|
444 |
|
|
*
|
445 |
|
|
* The function only returns active, non deleted fields.
|
446 |
|
|
*
|
447 |
|
|
* @return
|
448 |
|
|
* An array keyed by field name. Each value is an array with two entries:
|
449 |
|
|
* - type: The field type.
|
450 |
|
|
* - bundles: The bundles in which the field appears, as an array with entity
|
451 |
|
|
* types as keys and the array of bundle names as values.
|
452 |
|
|
* Example:
|
453 |
|
|
* @code
|
454 |
|
|
* array(
|
455 |
|
|
* 'body' => array(
|
456 |
|
|
* 'bundles' => array(
|
457 |
|
|
* 'node' => array('page', 'article'),
|
458 |
|
|
* ),
|
459 |
|
|
* 'type' => 'text_with_summary',
|
460 |
|
|
* ),
|
461 |
|
|
* );
|
462 |
|
|
* @endcode
|
463 |
|
|
*/
|
464 |
|
|
function field_info_field_map() {
|
465 |
|
|
$cache = _field_info_field_cache();
|
466 |
|
|
return $cache->getFieldMap();
|
467 |
|
|
}
|
468 |
|
|
|
469 |
|
|
/**
|
470 |
|
|
* Returns all field definitions.
|
471 |
|
|
*
|
472 |
|
|
* Use of this function should be avoided when possible, since it loads and
|
473 |
|
|
* statically caches a potentially large array of information. Use
|
474 |
|
|
* field_info_field_map() instead.
|
475 |
|
|
*
|
476 |
|
|
* When iterating over the fields present in a given bundle after a call to
|
477 |
|
|
* field_info_instances($entity_type, $bundle), it is recommended to use
|
478 |
|
|
* field_info_field() on each individual field instead.
|
479 |
|
|
*
|
480 |
|
|
* @return
|
481 |
|
|
* An array of field definitions, keyed by field name. Each field has an
|
482 |
|
|
* additional property, 'bundles', which is an array of all the bundles to
|
483 |
|
|
* which this field belongs keyed by entity type.
|
484 |
|
|
*
|
485 |
|
|
* @see field_info_field_map()
|
486 |
|
|
*/
|
487 |
|
|
function field_info_fields() {
|
488 |
|
|
$cache = _field_info_field_cache();
|
489 |
|
|
$info = $cache->getFields();
|
490 |
|
|
|
491 |
|
|
$fields = array();
|
492 |
|
|
foreach ($info as $key => $field) {
|
493 |
|
|
if (!$field['deleted']) {
|
494 |
|
|
$fields[$field['field_name']] = $field;
|
495 |
|
|
}
|
496 |
|
|
}
|
497 |
|
|
|
498 |
|
|
return $fields;
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
/**
|
502 |
|
|
* Returns data about an individual field, given a field name.
|
503 |
|
|
*
|
504 |
|
|
* @param $field_name
|
505 |
|
|
* The name of the field to retrieve. $field_name can only refer to a
|
506 |
|
|
* non-deleted, active field. For deleted fields, use
|
507 |
|
|
* field_info_field_by_id(). To retrieve information about inactive fields,
|
508 |
|
|
* use field_read_fields().
|
509 |
|
|
*
|
510 |
|
|
* @return
|
511 |
|
|
* The field array, as returned by field_read_fields(), with an
|
512 |
|
|
* additional element 'bundles', whose value is an array of all the bundles
|
513 |
|
|
* this field belongs to keyed by entity type. NULL if the field was not
|
514 |
|
|
* found.
|
515 |
|
|
*
|
516 |
|
|
* @see field_info_field_by_id()
|
517 |
|
|
*/
|
518 |
|
|
function field_info_field($field_name) {
|
519 |
|
|
$cache = _field_info_field_cache();
|
520 |
|
|
return $cache->getField($field_name);
|
521 |
|
|
}
|
522 |
|
|
|
523 |
|
|
/**
|
524 |
|
|
* Returns data about an individual field, given a field ID.
|
525 |
|
|
*
|
526 |
|
|
* @param $field_id
|
527 |
|
|
* The id of the field to retrieve. $field_id can refer to a
|
528 |
|
|
* deleted field, but not an inactive one.
|
529 |
|
|
*
|
530 |
|
|
* @return
|
531 |
|
|
* The field array, as returned by field_read_fields(), with an
|
532 |
|
|
* additional element 'bundles', whose value is an array of all the bundles
|
533 |
|
|
* this field belongs to.
|
534 |
|
|
*
|
535 |
|
|
* @see field_info_field()
|
536 |
|
|
*/
|
537 |
|
|
function field_info_field_by_id($field_id) {
|
538 |
|
|
$cache = _field_info_field_cache();
|
539 |
|
|
return $cache->getFieldById($field_id);
|
540 |
|
|
}
|
541 |
|
|
|
542 |
|
|
/**
|
543 |
|
|
* Returns the same data as field_info_field_by_id() for every field.
|
544 |
|
|
*
|
545 |
|
|
* Use of this function should be avoided when possible, since it loads and
|
546 |
|
|
* statically caches a potentially large array of information.
|
547 |
|
|
*
|
548 |
|
|
* When iterating over the fields present in a given bundle after a call to
|
549 |
|
|
* field_info_instances($entity_type, $bundle), it is recommended to use
|
550 |
|
|
* field_info_field() on each individual field instead.
|
551 |
|
|
*
|
552 |
|
|
* @return
|
553 |
|
|
* An array, each key is a field ID and the values are field arrays as
|
554 |
|
|
* returned by field_read_fields(), with an additional element 'bundles',
|
555 |
|
|
* whose value is an array of all the bundle this field belongs to.
|
556 |
|
|
*
|
557 |
|
|
* @see field_info_field()
|
558 |
|
|
* @see field_info_field_by_id()
|
559 |
|
|
*/
|
560 |
|
|
function field_info_field_by_ids() {
|
561 |
|
|
$cache = _field_info_field_cache();
|
562 |
|
|
return $cache->getFields();
|
563 |
|
|
}
|
564 |
|
|
|
565 |
|
|
/**
|
566 |
|
|
* Retrieves information about field instances.
|
567 |
|
|
*
|
568 |
|
|
* Use of this function to retrieve instances across separate bundles (i.e.
|
569 |
|
|
* when the $bundle parameter is NULL) should be avoided when possible, since
|
570 |
|
|
* it loads and statically caches a potentially large array of information. Use
|
571 |
|
|
* field_info_field_map() instead.
|
572 |
|
|
*
|
573 |
|
|
* When retrieving the instances of a specific bundle (i.e. when both
|
574 |
|
|
* $entity_type and $bundle_name are provided), the function also populates a
|
575 |
|
|
* static cache with the corresponding field definitions, allowing fast
|
576 |
|
|
* retrieval of field_info_field() later in the request.
|
577 |
|
|
*
|
578 |
|
|
* @param $entity_type
|
579 |
|
|
* (optional) The entity type for which to return instances.
|
580 |
|
|
* @param $bundle_name
|
581 |
|
|
* (optional) The bundle name for which to return instances. If $entity_type
|
582 |
|
|
* is NULL, the $bundle_name parameter is ignored.
|
583 |
|
|
*
|
584 |
|
|
* @return
|
585 |
|
|
* If $entity_type is not set, return all instances keyed by entity type and
|
586 |
|
|
* bundle name. If $entity_type is set, return all instances for that entity
|
587 |
|
|
* type, keyed by bundle name. If $entity_type and $bundle_name are set, return
|
588 |
|
|
* all instances for that bundle.
|
589 |
|
|
*
|
590 |
|
|
* @see field_info_field_map()
|
591 |
|
|
*/
|
592 |
|
|
function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
|
593 |
|
|
$cache = _field_info_field_cache();
|
594 |
|
|
|
595 |
|
|
if (!isset($entity_type)) {
|
596 |
|
|
return $cache->getInstances();
|
597 |
|
|
}
|
598 |
|
|
if (!isset($bundle_name)) {
|
599 |
|
|
return $cache->getInstances($entity_type);
|
600 |
|
|
}
|
601 |
|
|
|
602 |
|
|
return $cache->getBundleInstances($entity_type, $bundle_name);
|
603 |
|
|
}
|
604 |
|
|
|
605 |
|
|
/**
|
606 |
|
|
* Returns an array of instance data for a specific field and bundle.
|
607 |
|
|
*
|
608 |
|
|
* The function populates a static cache with all fields and instances used in
|
609 |
|
|
* the bundle, allowing fast retrieval of field_info_field() or
|
610 |
|
|
* field_info_instance() later in the request.
|
611 |
|
|
*
|
612 |
|
|
* @param $entity_type
|
613 |
|
|
* The entity type for the instance.
|
614 |
|
|
* @param $field_name
|
615 |
|
|
* The field name for the instance.
|
616 |
|
|
* @param $bundle_name
|
617 |
|
|
* The bundle name for the instance.
|
618 |
|
|
*
|
619 |
|
|
* @return
|
620 |
|
|
* An associative array of instance data for the specific field and bundle;
|
621 |
|
|
* NULL if the instance does not exist.
|
622 |
|
|
*/
|
623 |
|
|
function field_info_instance($entity_type, $field_name, $bundle_name) {
|
624 |
|
|
$cache = _field_info_field_cache();
|
625 |
|
|
$info = $cache->getBundleInstances($entity_type, $bundle_name);
|
626 |
|
|
if (isset($info[$field_name])) {
|
627 |
|
|
return $info[$field_name];
|
628 |
|
|
}
|
629 |
|
|
}
|
630 |
|
|
|
631 |
|
|
/**
|
632 |
|
|
* Returns a list and settings of pseudo-field elements in a given bundle.
|
633 |
|
|
*
|
634 |
|
|
* If $context is 'form', an array with the following structure:
|
635 |
|
|
* @code
|
636 |
|
|
* array(
|
637 |
|
|
* 'name_of_pseudo_field_component' => array(
|
638 |
|
|
* 'label' => The human readable name of the component,
|
639 |
|
|
* 'description' => A short description of the component content,
|
640 |
|
|
* 'weight' => The weight of the component in edit forms,
|
641 |
|
|
* ),
|
642 |
|
|
* 'name_of_other_pseudo_field_component' => array(
|
643 |
|
|
* // ...
|
644 |
|
|
* ),
|
645 |
|
|
* );
|
646 |
|
|
* @endcode
|
647 |
|
|
*
|
648 |
|
|
* If $context is 'display', an array with the following structure:
|
649 |
|
|
* @code
|
650 |
|
|
* array(
|
651 |
|
|
* 'name_of_pseudo_field_component' => array(
|
652 |
|
|
* 'label' => The human readable name of the component,
|
653 |
|
|
* 'description' => A short description of the component content,
|
654 |
|
|
* // One entry per view mode, including the 'default' mode:
|
655 |
|
|
* 'display' => array(
|
656 |
|
|
* 'default' => array(
|
657 |
|
|
* 'weight' => The weight of the component in displayed entities in
|
658 |
|
|
* this view mode,
|
659 |
|
|
* 'visible' => TRUE if the component is visible, FALSE if hidden, in
|
660 |
|
|
* displayed entities in this view mode,
|
661 |
|
|
* ),
|
662 |
|
|
* 'teaser' => array(
|
663 |
|
|
* // ...
|
664 |
|
|
* ),
|
665 |
|
|
* ),
|
666 |
|
|
* ),
|
667 |
|
|
* 'name_of_other_pseudo_field_component' => array(
|
668 |
|
|
* // ...
|
669 |
|
|
* ),
|
670 |
|
|
* );
|
671 |
|
|
* @endcode
|
672 |
|
|
*
|
673 |
|
|
* @param $entity_type
|
674 |
|
|
* The type of entity; e.g. 'node' or 'user'.
|
675 |
|
|
* @param $bundle
|
676 |
|
|
* The bundle name.
|
677 |
|
|
* @param $context
|
678 |
|
|
* The context for which the list of pseudo-fields is requested. Either
|
679 |
|
|
* 'form' or 'display'.
|
680 |
|
|
*
|
681 |
|
|
* @return
|
682 |
|
|
* The array of pseudo-field elements in the bundle.
|
683 |
|
|
*/
|
684 |
|
|
function field_info_extra_fields($entity_type, $bundle, $context) {
|
685 |
|
|
$cache = _field_info_field_cache();
|
686 |
|
|
$info = $cache->getBundleExtraFields($entity_type, $bundle);
|
687 |
|
|
|
688 |
|
|
return isset($info[$context]) ? $info[$context] : array();
|
689 |
|
|
}
|
690 |
|
|
|
691 |
|
|
/**
|
692 |
|
|
* Returns the maximum weight of all the components in an entity.
|
693 |
|
|
*
|
694 |
|
|
* This includes fields, 'extra_fields', and other components added by
|
695 |
|
|
* third-party modules (e.g. field_group).
|
696 |
|
|
*
|
697 |
|
|
* @param $entity_type
|
698 |
|
|
* The type of entity; e.g. 'node' or 'user'.
|
699 |
|
|
* @param $bundle
|
700 |
|
|
* The bundle name.
|
701 |
|
|
* @param $context
|
702 |
|
|
* The context for which the maximum weight is requested. Either 'form', or
|
703 |
|
|
* the name of a view mode.
|
704 |
|
|
* @return
|
705 |
|
|
* The maximum weight of the entity's components, or NULL if no components
|
706 |
|
|
* were found.
|
707 |
|
|
*/
|
708 |
|
|
function field_info_max_weight($entity_type, $bundle, $context) {
|
709 |
|
|
$weights = array();
|
710 |
|
|
|
711 |
|
|
// Collect weights for fields.
|
712 |
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
713 |
|
|
if ($context == 'form') {
|
714 |
|
|
$weights[] = $instance['widget']['weight'];
|
715 |
|
|
}
|
716 |
|
|
elseif (isset($instance['display'][$context]['weight'])) {
|
717 |
|
|
$weights[] = $instance['display'][$context]['weight'];
|
718 |
|
|
}
|
719 |
|
|
}
|
720 |
|
|
// Collect weights for extra fields.
|
721 |
|
|
foreach (field_info_extra_fields($entity_type, $bundle, $context) as $extra) {
|
722 |
|
|
$weights[] = $extra['weight'];
|
723 |
|
|
}
|
724 |
|
|
|
725 |
|
|
// Let other modules feedback about their own additions.
|
726 |
|
|
$weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, $context));
|
727 |
|
|
$max_weight = $weights ? max($weights) : NULL;
|
728 |
|
|
|
729 |
|
|
return $max_weight;
|
730 |
|
|
}
|
731 |
|
|
|
732 |
|
|
/**
|
733 |
|
|
* Returns a field type's default settings.
|
734 |
|
|
*
|
735 |
|
|
* @param $type
|
736 |
|
|
* A field type name.
|
737 |
|
|
*
|
738 |
|
|
* @return
|
739 |
|
|
* The field type's default settings, as provided by hook_field_info(), or an
|
740 |
|
|
* empty array if type or settings are not defined.
|
741 |
|
|
*/
|
742 |
|
|
function field_info_field_settings($type) {
|
743 |
|
|
$info = field_info_field_types($type);
|
744 |
|
|
return isset($info['settings']) ? $info['settings'] : array();
|
745 |
|
|
}
|
746 |
|
|
|
747 |
|
|
/**
|
748 |
|
|
* Returns a field type's default instance settings.
|
749 |
|
|
*
|
750 |
|
|
* @param $type
|
751 |
|
|
* A field type name.
|
752 |
|
|
*
|
753 |
|
|
* @return
|
754 |
|
|
* The field type's default instance settings, as provided by
|
755 |
|
|
* hook_field_info(), or an empty array if type or settings are not defined.
|
756 |
|
|
*/
|
757 |
|
|
function field_info_instance_settings($type) {
|
758 |
|
|
$info = field_info_field_types($type);
|
759 |
|
|
return isset($info['instance_settings']) ? $info['instance_settings'] : array();
|
760 |
|
|
}
|
761 |
|
|
|
762 |
|
|
/**
|
763 |
|
|
* Returns a field widget's default settings.
|
764 |
|
|
*
|
765 |
|
|
* @param $type
|
766 |
|
|
* A widget type name.
|
767 |
|
|
*
|
768 |
|
|
* @return
|
769 |
|
|
* The widget type's default settings, as provided by
|
770 |
|
|
* hook_field_widget_info(), or an empty array if type or settings are
|
771 |
|
|
* undefined.
|
772 |
|
|
*/
|
773 |
|
|
function field_info_widget_settings($type) {
|
774 |
|
|
$info = field_info_widget_types($type);
|
775 |
|
|
return isset($info['settings']) ? $info['settings'] : array();
|
776 |
|
|
}
|
777 |
|
|
|
778 |
|
|
/**
|
779 |
|
|
* Returns a field formatter's default settings.
|
780 |
|
|
*
|
781 |
|
|
* @param $type
|
782 |
|
|
* A field formatter type name.
|
783 |
|
|
*
|
784 |
|
|
* @return
|
785 |
|
|
* The formatter type's default settings, as provided by
|
786 |
|
|
* hook_field_formatter_info(), or an empty array if type or settings are
|
787 |
|
|
* undefined.
|
788 |
|
|
*/
|
789 |
|
|
function field_info_formatter_settings($type) {
|
790 |
|
|
$info = field_info_formatter_types($type);
|
791 |
|
|
return isset($info['settings']) ? $info['settings'] : array();
|
792 |
|
|
}
|
793 |
|
|
|
794 |
|
|
/**
|
795 |
|
|
* Returns a field storage type's default settings.
|
796 |
|
|
*
|
797 |
|
|
* @param $type
|
798 |
|
|
* A field storage type name.
|
799 |
|
|
*
|
800 |
|
|
* @return
|
801 |
|
|
* The storage type's default settings, as provided by
|
802 |
|
|
* hook_field_storage_info(), or an empty array if type or settings are
|
803 |
|
|
* undefined.
|
804 |
|
|
*/
|
805 |
|
|
function field_info_storage_settings($type) {
|
806 |
|
|
$info = field_info_storage_types($type);
|
807 |
|
|
return isset($info['settings']) ? $info['settings'] : array();
|
808 |
|
|
}
|
809 |
|
|
|
810 |
|
|
/**
|
811 |
|
|
* @} End of "defgroup field_info".
|
812 |
|
|
*/ |