1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Attach custom data fields to Drupal entities.
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Base class for all exceptions thrown by Field API functions.
|
9
|
*
|
10
|
* This class has no functionality of its own other than allowing all
|
11
|
* Field API exceptions to be caught by a single catch block.
|
12
|
*/
|
13
|
class FieldException extends Exception {}
|
14
|
|
15
|
/*
|
16
|
* Load all public Field API functions. Drupal currently has no
|
17
|
* mechanism for auto-loading core APIs, so we have to load them on
|
18
|
* every page request.
|
19
|
*/
|
20
|
require_once DRUPAL_ROOT . '/modules/field/field.crud.inc';
|
21
|
require_once DRUPAL_ROOT . '/modules/field/field.default.inc';
|
22
|
require_once DRUPAL_ROOT . '/modules/field/field.info.inc';
|
23
|
require_once DRUPAL_ROOT . '/modules/field/field.multilingual.inc';
|
24
|
require_once DRUPAL_ROOT . '/modules/field/field.attach.inc';
|
25
|
require_once DRUPAL_ROOT . '/modules/field/field.form.inc';
|
26
|
|
27
|
/**
|
28
|
* @defgroup field Field API
|
29
|
* @{
|
30
|
* Attach custom data fields to Drupal entities.
|
31
|
*
|
32
|
* The Field API allows custom data fields to be attached to Drupal
|
33
|
* entities and takes care of storing, loading, editing, and rendering
|
34
|
* field data. Any entity type (node, user, etc.) can use the Field
|
35
|
* API to make itself "fieldable" and thus allow fields to be attached
|
36
|
* to it. Other modules can provide a user interface for managing custom
|
37
|
* fields via a web browser as well as a wide and flexible variety of
|
38
|
* data type, form element, and display format capabilities.
|
39
|
*
|
40
|
* The Field API defines two primary data structures, Field and
|
41
|
* Instance, and the concept of a Bundle. A Field defines a
|
42
|
* particular type of data that can be attached to entities. A Field
|
43
|
* Instance is a Field attached to a single Bundle. A Bundle is a set
|
44
|
* of fields that are treated as a group by the Field Attach API and
|
45
|
* is related to a single fieldable entity type.
|
46
|
*
|
47
|
* For example, suppose a site administrator wants Article nodes to
|
48
|
* have a subtitle and photo. Using the Field API or Field UI module,
|
49
|
* the administrator creates a field named 'subtitle' of type 'text'
|
50
|
* and a field named 'photo' of type 'image'. The administrator
|
51
|
* (again, via a UI) creates two Field Instances, one attaching the
|
52
|
* field 'subtitle' to the 'node' bundle 'article' and one attaching
|
53
|
* the field 'photo' to the 'node' bundle 'article'. When the node
|
54
|
* system uses the Field Attach API to load all fields for an Article
|
55
|
* node, it passes the node's entity type (which is 'node') and
|
56
|
* content type (which is 'article') as the node's bundle.
|
57
|
* field_attach_load() then loads the 'subtitle' and 'photo' fields
|
58
|
* because they are both attached to the 'node' bundle 'article'.
|
59
|
*
|
60
|
* Field definitions are represented as an array of key/value pairs.
|
61
|
*
|
62
|
* array $field:
|
63
|
* - id (integer, read-only): The primary identifier of the field. It is
|
64
|
* assigned automatically by field_create_field().
|
65
|
* - field_name (string): The name of the field. Each field name is unique
|
66
|
* within Field API. When a field is attached to an entity, the field's data
|
67
|
* is stored in $entity->$field_name. Maximum length is 32 characters.
|
68
|
* - type (string): The type of the field, such as 'text' or 'image'. Field
|
69
|
* types are defined by modules that implement hook_field_info().
|
70
|
* - entity_types (array): The array of entity types that can hold instances
|
71
|
* of this field. If empty or not specified, the field can have instances
|
72
|
* in any entity type.
|
73
|
* - cardinality (integer): The number of values the field can hold. Legal
|
74
|
* values are any positive integer or FIELD_CARDINALITY_UNLIMITED.
|
75
|
* - translatable (integer): Whether the field is translatable.
|
76
|
* - locked (integer): Whether or not the field is available for editing. If
|
77
|
* TRUE, users can't change field settings or create new instances of the
|
78
|
* field in the UI. Defaults to FALSE.
|
79
|
* - module (string, read-only): The name of the module that implements the
|
80
|
* field type.
|
81
|
* - active (integer, read-only): TRUE if the module that implements the field
|
82
|
* type is currently enabled, FALSE otherwise.
|
83
|
* - deleted (integer, read-only): TRUE if this field has been deleted, FALSE
|
84
|
* otherwise. Deleted fields are ignored by the Field Attach API. This
|
85
|
* property exists because fields can be marked for deletion but only
|
86
|
* actually destroyed by a separate garbage-collection process.
|
87
|
* - columns (array, read-only): An array of the Field API columns used to
|
88
|
* store each value of this field. The column list may depend on field
|
89
|
* settings; it is not constant per field type. Field API column
|
90
|
* specifications are exactly like Schema API column specifications but,
|
91
|
* depending on the field storage module in use, the name of the column may
|
92
|
* not represent an actual column in an SQL database.
|
93
|
* - indexes (array): An array of indexes on data columns, using the same
|
94
|
* definition format as Schema API index specifications. Only columns that
|
95
|
* appear in the 'columns' setting are allowed. Note that field types can
|
96
|
* specify default indexes, which can be modified or added to when
|
97
|
* creating a field.
|
98
|
* - foreign keys: (optional) An associative array of relations, using the same
|
99
|
* structure as the 'foreign keys' definition of hook_schema(). Note,
|
100
|
* however, that the field data is not necessarily stored in SQL. Also, the
|
101
|
* possible usage is limited, as you cannot specify another field as
|
102
|
* related, only existing SQL tables, such as filter formats.
|
103
|
* - settings (array): A sub-array of key/value pairs of field-type-specific
|
104
|
* settings. Each field type module defines and documents its own field
|
105
|
* settings.
|
106
|
* - storage (array): A sub-array of key/value pairs identifying the storage
|
107
|
* backend to use for the for the field:
|
108
|
* - type (string): The storage backend used by the field. Storage backends
|
109
|
* are defined by modules that implement hook_field_storage_info().
|
110
|
* - module (string, read-only): The name of the module that implements the
|
111
|
* storage backend.
|
112
|
* - active (integer, read-only): TRUE if the module that implements the
|
113
|
* storage backend is currently enabled, FALSE otherwise.
|
114
|
* - settings (array): A sub-array of key/value pairs of settings. Each
|
115
|
* storage backend defines and documents its own settings.
|
116
|
*
|
117
|
* Field instance definitions are represented as an array of key/value pairs.
|
118
|
*
|
119
|
* array $instance:
|
120
|
* - id (integer, read-only): The primary identifier of this field instance.
|
121
|
* It is assigned automatically by field_create_instance().
|
122
|
* - field_id (integer, read-only): The foreign key of the field attached to
|
123
|
* the bundle by this instance. It is populated automatically by
|
124
|
* field_create_instance().
|
125
|
* - field_name (string): The name of the field attached to the bundle by this
|
126
|
* instance.
|
127
|
* - entity_type (string): The name of the entity type the instance is attached
|
128
|
* to.
|
129
|
* - bundle (string): The name of the bundle that the field is attached to.
|
130
|
* - label (string): A human-readable label for the field when used with this
|
131
|
* bundle. For example, the label will be the title of Form API elements
|
132
|
* for this instance.
|
133
|
* - description (string): A human-readable description for the field when
|
134
|
* used with this bundle. For example, the description will be the help
|
135
|
* text of Form API elements for this instance.
|
136
|
* - required (integer): TRUE if a value for this field is required when used
|
137
|
* with this bundle, FALSE otherwise. Currently, required-ness is only
|
138
|
* enforced during Form API operations, not by field_attach_load(),
|
139
|
* field_attach_insert(), or field_attach_update().
|
140
|
* - default_value_function (string): The name of the function, if any, that
|
141
|
* will provide a default value.
|
142
|
* - default_value (array): If default_value_function is not set, then fixed
|
143
|
* values can be provided.
|
144
|
* - deleted (integer, read-only): TRUE if this instance has been deleted,
|
145
|
* FALSE otherwise. Deleted instances are ignored by the Field Attach API.
|
146
|
* This property exists because instances can be marked for deletion but
|
147
|
* only actually destroyed by a separate garbage-collection process.
|
148
|
* - settings (array): A sub-array of key/value pairs of field-type-specific
|
149
|
* instance settings. Each field type module defines and documents its own
|
150
|
* instance settings.
|
151
|
* - widget (array): A sub-array of key/value pairs identifying the Form API
|
152
|
* input widget for the field when used by this bundle:
|
153
|
* - type (string): The type of the widget, such as text_textfield. Widget
|
154
|
* types are defined by modules that implement hook_field_widget_info().
|
155
|
* - settings (array): A sub-array of key/value pairs of
|
156
|
* widget-type-specific settings. Each field widget type module defines
|
157
|
* and documents its own widget settings.
|
158
|
* - weight (float): The weight of the widget relative to the other elements
|
159
|
* in entity edit forms.
|
160
|
* - module (string, read-only): The name of the module that implements the
|
161
|
* widget type.
|
162
|
* - display (array): A sub-array of key/value pairs identifying the way field
|
163
|
* values should be displayed in each of the entity type's view modes, plus
|
164
|
* the 'default' mode. For each view mode, Field UI lets site administrators
|
165
|
* define whether they want to use a dedicated set of display options or the
|
166
|
* 'default' options to reduce the number of displays to maintain as they
|
167
|
* add new fields. For nodes, on a fresh install, only the 'teaser' view
|
168
|
* mode is configured to use custom display options, all other view modes
|
169
|
* defined use the 'default' options by default. When programmatically
|
170
|
* adding field instances on nodes, it is therefore recommended to at least
|
171
|
* specify display options for 'default' and 'teaser':
|
172
|
* - default (array): A sub-array of key/value pairs describing the display
|
173
|
* options to be used when the field is being displayed in view modes
|
174
|
* that are not configured to use dedicated display options:
|
175
|
* - label (string): Position of the label. 'inline', 'above' and
|
176
|
* 'hidden' are the values recognized by the default 'field' theme
|
177
|
* implementation.
|
178
|
* - type (string): The type of the display formatter, or 'hidden' for
|
179
|
* no display.
|
180
|
* - settings (array): A sub-array of key/value pairs of display
|
181
|
* options specific to the formatter.
|
182
|
* - weight (float): The weight of the field relative to the other entity
|
183
|
* components displayed in this view mode.
|
184
|
* - module (string, read-only): The name of the module which implements
|
185
|
* the display formatter.
|
186
|
* - some_mode: A sub-array of key/value pairs describing the display
|
187
|
* options to be used when the field is being displayed in the 'some_mode'
|
188
|
* view mode. Those options will only be actually applied at run time if
|
189
|
* the view mode is not configured to use default settings for this bundle:
|
190
|
* - ...
|
191
|
* - other_mode:
|
192
|
* - ...
|
193
|
*
|
194
|
* The (default) render arrays produced for field instances are documented at
|
195
|
* field_attach_view().
|
196
|
*
|
197
|
* Bundles are represented by two strings, an entity type and a bundle name.
|
198
|
*
|
199
|
* - @link field_types Field Types API @endlink. Defines field types,
|
200
|
* widget types, and display formatters. Field modules use this API
|
201
|
* to provide field types like Text and Node Reference along with the
|
202
|
* associated form elements and display formatters.
|
203
|
*
|
204
|
* - @link field_crud Field CRUD API @endlink. Create, updates, and
|
205
|
* deletes fields, bundles (a.k.a. "content types"), and instances.
|
206
|
* Modules use this API, often in hook_install(), to create
|
207
|
* custom data structures.
|
208
|
*
|
209
|
* - @link field_attach Field Attach API @endlink. Connects entity
|
210
|
* types to the Field API. Field Attach API functions load, store,
|
211
|
* generate Form API structures, display, and perform a variety of
|
212
|
* other functions for field data connected to individual entities.
|
213
|
* Fieldable entity types like node and user use this API to make
|
214
|
* themselves fieldable.
|
215
|
*
|
216
|
* - @link field_info Field Info API @endlink. Exposes information
|
217
|
* about all fields, instances, widgets, and related information
|
218
|
* defined by or with the Field API.
|
219
|
*
|
220
|
* - @link field_storage Field Storage API @endlink. Provides a
|
221
|
* pluggable back-end storage system for actual field data. The
|
222
|
* default implementation, field_sql_storage.module, stores field data
|
223
|
* in the local SQL database.
|
224
|
*
|
225
|
* - @link field_purge Field API bulk data deletion @endlink. Cleans
|
226
|
* up after bulk deletion operations such as field_delete_field()
|
227
|
* and field_delete_instance().
|
228
|
*
|
229
|
* - @link field_language Field language API @endlink. Provides native
|
230
|
* multilingual support for the Field API.
|
231
|
*/
|
232
|
|
233
|
/**
|
234
|
* Value for field API indicating a field accepts an unlimited number of values.
|
235
|
*/
|
236
|
define('FIELD_CARDINALITY_UNLIMITED', -1);
|
237
|
|
238
|
/**
|
239
|
* Value for field API indicating a widget doesn't accept default values.
|
240
|
*
|
241
|
* @see hook_field_widget_info()
|
242
|
*/
|
243
|
define('FIELD_BEHAVIOR_NONE', 0x0001);
|
244
|
|
245
|
/**
|
246
|
* Value for field API concerning widget default and multiple value settings.
|
247
|
*
|
248
|
* @see hook_field_widget_info()
|
249
|
*
|
250
|
* When used in a widget default context, indicates the widget accepts default
|
251
|
* values. When used in a multiple value context for a widget that allows the
|
252
|
* input of one single field value, indicates that the widget will be repeated
|
253
|
* for each value input.
|
254
|
*/
|
255
|
define('FIELD_BEHAVIOR_DEFAULT', 0x0002);
|
256
|
|
257
|
/**
|
258
|
* Value for field API indicating a widget can receive several field values.
|
259
|
*
|
260
|
* @see hook_field_widget_info()
|
261
|
*/
|
262
|
define('FIELD_BEHAVIOR_CUSTOM', 0x0004);
|
263
|
|
264
|
/**
|
265
|
* Age argument for loading the most recent version of an entity's
|
266
|
* field data with field_attach_load().
|
267
|
*/
|
268
|
define('FIELD_LOAD_CURRENT', 'FIELD_LOAD_CURRENT');
|
269
|
|
270
|
/**
|
271
|
* Age argument for loading the version of an entity's field data
|
272
|
* specified in the entity with field_attach_load().
|
273
|
*/
|
274
|
define('FIELD_LOAD_REVISION', 'FIELD_LOAD_REVISION');
|
275
|
|
276
|
/**
|
277
|
* Exception class thrown by hook_field_update_forbid().
|
278
|
*/
|
279
|
class FieldUpdateForbiddenException extends FieldException {}
|
280
|
|
281
|
/**
|
282
|
* Implements hook_help().
|
283
|
*/
|
284
|
function field_help($path, $arg) {
|
285
|
switch ($path) {
|
286
|
case 'admin/help#field':
|
287
|
$output = '';
|
288
|
$output .= '<h3>' . t('About') . '</h3>';
|
289
|
$output .= '<p>' . t('The Field module allows custom data fields to be defined for <em>entity</em> types (entities include content items, comments, user accounts, and taxonomy terms). The Field module takes care of storing, loading, editing, and rendering field data. Most users will not interact with the Field module directly, but will instead use the <a href="@field-ui-help">Field UI module</a> user interface. Module developers can use the Field API to make new entity types "fieldable" and thus allow fields to be attached to them. For more information, see the online handbook entry for <a href="@field">Field module</a>.', array('@field-ui-help' => url('admin/help/field_ui'), '@field' => 'http://drupal.org/documentation/modules/field')) . '</p>';
|
290
|
$output .= '<h3>' . t('Uses') . '</h3>';
|
291
|
$output .= '<dl>';
|
292
|
$output .= '<dt>' . t('Enabling field types') . '</dt>';
|
293
|
$output .= '<dd>' . t('The Field module provides the infrastructure for fields and field attachment; the field types and input widgets themselves are provided by additional modules. Some of the modules are required; the optional modules can be enabled from the <a href="@modules">Modules administration page</a>. Drupal core includes the following field type modules: Number (required), Text (required), List (required), Taxonomy (optional), Image (optional), and File (optional); the required Options module provides input widgets for other field modules. Additional fields and widgets may be provided by contributed modules, which you can find in the <a href="@contrib">contributed module section of Drupal.org</a>. Currently enabled field and input widget modules:', array('@modules' => url('admin/modules'), '@contrib' => 'http://drupal.org/project/modules', '@options' => url('admin/help/options')));
|
294
|
|
295
|
// Make a list of all widget and field modules currently enabled, in
|
296
|
// order by displayed module name (module names are not translated).
|
297
|
$items = array();
|
298
|
$info = system_get_info('module');
|
299
|
$modules = array_merge(module_implements('field_info'), module_implements('field_widget_info'));
|
300
|
$modules = array_unique($modules);
|
301
|
sort($modules);
|
302
|
foreach ($modules as $module) {
|
303
|
$display = $info[$module]['name'];
|
304
|
if (module_hook($module, 'help')) {
|
305
|
$items['items'][] = l($display, 'admin/help/' . $module);
|
306
|
}
|
307
|
else {
|
308
|
$items['items'][] = $display;
|
309
|
}
|
310
|
}
|
311
|
$output .= theme('item_list', $items) . '</dd>';
|
312
|
$output .= '<dt>' . t('Managing field data storage') . '</dt>';
|
313
|
$output .= '<dd>' . t('Developers of field modules can either use the default <a href="@sql-store">Field SQL storage module</a> to store data for their fields, or a contributed or custom module developed using the <a href="@storage-api">field storage API</a>.', array('@storage-api' => 'http://api.drupal.org/api/group/field_storage/7', '@sql-store' => url('admin/help/field_sql_storage'))) . '</dd>';
|
314
|
$output .= '</dl>';
|
315
|
return $output;
|
316
|
}
|
317
|
}
|
318
|
|
319
|
/**
|
320
|
* Implements hook_theme().
|
321
|
*/
|
322
|
function field_theme() {
|
323
|
return array(
|
324
|
'field' => array(
|
325
|
'render element' => 'element',
|
326
|
),
|
327
|
'field_multiple_value_form' => array(
|
328
|
'render element' => 'element',
|
329
|
),
|
330
|
);
|
331
|
}
|
332
|
|
333
|
/**
|
334
|
* Implements hook_cron().
|
335
|
*/
|
336
|
function field_cron() {
|
337
|
// Refresh the 'active' status of fields.
|
338
|
field_sync_field_status();
|
339
|
|
340
|
// Do a pass of purging on deleted Field API data, if any exists.
|
341
|
$limit = variable_get('field_purge_batch_size', 10);
|
342
|
field_purge_batch($limit);
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Implements hook_modules_uninstalled().
|
347
|
*/
|
348
|
function field_modules_uninstalled($modules) {
|
349
|
module_load_include('inc', 'field', 'field.crud');
|
350
|
foreach ($modules as $module) {
|
351
|
// TODO D7: field_module_delete is not yet implemented
|
352
|
// field_module_delete($module);
|
353
|
}
|
354
|
}
|
355
|
|
356
|
/**
|
357
|
* Implements hook_system_info_alter().
|
358
|
*
|
359
|
* Goes through a list of all modules that provide a field type, and makes them
|
360
|
* required if there are any active fields of that type.
|
361
|
*/
|
362
|
function field_system_info_alter(&$info, $file, $type) {
|
363
|
if ($type == 'module' && module_hook($file->name, 'field_info')) {
|
364
|
$fields = field_read_fields(array('module' => $file->name), array('include_deleted' => TRUE));
|
365
|
if ($fields) {
|
366
|
$info['required'] = TRUE;
|
367
|
|
368
|
// Provide an explanation message (only mention pending deletions if there
|
369
|
// remains no actual, non-deleted fields)
|
370
|
$non_deleted = FALSE;
|
371
|
foreach ($fields as $field) {
|
372
|
if (empty($field['deleted'])) {
|
373
|
$non_deleted = TRUE;
|
374
|
break;
|
375
|
}
|
376
|
}
|
377
|
if ($non_deleted) {
|
378
|
if (module_exists('field_ui')) {
|
379
|
$explanation = t('Field type(s) in use - see <a href="@fields-page">Field list</a>', array('@fields-page' => url('admin/reports/fields')));
|
380
|
}
|
381
|
else {
|
382
|
$explanation = t('Fields type(s) in use');
|
383
|
}
|
384
|
}
|
385
|
else {
|
386
|
$explanation = t('Fields pending deletion');
|
387
|
}
|
388
|
$info['explanation'] = $explanation;
|
389
|
}
|
390
|
}
|
391
|
}
|
392
|
|
393
|
/**
|
394
|
* Implements hook_flush_caches().
|
395
|
*/
|
396
|
function field_flush_caches() {
|
397
|
// Refresh the 'active' status of fields.
|
398
|
field_sync_field_status();
|
399
|
|
400
|
// Request a flush of our cache table.
|
401
|
return array('cache_field');
|
402
|
}
|
403
|
|
404
|
/**
|
405
|
* Implements hook_modules_enabled().
|
406
|
*/
|
407
|
function field_modules_enabled($modules) {
|
408
|
// Refresh the 'active' status of fields.
|
409
|
field_sync_field_status();
|
410
|
}
|
411
|
|
412
|
/**
|
413
|
* Implements hook_modules_disabled().
|
414
|
*/
|
415
|
function field_modules_disabled($modules) {
|
416
|
// Refresh the 'active' status of fields.
|
417
|
field_sync_field_status();
|
418
|
}
|
419
|
|
420
|
/**
|
421
|
* Refreshes the 'active' and 'storage_active' columns for fields.
|
422
|
*/
|
423
|
function field_sync_field_status() {
|
424
|
// Refresh the 'active' and 'storage_active' columns according to the current
|
425
|
// set of enabled modules.
|
426
|
$modules = module_list();
|
427
|
foreach ($modules as $module_name) {
|
428
|
field_associate_fields($module_name);
|
429
|
}
|
430
|
db_update('field_config')
|
431
|
->fields(array('active' => 0))
|
432
|
->condition('module', $modules, 'NOT IN')
|
433
|
->execute();
|
434
|
db_update('field_config')
|
435
|
->fields(array('storage_active' => 0))
|
436
|
->condition('storage_module', $modules, 'NOT IN')
|
437
|
->execute();
|
438
|
}
|
439
|
|
440
|
/**
|
441
|
* Allows a module to update the database for fields and columns it controls.
|
442
|
*
|
443
|
* @param $module
|
444
|
* The name of the module to update on.
|
445
|
*/
|
446
|
function field_associate_fields($module) {
|
447
|
// Associate field types.
|
448
|
$field_types = (array) module_invoke($module, 'field_info');
|
449
|
if ($field_types) {
|
450
|
db_update('field_config')
|
451
|
->fields(array('module' => $module, 'active' => 1))
|
452
|
->condition('type', array_keys($field_types))
|
453
|
->execute();
|
454
|
}
|
455
|
// Associate storage backends.
|
456
|
$storage_types = (array) module_invoke($module, 'field_storage_info');
|
457
|
if ($storage_types) {
|
458
|
db_update('field_config')
|
459
|
->fields(array('storage_module' => $module, 'storage_active' => 1))
|
460
|
->condition('storage_type', array_keys($storage_types))
|
461
|
->execute();
|
462
|
}
|
463
|
}
|
464
|
|
465
|
/**
|
466
|
* Helper function to get the default value for a field on an entity.
|
467
|
*
|
468
|
* @param $entity_type
|
469
|
* The type of $entity; e.g., 'node' or 'user'.
|
470
|
* @param $entity
|
471
|
* The entity for the operation.
|
472
|
* @param $field
|
473
|
* The field structure.
|
474
|
* @param $instance
|
475
|
* The instance structure.
|
476
|
* @param $langcode
|
477
|
* The field language to fill-in with the default value.
|
478
|
*/
|
479
|
function field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL) {
|
480
|
$items = array();
|
481
|
if (!empty($instance['default_value_function'])) {
|
482
|
$function = $instance['default_value_function'];
|
483
|
if (function_exists($function)) {
|
484
|
$items = $function($entity_type, $entity, $field, $instance, $langcode);
|
485
|
}
|
486
|
}
|
487
|
elseif (!empty($instance['default_value'])) {
|
488
|
$items = $instance['default_value'];
|
489
|
}
|
490
|
return $items;
|
491
|
}
|
492
|
|
493
|
/**
|
494
|
* Helper function to filter out empty field values.
|
495
|
*
|
496
|
* @param $field
|
497
|
* The field definition.
|
498
|
* @param $items
|
499
|
* The field values to filter.
|
500
|
*
|
501
|
* @return
|
502
|
* The array of items without empty field values. The function also renumbers
|
503
|
* the array keys to ensure sequential deltas.
|
504
|
*/
|
505
|
function _field_filter_items($field, $items) {
|
506
|
$function = $field['module'] . '_field_is_empty';
|
507
|
foreach ((array) $items as $delta => $item) {
|
508
|
// Explicitly break if the function is undefined.
|
509
|
if ($function($item, $field)) {
|
510
|
unset($items[$delta]);
|
511
|
}
|
512
|
}
|
513
|
return array_values($items);
|
514
|
}
|
515
|
|
516
|
/**
|
517
|
* Helper function to sort items in a field according to
|
518
|
* user drag-n-drop reordering.
|
519
|
*/
|
520
|
function _field_sort_items($field, $items) {
|
521
|
if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) {
|
522
|
usort($items, '_field_sort_items_helper');
|
523
|
foreach ($items as $delta => $item) {
|
524
|
if (is_array($items[$delta])) {
|
525
|
unset($items[$delta]['_weight']);
|
526
|
}
|
527
|
}
|
528
|
}
|
529
|
return $items;
|
530
|
}
|
531
|
|
532
|
/**
|
533
|
* Sort function for items order.
|
534
|
* (copied form element_sort(), which acts on #weight keys)
|
535
|
*/
|
536
|
function _field_sort_items_helper($a, $b) {
|
537
|
$a_weight = (is_array($a) ? $a['_weight'] : 0);
|
538
|
$b_weight = (is_array($b) ? $b['_weight'] : 0);
|
539
|
return $a_weight - $b_weight;
|
540
|
}
|
541
|
|
542
|
/**
|
543
|
* Same as above, using ['_weight']['#value']
|
544
|
*/
|
545
|
function _field_sort_items_value_helper($a, $b) {
|
546
|
$a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
|
547
|
$b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
|
548
|
return $a_weight - $b_weight;
|
549
|
}
|
550
|
|
551
|
/**
|
552
|
* Gets or sets administratively defined bundle settings.
|
553
|
*
|
554
|
* @param string $entity_type
|
555
|
* The type of $entity; e.g., 'node' or 'user'.
|
556
|
* @param string $bundle
|
557
|
* The bundle name.
|
558
|
* @param array|null $settings
|
559
|
* (optional) The settings to store, an associative array with the following
|
560
|
* elements:
|
561
|
* - view_modes: An associative array keyed by view mode, with the following
|
562
|
* key/value pairs:
|
563
|
* - custom_settings: Boolean specifying whether the view mode uses a
|
564
|
* dedicated set of display options (TRUE), or the 'default' options
|
565
|
* (FALSE). Defaults to FALSE.
|
566
|
* - extra_fields: An associative array containing the form and display
|
567
|
* settings for extra fields (also known as pseudo-fields):
|
568
|
* - form: An associative array whose keys are the names of extra fields,
|
569
|
* and whose values are associative arrays with the following elements:
|
570
|
* - weight: The weight of the extra field, determining its position on an
|
571
|
* entity form.
|
572
|
* - display: An associative array whose keys are the names of extra fields,
|
573
|
* and whose values are associative arrays keyed by the name of view
|
574
|
* modes. This array must include an item for the 'default' view mode.
|
575
|
* Each view mode sub-array contains the following elements:
|
576
|
* - weight: The weight of the extra field, determining its position when
|
577
|
* an entity is viewed.
|
578
|
* - visible: TRUE if the extra field is visible, FALSE otherwise.
|
579
|
*
|
580
|
* @return array|null
|
581
|
* If no $settings are passed, the current settings are returned.
|
582
|
*/
|
583
|
function field_bundle_settings($entity_type, $bundle, $settings = NULL) {
|
584
|
if (isset($settings)) {
|
585
|
variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $settings);
|
586
|
field_info_cache_clear();
|
587
|
}
|
588
|
else {
|
589
|
$settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle, array());
|
590
|
$settings += array(
|
591
|
'view_modes' => array(),
|
592
|
'extra_fields' => array(),
|
593
|
);
|
594
|
$settings['extra_fields'] += array(
|
595
|
'form' => array(),
|
596
|
'display' => array(),
|
597
|
);
|
598
|
|
599
|
return $settings;
|
600
|
}
|
601
|
}
|
602
|
|
603
|
/**
|
604
|
* Returns view mode settings in a given bundle.
|
605
|
*
|
606
|
* @param $entity_type
|
607
|
* The type of entity; e.g. 'node' or 'user'.
|
608
|
* @param $bundle
|
609
|
* The bundle name to return view mode settings for.
|
610
|
*
|
611
|
* @return
|
612
|
* An array keyed by view mode, with the following key/value pairs:
|
613
|
* - custom_settings: Boolean specifying whether the view mode uses a
|
614
|
* dedicated set of display options (TRUE), or the 'default' options
|
615
|
* (FALSE). Defaults to FALSE.
|
616
|
*/
|
617
|
function field_view_mode_settings($entity_type, $bundle) {
|
618
|
$cache = &drupal_static(__FUNCTION__, array());
|
619
|
|
620
|
if (!isset($cache[$entity_type][$bundle])) {
|
621
|
$bundle_settings = field_bundle_settings($entity_type, $bundle);
|
622
|
$settings = $bundle_settings['view_modes'];
|
623
|
// Include view modes for which nothing has been stored yet, but whose
|
624
|
// definition in hook_entity_info() specify they should use custom settings
|
625
|
// by default.
|
626
|
$entity_info = entity_get_info($entity_type);
|
627
|
foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
|
628
|
if (!isset($settings[$view_mode]['custom_settings']) && $view_mode_info['custom settings']) {
|
629
|
$settings[$view_mode]['custom_settings'] = TRUE;
|
630
|
}
|
631
|
}
|
632
|
$cache[$entity_type][$bundle] = $settings;
|
633
|
}
|
634
|
|
635
|
return $cache[$entity_type][$bundle];
|
636
|
}
|
637
|
|
638
|
/**
|
639
|
* Returns the display settings to use for an instance in a given view mode.
|
640
|
*
|
641
|
* @param $instance
|
642
|
* The field instance being displayed.
|
643
|
* @param $view_mode
|
644
|
* The view mode.
|
645
|
* @param $entity
|
646
|
* The entity being displayed.
|
647
|
*
|
648
|
* @return
|
649
|
* The display settings to be used when displaying the field values.
|
650
|
*/
|
651
|
function field_get_display($instance, $view_mode, $entity) {
|
652
|
// Check whether the view mode uses custom display settings or the 'default'
|
653
|
// mode.
|
654
|
$view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
|
655
|
$actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
|
656
|
$display = $instance['display'][$actual_mode];
|
657
|
|
658
|
// Let modules alter the display settings.
|
659
|
$context = array(
|
660
|
'entity_type' => $instance['entity_type'],
|
661
|
'field' => field_info_field($instance['field_name']),
|
662
|
'instance' => $instance,
|
663
|
'entity' => $entity,
|
664
|
'view_mode' => $view_mode,
|
665
|
);
|
666
|
drupal_alter(array('field_display', 'field_display_' . $instance['entity_type']), $display, $context);
|
667
|
|
668
|
return $display;
|
669
|
}
|
670
|
|
671
|
/**
|
672
|
* Returns the display settings to use for pseudo-fields in a given view mode.
|
673
|
*
|
674
|
* @param $entity_type
|
675
|
* The type of $entity; e.g., 'node' or 'user'.
|
676
|
* @param $bundle
|
677
|
* The bundle name.
|
678
|
* @param $view_mode
|
679
|
* The view mode.
|
680
|
*
|
681
|
* @return
|
682
|
* The display settings to be used when viewing the bundle's pseudo-fields.
|
683
|
*/
|
684
|
function field_extra_fields_get_display($entity_type, $bundle, $view_mode) {
|
685
|
// Check whether the view mode uses custom display settings or the 'default'
|
686
|
// mode.
|
687
|
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
|
688
|
$actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings'])) ? $view_mode : 'default';
|
689
|
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
|
690
|
|
691
|
$displays = array();
|
692
|
foreach ($extra_fields as $name => $value) {
|
693
|
$displays[$name] = $extra_fields[$name]['display'][$actual_mode];
|
694
|
}
|
695
|
|
696
|
// Let modules alter the display settings.
|
697
|
$context = array(
|
698
|
'entity_type' => $entity_type,
|
699
|
'bundle' => $bundle,
|
700
|
'view_mode' => $view_mode,
|
701
|
);
|
702
|
drupal_alter('field_extra_fields_display', $displays, $context);
|
703
|
|
704
|
return $displays;
|
705
|
}
|
706
|
|
707
|
/**
|
708
|
* Pre-render callback to adjust weights and visibility of non-field elements.
|
709
|
*/
|
710
|
function _field_extra_fields_pre_render($elements) {
|
711
|
$entity_type = $elements['#entity_type'];
|
712
|
$bundle = $elements['#bundle'];
|
713
|
|
714
|
if (isset($elements['#type']) && $elements['#type'] == 'form') {
|
715
|
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
|
716
|
foreach ($extra_fields as $name => $settings) {
|
717
|
if (isset($elements[$name])) {
|
718
|
$elements[$name]['#weight'] = $settings['weight'];
|
719
|
}
|
720
|
}
|
721
|
}
|
722
|
elseif (isset($elements['#view_mode'])) {
|
723
|
$view_mode = $elements['#view_mode'];
|
724
|
$extra_fields = field_extra_fields_get_display($entity_type, $bundle, $view_mode);
|
725
|
foreach ($extra_fields as $name => $settings) {
|
726
|
if (isset($elements[$name])) {
|
727
|
$elements[$name]['#weight'] = $settings['weight'];
|
728
|
// Visibility: make sure we do not accidentally show a hidden element.
|
729
|
$elements[$name]['#access'] = isset($elements[$name]['#access']) ? ($elements[$name]['#access'] && $settings['visible']) : $settings['visible'];
|
730
|
}
|
731
|
}
|
732
|
}
|
733
|
|
734
|
return $elements;
|
735
|
}
|
736
|
|
737
|
/**
|
738
|
* Clear the field info and field data caches.
|
739
|
*/
|
740
|
function field_cache_clear() {
|
741
|
cache_clear_all('*', 'cache_field', TRUE);
|
742
|
field_info_cache_clear();
|
743
|
}
|
744
|
|
745
|
/**
|
746
|
* Like filter_xss_admin(), but with a shorter list of allowed tags.
|
747
|
*
|
748
|
* Used for items entered by administrators, like field descriptions,
|
749
|
* allowed values, where some (mainly inline) mark-up may be desired
|
750
|
* (so check_plain() is not acceptable).
|
751
|
*/
|
752
|
function field_filter_xss($string) {
|
753
|
return filter_xss($string, _field_filter_xss_allowed_tags());
|
754
|
}
|
755
|
|
756
|
/**
|
757
|
* List of tags allowed by field_filter_xss().
|
758
|
*/
|
759
|
function _field_filter_xss_allowed_tags() {
|
760
|
return array('a', 'b', 'big', 'code', 'del', 'em', 'i', 'ins', 'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
|
761
|
}
|
762
|
|
763
|
/**
|
764
|
* Human-readable list of allowed tags, for display in help texts.
|
765
|
*/
|
766
|
function _field_filter_xss_display_allowed_tags() {
|
767
|
return '<' . implode('> <', _field_filter_xss_allowed_tags()) . '>';
|
768
|
}
|
769
|
|
770
|
/**
|
771
|
* Returns a renderable array for a single field value.
|
772
|
*
|
773
|
* @param $entity_type
|
774
|
* The type of $entity; e.g., 'node' or 'user'.
|
775
|
* @param $entity
|
776
|
* The entity containing the field to display. Must at least contain the id
|
777
|
* key and the field data to display.
|
778
|
* @param $field_name
|
779
|
* The name of the field to display.
|
780
|
* @param $item
|
781
|
* The field value to display, as found in
|
782
|
* $entity->field_name[$langcode][$delta].
|
783
|
* @param $display
|
784
|
* Can be either the name of a view mode, or an array of display settings.
|
785
|
* See field_view_field() for more information.
|
786
|
* @param $langcode
|
787
|
* (Optional) The language of the value in $item. If not provided, the
|
788
|
* current language will be assumed.
|
789
|
* @return
|
790
|
* A renderable array for the field value.
|
791
|
*/
|
792
|
function field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) {
|
793
|
$output = array();
|
794
|
|
795
|
if ($field = field_info_field($field_name)) {
|
796
|
// Determine the langcode that will be used by language fallback.
|
797
|
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
|
798
|
|
799
|
// Push the item as the single value for the field, and defer to
|
800
|
// field_view_field() to build the render array for the whole field.
|
801
|
$clone = clone $entity;
|
802
|
$clone->{$field_name}[$langcode] = array($item);
|
803
|
$elements = field_view_field($entity_type, $clone, $field_name, $display, $langcode);
|
804
|
|
805
|
// Extract the part of the render array we need.
|
806
|
$output = isset($elements[0]) ? $elements[0] : array();
|
807
|
if (isset($elements['#access'])) {
|
808
|
$output['#access'] = $elements['#access'];
|
809
|
}
|
810
|
}
|
811
|
|
812
|
return $output;
|
813
|
}
|
814
|
|
815
|
/**
|
816
|
* Returns a renderable array for the value of a single field in an entity.
|
817
|
*
|
818
|
* The resulting output is a fully themed field with label and multiple values.
|
819
|
*
|
820
|
* This function can be used by third-party modules that need to output an
|
821
|
* isolated field.
|
822
|
* - Do not use inside node (or any other entity) templates; use
|
823
|
* render($content[FIELD_NAME]) instead.
|
824
|
* - Do not use to display all fields in an entity; use
|
825
|
* field_attach_prepare_view() and field_attach_view() instead.
|
826
|
* - The field_view_value() function can be used to output a single formatted
|
827
|
* field value, without label or wrapping field markup.
|
828
|
*
|
829
|
* The function takes care of invoking the prepare_view steps. It also respects
|
830
|
* field access permissions.
|
831
|
*
|
832
|
* @param $entity_type
|
833
|
* The type of $entity; e.g., 'node' or 'user'.
|
834
|
* @param $entity
|
835
|
* The entity containing the field to display. Must at least contain the id
|
836
|
* key and the field data to display.
|
837
|
* @param $field_name
|
838
|
* The name of the field to display.
|
839
|
* @param $display
|
840
|
* Can be either:
|
841
|
* - The name of a view mode. The field will be displayed according to the
|
842
|
* display settings specified for this view mode in the $instance
|
843
|
* definition for the field in the entity's bundle.
|
844
|
* If no display settings are found for the view mode, the settings for
|
845
|
* the 'default' view mode will be used.
|
846
|
* - An array of display settings, as found in the 'display' entry of
|
847
|
* $instance definitions. The following key/value pairs are allowed:
|
848
|
* - label: (string) Position of the label. The default 'field' theme
|
849
|
* implementation supports the values 'inline', 'above' and 'hidden'.
|
850
|
* Defaults to 'above'.
|
851
|
* - type: (string) The formatter to use. Defaults to the
|
852
|
* 'default_formatter' for the field type, specified in
|
853
|
* hook_field_info(). The default formatter will also be used if the
|
854
|
* requested formatter is not available.
|
855
|
* - settings: (array) Settings specific to the formatter. Defaults to the
|
856
|
* formatter's default settings, specified in
|
857
|
* hook_field_formatter_info().
|
858
|
* - weight: (float) The weight to assign to the renderable element.
|
859
|
* Defaults to 0.
|
860
|
* @param $langcode
|
861
|
* (Optional) The language the field values are to be shown in. The site's
|
862
|
* current language fallback logic will be applied no values are available
|
863
|
* for the language. If no language is provided the current language will be
|
864
|
* used.
|
865
|
* @return
|
866
|
* A renderable array for the field value.
|
867
|
*
|
868
|
* @see field_view_value()
|
869
|
*/
|
870
|
function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
|
871
|
$output = array();
|
872
|
|
873
|
if ($field = field_info_field($field_name)) {
|
874
|
if (is_array($display)) {
|
875
|
// When using custom display settings, fill in default values.
|
876
|
$cache = _field_info_field_cache();
|
877
|
$display = $cache->prepareInstanceDisplay($display, $field["type"]);
|
878
|
}
|
879
|
|
880
|
// Hook invocations are done through the _field_invoke() functions in
|
881
|
// 'single field' mode, to reuse the language fallback logic.
|
882
|
// Determine the actual language to display for the field, given the
|
883
|
// languages available in the field data.
|
884
|
$display_language = field_language($entity_type, $entity, $field_name, $langcode);
|
885
|
$options = array('field_name' => $field_name, 'language' => $display_language);
|
886
|
$null = NULL;
|
887
|
|
888
|
// Invoke prepare_view steps if needed.
|
889
|
if (empty($entity->_field_view_prepared)) {
|
890
|
list($id) = entity_extract_ids($entity_type, $entity);
|
891
|
|
892
|
// First let the field types do their preparation.
|
893
|
_field_invoke_multiple('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
|
894
|
// Then let the formatters do their own specific massaging.
|
895
|
_field_invoke_multiple_default('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
|
896
|
}
|
897
|
|
898
|
// Build the renderable array.
|
899
|
$result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);
|
900
|
|
901
|
// Invoke hook_field_attach_view_alter() to let other modules alter the
|
902
|
// renderable array, as in a full field_attach_view() execution.
|
903
|
$context = array(
|
904
|
'entity_type' => $entity_type,
|
905
|
'entity' => $entity,
|
906
|
'view_mode' => '_custom',
|
907
|
'display' => $display,
|
908
|
);
|
909
|
drupal_alter('field_attach_view', $result, $context);
|
910
|
|
911
|
if (isset($result[$field_name])) {
|
912
|
$output = $result[$field_name];
|
913
|
}
|
914
|
}
|
915
|
|
916
|
return $output;
|
917
|
}
|
918
|
|
919
|
/**
|
920
|
* Returns the field items in the language they currently would be displayed.
|
921
|
*
|
922
|
* @param $entity_type
|
923
|
* The type of $entity; e.g., 'node' or 'user'.
|
924
|
* @param $entity
|
925
|
* The entity containing the data to be displayed.
|
926
|
* @param $field_name
|
927
|
* The field to be displayed.
|
928
|
* @param $langcode
|
929
|
* (optional) The language code $entity->{$field_name} has to be displayed in.
|
930
|
* Defaults to the current language.
|
931
|
*
|
932
|
* @return
|
933
|
* An array of field items keyed by delta if available, FALSE otherwise.
|
934
|
*/
|
935
|
function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
|
936
|
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
|
937
|
return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
|
938
|
}
|
939
|
|
940
|
/**
|
941
|
* Determine whether a field has any data.
|
942
|
*
|
943
|
* @param $field
|
944
|
* A field structure.
|
945
|
* @return
|
946
|
* TRUE if the field has data for any entity; FALSE otherwise.
|
947
|
*/
|
948
|
function field_has_data($field) {
|
949
|
$query = new EntityFieldQuery();
|
950
|
return (bool) $query
|
951
|
->fieldCondition($field)
|
952
|
->range(0, 1)
|
953
|
->count()
|
954
|
// Neutralize the 'entity_field_access' query tag added by
|
955
|
// field_sql_storage_field_storage_query(). The result cannot depend on the
|
956
|
// access grants of the current user.
|
957
|
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
|
958
|
->execute();
|
959
|
}
|
960
|
|
961
|
/**
|
962
|
* Determine whether the user has access to a given field.
|
963
|
*
|
964
|
* @param $op
|
965
|
* The operation to be performed. Possible values:
|
966
|
* - 'edit'
|
967
|
* - 'view'
|
968
|
* @param $field
|
969
|
* The full field structure array for the field on which the operation is to
|
970
|
* be performed. See field_info_field().
|
971
|
* @param $entity_type
|
972
|
* The type of $entity; e.g., 'node' or 'user'.
|
973
|
* @param $entity
|
974
|
* (optional) The entity for the operation.
|
975
|
* @param $account
|
976
|
* (optional) The account to check, if not given use currently logged in user.
|
977
|
*
|
978
|
* @return
|
979
|
* TRUE if the operation is allowed;
|
980
|
* FALSE if the operation is denied.
|
981
|
*/
|
982
|
function field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) {
|
983
|
global $user;
|
984
|
|
985
|
if (!isset($account)) {
|
986
|
$account = $user;
|
987
|
}
|
988
|
|
989
|
foreach (module_implements('field_access') as $module) {
|
990
|
$function = $module . '_field_access';
|
991
|
$access = $function($op, $field, $entity_type, $entity, $account);
|
992
|
if ($access === FALSE) {
|
993
|
return FALSE;
|
994
|
}
|
995
|
}
|
996
|
return TRUE;
|
997
|
}
|
998
|
|
999
|
/**
|
1000
|
* Helper function to extract the bundle name of from a bundle object.
|
1001
|
*
|
1002
|
* @param $entity_type
|
1003
|
* The type of $entity; e.g., 'node' or 'user'.
|
1004
|
* @param $bundle
|
1005
|
* The bundle object (or string if bundles for this entity type do not exist
|
1006
|
* as standalone objects).
|
1007
|
* @return
|
1008
|
* The bundle name.
|
1009
|
*/
|
1010
|
function field_extract_bundle($entity_type, $bundle) {
|
1011
|
if (is_string($bundle)) {
|
1012
|
return $bundle;
|
1013
|
}
|
1014
|
|
1015
|
$info = entity_get_info($entity_type);
|
1016
|
if (is_object($bundle) && isset($info['bundle keys']['bundle']) && isset($bundle->{$info['bundle keys']['bundle']})) {
|
1017
|
return $bundle->{$info['bundle keys']['bundle']};
|
1018
|
}
|
1019
|
}
|
1020
|
|
1021
|
/**
|
1022
|
* Theme preprocess function for theme_field() and field.tpl.php.
|
1023
|
*
|
1024
|
* @see theme_field()
|
1025
|
* @see field.tpl.php
|
1026
|
*/
|
1027
|
function template_preprocess_field(&$variables, $hook) {
|
1028
|
$element = $variables['element'];
|
1029
|
|
1030
|
// There's some overhead in calling check_plain() so only call it if the label
|
1031
|
// variable is being displayed. Otherwise, set it to NULL to avoid PHP
|
1032
|
// warnings if a theme implementation accesses the variable even when it's
|
1033
|
// supposed to be hidden. If a theme implementation needs to print a hidden
|
1034
|
// label, it needs to supply a preprocess function that sets it to the
|
1035
|
// sanitized element title or whatever else is wanted in its place.
|
1036
|
$variables['label_hidden'] = ($element['#label_display'] == 'hidden');
|
1037
|
$variables['label'] = $variables['label_hidden'] ? NULL : check_plain($element['#title']);
|
1038
|
|
1039
|
// We want other preprocess functions and the theme implementation to have
|
1040
|
// fast access to the field item render arrays. The item render array keys
|
1041
|
// (deltas) should always be a subset of the keys in #items, and looping on
|
1042
|
// those keys is faster than calling element_children() or looping on all keys
|
1043
|
// within $element, since that requires traversal of all element properties.
|
1044
|
$variables['items'] = array();
|
1045
|
foreach ($element['#items'] as $delta => $item) {
|
1046
|
if (!empty($element[$delta])) {
|
1047
|
$variables['items'][$delta] = $element[$delta];
|
1048
|
}
|
1049
|
}
|
1050
|
|
1051
|
// Add default CSS classes. Since there can be many fields rendered on a page,
|
1052
|
// save some overhead by calling strtr() directly instead of
|
1053
|
// drupal_html_class().
|
1054
|
$variables['field_name_css'] = strtr($element['#field_name'], '_', '-');
|
1055
|
$variables['field_type_css'] = strtr($element['#field_type'], '_', '-');
|
1056
|
$variables['classes_array'] = array(
|
1057
|
'field',
|
1058
|
'field-name-' . $variables['field_name_css'],
|
1059
|
'field-type-' . $variables['field_type_css'],
|
1060
|
'field-label-' . $element['#label_display'],
|
1061
|
);
|
1062
|
// Add a "clearfix" class to the wrapper since we float the label and the
|
1063
|
// field items in field.css if the label is inline.
|
1064
|
if ($element['#label_display'] == 'inline') {
|
1065
|
$variables['classes_array'][] = 'clearfix';
|
1066
|
}
|
1067
|
|
1068
|
// Add specific suggestions that can override the default implementation.
|
1069
|
$variables['theme_hook_suggestions'] = array(
|
1070
|
'field__' . $element['#field_type'],
|
1071
|
'field__' . $element['#field_name'],
|
1072
|
'field__' . $element['#bundle'],
|
1073
|
'field__' . $element['#field_name'] . '__' . $element['#bundle'],
|
1074
|
);
|
1075
|
}
|
1076
|
|
1077
|
/**
|
1078
|
* Theme process function for theme_field() and field.tpl.php.
|
1079
|
*
|
1080
|
* @see theme_field()
|
1081
|
* @see field.tpl.php
|
1082
|
*/
|
1083
|
function template_process_field(&$variables, $hook) {
|
1084
|
// The default theme implementation is a function, so template_process() does
|
1085
|
// not automatically run, so we need to flatten the classes and attributes
|
1086
|
// here. For best performance, only call drupal_attributes() when needed, and
|
1087
|
// note that template_preprocess_field() does not initialize the
|
1088
|
// *_attributes_array variables.
|
1089
|
$variables['classes'] = implode(' ', $variables['classes_array']);
|
1090
|
$variables['attributes'] = empty($variables['attributes_array']) ? '' : drupal_attributes($variables['attributes_array']);
|
1091
|
$variables['title_attributes'] = empty($variables['title_attributes_array']) ? '' : drupal_attributes($variables['title_attributes_array']);
|
1092
|
$variables['content_attributes'] = empty($variables['content_attributes_array']) ? '' : drupal_attributes($variables['content_attributes_array']);
|
1093
|
foreach ($variables['items'] as $delta => $item) {
|
1094
|
$variables['item_attributes'][$delta] = empty($variables['item_attributes_array'][$delta]) ? '' : drupal_attributes($variables['item_attributes_array'][$delta]);
|
1095
|
}
|
1096
|
}
|
1097
|
/**
|
1098
|
* @} End of "defgroup field".
|
1099
|
*/
|
1100
|
|
1101
|
/**
|
1102
|
* Returns HTML for a field.
|
1103
|
*
|
1104
|
* This is the default theme implementation to display the value of a field.
|
1105
|
* Theme developers who are comfortable with overriding theme functions may do
|
1106
|
* so in order to customize this markup. This function can be overridden with
|
1107
|
* varying levels of specificity. For example, for a field named 'body'
|
1108
|
* displayed on the 'article' content type, any of the following functions will
|
1109
|
* override this default implementation. The first of these functions that
|
1110
|
* exists is used:
|
1111
|
* - THEMENAME_field__body__article()
|
1112
|
* - THEMENAME_field__article()
|
1113
|
* - THEMENAME_field__body()
|
1114
|
* - THEMENAME_field()
|
1115
|
*
|
1116
|
* Theme developers who prefer to customize templates instead of overriding
|
1117
|
* functions may copy the "field.tpl.php" from the "modules/field/theme" folder
|
1118
|
* of the Drupal installation to somewhere within the theme's folder and
|
1119
|
* customize it, just like customizing other Drupal templates such as
|
1120
|
* page.tpl.php or node.tpl.php. However, it takes longer for the server to
|
1121
|
* process templates than to call a function, so for websites with many fields
|
1122
|
* displayed on a page, this can result in a noticeable slowdown of the website.
|
1123
|
* For these websites, developers are discouraged from placing a field.tpl.php
|
1124
|
* file into the theme's folder, but may customize templates for specific
|
1125
|
* fields. For example, for a field named 'body' displayed on the 'article'
|
1126
|
* content type, any of the following templates will override this default
|
1127
|
* implementation. The first of these templates that exists is used:
|
1128
|
* - field--body--article.tpl.php
|
1129
|
* - field--article.tpl.php
|
1130
|
* - field--body.tpl.php
|
1131
|
* - field.tpl.php
|
1132
|
* So, if the body field on the article content type needs customization, a
|
1133
|
* field--body--article.tpl.php file can be added within the theme's folder.
|
1134
|
* Because it's a template, it will result in slightly more time needed to
|
1135
|
* display that field, but it will not impact other fields, and therefore,
|
1136
|
* is unlikely to cause a noticeable change in website performance. A very rough
|
1137
|
* guideline is that if a page is being displayed with more than 100 fields and
|
1138
|
* they are all themed with a template instead of a function, it can add up to
|
1139
|
* 5% to the time it takes to display that page. This is a guideline only and
|
1140
|
* the exact performance impact depends on the server configuration and the
|
1141
|
* details of the website.
|
1142
|
*
|
1143
|
* @param $variables
|
1144
|
* An associative array containing:
|
1145
|
* - label_hidden: A boolean indicating to show or hide the field label.
|
1146
|
* - title_attributes: A string containing the attributes for the title.
|
1147
|
* - label: The label for the field.
|
1148
|
* - content_attributes: A string containing the attributes for the content's
|
1149
|
* div.
|
1150
|
* - items: An array of field items.
|
1151
|
* - item_attributes: An array of attributes for each item.
|
1152
|
* - classes: A string containing the classes for the wrapping div.
|
1153
|
* - attributes: A string containing the attributes for the wrapping div.
|
1154
|
*
|
1155
|
* @see template_preprocess_field()
|
1156
|
* @see template_process_field()
|
1157
|
* @see field.tpl.php
|
1158
|
*
|
1159
|
* @ingroup themeable
|
1160
|
*/
|
1161
|
function theme_field($variables) {
|
1162
|
$output = '';
|
1163
|
|
1164
|
// Render the label, if it's not hidden.
|
1165
|
if (!$variables['label_hidden']) {
|
1166
|
$output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ': </div>';
|
1167
|
}
|
1168
|
|
1169
|
// Render the items.
|
1170
|
$output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
|
1171
|
foreach ($variables['items'] as $delta => $item) {
|
1172
|
$classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
|
1173
|
$output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
|
1174
|
}
|
1175
|
$output .= '</div>';
|
1176
|
|
1177
|
// Render the top-level DIV.
|
1178
|
$output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
|
1179
|
|
1180
|
return $output;
|
1181
|
}
|
1182
|
|
1183
|
/**
|
1184
|
* DEPRECATED: Helper form element validator: integer.
|
1185
|
*
|
1186
|
* Use element_validate_integer() instead.
|
1187
|
*
|
1188
|
* @deprecated
|
1189
|
* @see element_validate_integer()
|
1190
|
*/
|
1191
|
function _element_validate_integer($element, &$form_state) {
|
1192
|
element_validate_integer($element, $form_state);
|
1193
|
}
|
1194
|
|
1195
|
/**
|
1196
|
* DEPRECATED: Helper form element validator: integer > 0.
|
1197
|
*
|
1198
|
* Use element_validate_integer_positive() instead.
|
1199
|
*
|
1200
|
* @deprecated
|
1201
|
* @see element_validate_integer_positive()
|
1202
|
*/
|
1203
|
function _element_validate_integer_positive($element, &$form_state) {
|
1204
|
element_validate_integer_positive($element, $form_state);
|
1205
|
}
|
1206
|
|
1207
|
/**
|
1208
|
* DEPRECATED: Helper form element validator: number.
|
1209
|
*
|
1210
|
* Use element_validate_number() instead.
|
1211
|
*
|
1212
|
* @deprecated
|
1213
|
* @see element_validate_number()
|
1214
|
*/
|
1215
|
function _element_validate_number($element, &$form_state) {
|
1216
|
element_validate_number($element, $form_state);
|
1217
|
}
|