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_system_info_alter().
|
347
|
*
|
348
|
* Goes through a list of all modules that provide a field type, and makes them
|
349
|
* required if there are any active fields of that type.
|
350
|
*/
|
351
|
function field_system_info_alter(&$info, $file, $type) {
|
352
|
if ($type == 'module' && module_hook($file->name, 'field_info')) {
|
353
|
$fields = field_read_fields(array('module' => $file->name), array('include_deleted' => TRUE));
|
354
|
if ($fields) {
|
355
|
$info['required'] = TRUE;
|
356
|
|
357
|
// Provide an explanation message (only mention pending deletions if there
|
358
|
// remains no actual, non-deleted fields)
|
359
|
$non_deleted = FALSE;
|
360
|
foreach ($fields as $field) {
|
361
|
if (empty($field['deleted'])) {
|
362
|
$non_deleted = TRUE;
|
363
|
break;
|
364
|
}
|
365
|
}
|
366
|
if ($non_deleted) {
|
367
|
if (module_exists('field_ui')) {
|
368
|
$explanation = t('Field type(s) in use - see <a href="@fields-page">Field list</a>', array('@fields-page' => url('admin/reports/fields')));
|
369
|
}
|
370
|
else {
|
371
|
$explanation = t('Fields type(s) in use');
|
372
|
}
|
373
|
}
|
374
|
else {
|
375
|
$explanation = t('Fields pending deletion');
|
376
|
}
|
377
|
$info['explanation'] = $explanation;
|
378
|
}
|
379
|
}
|
380
|
}
|
381
|
|
382
|
/**
|
383
|
* Implements hook_flush_caches().
|
384
|
*/
|
385
|
function field_flush_caches() {
|
386
|
// Refresh the 'active' status of fields.
|
387
|
field_sync_field_status();
|
388
|
|
389
|
// Request a flush of our cache table.
|
390
|
return array('cache_field');
|
391
|
}
|
392
|
|
393
|
/**
|
394
|
* Implements hook_modules_enabled().
|
395
|
*/
|
396
|
function field_modules_enabled($modules) {
|
397
|
// Refresh the 'active' status of fields.
|
398
|
field_sync_field_status();
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Implements hook_modules_disabled().
|
403
|
*/
|
404
|
function field_modules_disabled($modules) {
|
405
|
// Refresh the 'active' status of fields.
|
406
|
field_sync_field_status();
|
407
|
}
|
408
|
|
409
|
/**
|
410
|
* Refreshes the 'active' and 'storage_active' columns for fields.
|
411
|
*/
|
412
|
function field_sync_field_status() {
|
413
|
// Refresh the 'active' and 'storage_active' columns according to the current
|
414
|
// set of enabled modules.
|
415
|
$modules = module_list();
|
416
|
foreach ($modules as $module_name) {
|
417
|
field_associate_fields($module_name);
|
418
|
}
|
419
|
db_update('field_config')
|
420
|
->fields(array('active' => 0))
|
421
|
->condition('module', $modules, 'NOT IN')
|
422
|
->execute();
|
423
|
db_update('field_config')
|
424
|
->fields(array('storage_active' => 0))
|
425
|
->condition('storage_module', $modules, 'NOT IN')
|
426
|
->execute();
|
427
|
}
|
428
|
|
429
|
/**
|
430
|
* Allows a module to update the database for fields and columns it controls.
|
431
|
*
|
432
|
* @param $module
|
433
|
* The name of the module to update on.
|
434
|
*/
|
435
|
function field_associate_fields($module) {
|
436
|
// Associate field types.
|
437
|
$field_types = (array) module_invoke($module, 'field_info');
|
438
|
if ($field_types) {
|
439
|
db_update('field_config')
|
440
|
->fields(array('module' => $module, 'active' => 1))
|
441
|
->condition('type', array_keys($field_types))
|
442
|
->execute();
|
443
|
}
|
444
|
// Associate storage backends.
|
445
|
$storage_types = (array) module_invoke($module, 'field_storage_info');
|
446
|
if ($storage_types) {
|
447
|
db_update('field_config')
|
448
|
->fields(array('storage_module' => $module, 'storage_active' => 1))
|
449
|
->condition('storage_type', array_keys($storage_types))
|
450
|
->execute();
|
451
|
}
|
452
|
}
|
453
|
|
454
|
/**
|
455
|
* Helper function to get the default value for a field on an entity.
|
456
|
*
|
457
|
* @param $entity_type
|
458
|
* The type of $entity; e.g., 'node' or 'user'.
|
459
|
* @param $entity
|
460
|
* The entity for the operation.
|
461
|
* @param $field
|
462
|
* The field structure.
|
463
|
* @param $instance
|
464
|
* The instance structure.
|
465
|
* @param $langcode
|
466
|
* The field language to fill-in with the default value.
|
467
|
*/
|
468
|
function field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL) {
|
469
|
$items = array();
|
470
|
if (!empty($instance['default_value_function'])) {
|
471
|
$function = $instance['default_value_function'];
|
472
|
if (function_exists($function)) {
|
473
|
$items = $function($entity_type, $entity, $field, $instance, $langcode);
|
474
|
}
|
475
|
}
|
476
|
elseif (!empty($instance['default_value'])) {
|
477
|
$items = $instance['default_value'];
|
478
|
}
|
479
|
return $items;
|
480
|
}
|
481
|
|
482
|
/**
|
483
|
* Helper function to filter out empty field values.
|
484
|
*
|
485
|
* @param $field
|
486
|
* The field definition.
|
487
|
* @param $items
|
488
|
* The field values to filter.
|
489
|
*
|
490
|
* @return
|
491
|
* The array of items without empty field values. The function also renumbers
|
492
|
* the array keys to ensure sequential deltas.
|
493
|
*/
|
494
|
function _field_filter_items($field, $items) {
|
495
|
$function = $field['module'] . '_field_is_empty';
|
496
|
foreach ((array) $items as $delta => $item) {
|
497
|
// Explicitly break if the function is undefined.
|
498
|
if ($function($item, $field)) {
|
499
|
unset($items[$delta]);
|
500
|
}
|
501
|
}
|
502
|
return array_values($items);
|
503
|
}
|
504
|
|
505
|
/**
|
506
|
* Helper function to sort items in a field according to
|
507
|
* user drag-n-drop reordering.
|
508
|
*/
|
509
|
function _field_sort_items($field, $items) {
|
510
|
if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) {
|
511
|
usort($items, '_field_sort_items_helper');
|
512
|
foreach ($items as $delta => $item) {
|
513
|
if (is_array($items[$delta])) {
|
514
|
unset($items[$delta]['_weight']);
|
515
|
}
|
516
|
}
|
517
|
}
|
518
|
return $items;
|
519
|
}
|
520
|
|
521
|
/**
|
522
|
* Sort function for items order.
|
523
|
* (copied form element_sort(), which acts on #weight keys)
|
524
|
*/
|
525
|
function _field_sort_items_helper($a, $b) {
|
526
|
$a_weight = (is_array($a) ? $a['_weight'] : 0);
|
527
|
$b_weight = (is_array($b) ? $b['_weight'] : 0);
|
528
|
return $a_weight - $b_weight;
|
529
|
}
|
530
|
|
531
|
/**
|
532
|
* Same as above, using ['_weight']['#value']
|
533
|
*/
|
534
|
function _field_sort_items_value_helper($a, $b) {
|
535
|
$a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
|
536
|
$b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
|
537
|
return $a_weight - $b_weight;
|
538
|
}
|
539
|
|
540
|
/**
|
541
|
* Gets or sets administratively defined bundle settings.
|
542
|
*
|
543
|
* @param string $entity_type
|
544
|
* The type of $entity; e.g., 'node' or 'user'.
|
545
|
* @param string $bundle
|
546
|
* The bundle name.
|
547
|
* @param array|null $settings
|
548
|
* (optional) The settings to store, an associative array with the following
|
549
|
* elements:
|
550
|
* - view_modes: An associative array keyed by view mode, with the following
|
551
|
* key/value pairs:
|
552
|
* - custom_settings: Boolean specifying whether the view mode uses a
|
553
|
* dedicated set of display options (TRUE), or the 'default' options
|
554
|
* (FALSE). Defaults to FALSE.
|
555
|
* - extra_fields: An associative array containing the form and display
|
556
|
* settings for extra fields (also known as pseudo-fields):
|
557
|
* - form: An associative array whose keys are the names of extra fields,
|
558
|
* and whose values are associative arrays with the following elements:
|
559
|
* - weight: The weight of the extra field, determining its position on an
|
560
|
* entity form.
|
561
|
* - display: An associative array whose keys are the names of extra fields,
|
562
|
* and whose values are associative arrays keyed by the name of view
|
563
|
* modes. This array must include an item for the 'default' view mode.
|
564
|
* Each view mode sub-array contains the following elements:
|
565
|
* - weight: The weight of the extra field, determining its position when
|
566
|
* an entity is viewed.
|
567
|
* - visible: TRUE if the extra field is visible, FALSE otherwise.
|
568
|
*
|
569
|
* @return array|null
|
570
|
* If no $settings are passed, the current settings are returned.
|
571
|
*/
|
572
|
function field_bundle_settings($entity_type, $bundle, $settings = NULL) {
|
573
|
if (isset($settings)) {
|
574
|
variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $settings);
|
575
|
field_info_cache_clear();
|
576
|
}
|
577
|
else {
|
578
|
$settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle, array());
|
579
|
$settings += array(
|
580
|
'view_modes' => array(),
|
581
|
'extra_fields' => array(),
|
582
|
);
|
583
|
$settings['extra_fields'] += array(
|
584
|
'form' => array(),
|
585
|
'display' => array(),
|
586
|
);
|
587
|
|
588
|
return $settings;
|
589
|
}
|
590
|
}
|
591
|
|
592
|
/**
|
593
|
* Returns view mode settings in a given bundle.
|
594
|
*
|
595
|
* @param $entity_type
|
596
|
* The type of entity; e.g. 'node' or 'user'.
|
597
|
* @param $bundle
|
598
|
* The bundle name to return view mode settings for.
|
599
|
*
|
600
|
* @return
|
601
|
* An array keyed by view mode, with the following key/value pairs:
|
602
|
* - custom_settings: Boolean specifying whether the view mode uses a
|
603
|
* dedicated set of display options (TRUE), or the 'default' options
|
604
|
* (FALSE). Defaults to FALSE.
|
605
|
*/
|
606
|
function field_view_mode_settings($entity_type, $bundle) {
|
607
|
$cache = &drupal_static(__FUNCTION__, array());
|
608
|
|
609
|
if (!isset($cache[$entity_type][$bundle])) {
|
610
|
$bundle_settings = field_bundle_settings($entity_type, $bundle);
|
611
|
$settings = $bundle_settings['view_modes'];
|
612
|
// Include view modes for which nothing has been stored yet, but whose
|
613
|
// definition in hook_entity_info() specify they should use custom settings
|
614
|
// by default.
|
615
|
$entity_info = entity_get_info($entity_type);
|
616
|
foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
|
617
|
if (!isset($settings[$view_mode]['custom_settings']) && $view_mode_info['custom settings']) {
|
618
|
$settings[$view_mode]['custom_settings'] = TRUE;
|
619
|
}
|
620
|
}
|
621
|
$cache[$entity_type][$bundle] = $settings;
|
622
|
}
|
623
|
|
624
|
return $cache[$entity_type][$bundle];
|
625
|
}
|
626
|
|
627
|
/**
|
628
|
* Returns the display settings to use for an instance in a given view mode.
|
629
|
*
|
630
|
* @param $instance
|
631
|
* The field instance being displayed.
|
632
|
* @param $view_mode
|
633
|
* The view mode.
|
634
|
* @param $entity
|
635
|
* The entity being displayed.
|
636
|
*
|
637
|
* @return
|
638
|
* The display settings to be used when displaying the field values.
|
639
|
*/
|
640
|
function field_get_display($instance, $view_mode, $entity) {
|
641
|
// Check whether the view mode uses custom display settings or the 'default'
|
642
|
// mode.
|
643
|
$view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
|
644
|
$actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
|
645
|
$display = $instance['display'][$actual_mode];
|
646
|
|
647
|
// Let modules alter the display settings.
|
648
|
$context = array(
|
649
|
'entity_type' => $instance['entity_type'],
|
650
|
'field' => field_info_field($instance['field_name']),
|
651
|
'instance' => $instance,
|
652
|
'entity' => $entity,
|
653
|
'view_mode' => $view_mode,
|
654
|
);
|
655
|
drupal_alter(array('field_display', 'field_display_' . $instance['entity_type']), $display, $context);
|
656
|
|
657
|
return $display;
|
658
|
}
|
659
|
|
660
|
/**
|
661
|
* Returns the display settings to use for pseudo-fields in a given view mode.
|
662
|
*
|
663
|
* @param $entity_type
|
664
|
* The type of $entity; e.g., 'node' or 'user'.
|
665
|
* @param $bundle
|
666
|
* The bundle name.
|
667
|
* @param $view_mode
|
668
|
* The view mode.
|
669
|
*
|
670
|
* @return
|
671
|
* The display settings to be used when viewing the bundle's pseudo-fields.
|
672
|
*/
|
673
|
function field_extra_fields_get_display($entity_type, $bundle, $view_mode) {
|
674
|
// Check whether the view mode uses custom display settings or the 'default'
|
675
|
// mode.
|
676
|
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
|
677
|
$actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings'])) ? $view_mode : 'default';
|
678
|
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
|
679
|
|
680
|
$displays = array();
|
681
|
foreach ($extra_fields as $name => $value) {
|
682
|
$displays[$name] = $extra_fields[$name]['display'][$actual_mode];
|
683
|
}
|
684
|
|
685
|
// Let modules alter the display settings.
|
686
|
$context = array(
|
687
|
'entity_type' => $entity_type,
|
688
|
'bundle' => $bundle,
|
689
|
'view_mode' => $view_mode,
|
690
|
);
|
691
|
drupal_alter('field_extra_fields_display', $displays, $context);
|
692
|
|
693
|
return $displays;
|
694
|
}
|
695
|
|
696
|
/**
|
697
|
* Pre-render callback to adjust weights and visibility of non-field elements.
|
698
|
*/
|
699
|
function _field_extra_fields_pre_render($elements) {
|
700
|
$entity_type = $elements['#entity_type'];
|
701
|
$bundle = $elements['#bundle'];
|
702
|
|
703
|
if (isset($elements['#type']) && $elements['#type'] == 'form') {
|
704
|
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
|
705
|
foreach ($extra_fields as $name => $settings) {
|
706
|
if (isset($elements[$name])) {
|
707
|
$elements[$name]['#weight'] = $settings['weight'];
|
708
|
}
|
709
|
}
|
710
|
}
|
711
|
elseif (isset($elements['#view_mode'])) {
|
712
|
$view_mode = $elements['#view_mode'];
|
713
|
$extra_fields = field_extra_fields_get_display($entity_type, $bundle, $view_mode);
|
714
|
foreach ($extra_fields as $name => $settings) {
|
715
|
if (isset($elements[$name])) {
|
716
|
$elements[$name]['#weight'] = $settings['weight'];
|
717
|
// Visibility: make sure we do not accidentally show a hidden element.
|
718
|
$elements[$name]['#access'] = isset($elements[$name]['#access']) ? ($elements[$name]['#access'] && $settings['visible']) : $settings['visible'];
|
719
|
}
|
720
|
}
|
721
|
}
|
722
|
|
723
|
return $elements;
|
724
|
}
|
725
|
|
726
|
/**
|
727
|
* Clear the field info and field data caches.
|
728
|
*/
|
729
|
function field_cache_clear() {
|
730
|
cache_clear_all('*', 'cache_field', TRUE);
|
731
|
field_info_cache_clear();
|
732
|
}
|
733
|
|
734
|
/**
|
735
|
* Like filter_xss_admin(), but with a shorter list of allowed tags.
|
736
|
*
|
737
|
* Used for items entered by administrators, like field descriptions,
|
738
|
* allowed values, where some (mainly inline) mark-up may be desired
|
739
|
* (so check_plain() is not acceptable).
|
740
|
*/
|
741
|
function field_filter_xss($string) {
|
742
|
return filter_xss($string, _field_filter_xss_allowed_tags());
|
743
|
}
|
744
|
|
745
|
/**
|
746
|
* List of tags allowed by field_filter_xss().
|
747
|
*/
|
748
|
function _field_filter_xss_allowed_tags() {
|
749
|
return array('a', 'b', 'big', 'code', 'del', 'em', 'i', 'ins', 'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
|
750
|
}
|
751
|
|
752
|
/**
|
753
|
* Human-readable list of allowed tags, for display in help texts.
|
754
|
*/
|
755
|
function _field_filter_xss_display_allowed_tags() {
|
756
|
return '<' . implode('> <', _field_filter_xss_allowed_tags()) . '>';
|
757
|
}
|
758
|
|
759
|
/**
|
760
|
* Returns a renderable array for a single field value.
|
761
|
*
|
762
|
* @param $entity_type
|
763
|
* The type of $entity; e.g., 'node' or 'user'.
|
764
|
* @param $entity
|
765
|
* The entity containing the field to display. Must at least contain the id
|
766
|
* key and the field data to display.
|
767
|
* @param $field_name
|
768
|
* The name of the field to display.
|
769
|
* @param $item
|
770
|
* The field value to display, as found in
|
771
|
* $entity->field_name[$langcode][$delta].
|
772
|
* @param $display
|
773
|
* Can be either the name of a view mode, or an array of display settings.
|
774
|
* See field_view_field() for more information.
|
775
|
* @param $langcode
|
776
|
* (Optional) The language of the value in $item. If not provided, the
|
777
|
* current language will be assumed.
|
778
|
* @return
|
779
|
* A renderable array for the field value.
|
780
|
*/
|
781
|
function field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) {
|
782
|
$output = array();
|
783
|
|
784
|
if ($field = field_info_field($field_name)) {
|
785
|
// Determine the langcode that will be used by language fallback.
|
786
|
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
|
787
|
|
788
|
// Push the item as the single value for the field, and defer to
|
789
|
// field_view_field() to build the render array for the whole field.
|
790
|
$clone = clone $entity;
|
791
|
$clone->{$field_name}[$langcode] = array($item);
|
792
|
$elements = field_view_field($entity_type, $clone, $field_name, $display, $langcode);
|
793
|
|
794
|
// Extract the part of the render array we need.
|
795
|
$output = isset($elements[0]) ? $elements[0] : array();
|
796
|
if (isset($elements['#access'])) {
|
797
|
$output['#access'] = $elements['#access'];
|
798
|
}
|
799
|
}
|
800
|
|
801
|
return $output;
|
802
|
}
|
803
|
|
804
|
/**
|
805
|
* Returns a renderable array for the value of a single field in an entity.
|
806
|
*
|
807
|
* The resulting output is a fully themed field with label and multiple values.
|
808
|
*
|
809
|
* This function can be used by third-party modules that need to output an
|
810
|
* isolated field.
|
811
|
* - Do not use inside node (or any other entity) templates; use
|
812
|
* render($content[FIELD_NAME]) instead.
|
813
|
* - Do not use to display all fields in an entity; use
|
814
|
* field_attach_prepare_view() and field_attach_view() instead.
|
815
|
* - The field_view_value() function can be used to output a single formatted
|
816
|
* field value, without label or wrapping field markup.
|
817
|
*
|
818
|
* The function takes care of invoking the prepare_view steps. It also respects
|
819
|
* field access permissions.
|
820
|
*
|
821
|
* @param $entity_type
|
822
|
* The type of $entity; e.g., 'node' or 'user'.
|
823
|
* @param $entity
|
824
|
* The entity containing the field to display. Must at least contain the id
|
825
|
* key and the field data to display.
|
826
|
* @param $field_name
|
827
|
* The name of the field to display.
|
828
|
* @param $display
|
829
|
* Can be either:
|
830
|
* - The name of a view mode. The field will be displayed according to the
|
831
|
* display settings specified for this view mode in the $instance
|
832
|
* definition for the field in the entity's bundle.
|
833
|
* If no display settings are found for the view mode, the settings for
|
834
|
* the 'default' view mode will be used.
|
835
|
* - An array of display settings, as found in the 'display' entry of
|
836
|
* $instance definitions. The following key/value pairs are allowed:
|
837
|
* - label: (string) Position of the label. The default 'field' theme
|
838
|
* implementation supports the values 'inline', 'above' and 'hidden'.
|
839
|
* Defaults to 'above'.
|
840
|
* - type: (string) The formatter to use. Defaults to the
|
841
|
* 'default_formatter' for the field type, specified in
|
842
|
* hook_field_info(). The default formatter will also be used if the
|
843
|
* requested formatter is not available.
|
844
|
* - settings: (array) Settings specific to the formatter. Defaults to the
|
845
|
* formatter's default settings, specified in
|
846
|
* hook_field_formatter_info().
|
847
|
* - weight: (float) The weight to assign to the renderable element.
|
848
|
* Defaults to 0.
|
849
|
* @param $langcode
|
850
|
* (Optional) The language the field values are to be shown in. The site's
|
851
|
* current language fallback logic will be applied no values are available
|
852
|
* for the language. If no language is provided the current language will be
|
853
|
* used.
|
854
|
* @return
|
855
|
* A renderable array for the field value.
|
856
|
*
|
857
|
* @see field_view_value()
|
858
|
*/
|
859
|
function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
|
860
|
$output = array();
|
861
|
|
862
|
if ($field = field_info_field($field_name)) {
|
863
|
if (is_array($display)) {
|
864
|
// When using custom display settings, fill in default values.
|
865
|
$cache = _field_info_field_cache();
|
866
|
$display = $cache->prepareInstanceDisplay($display, $field["type"]);
|
867
|
}
|
868
|
|
869
|
// Hook invocations are done through the _field_invoke() functions in
|
870
|
// 'single field' mode, to reuse the language fallback logic.
|
871
|
// Determine the actual language to display for the field, given the
|
872
|
// languages available in the field data.
|
873
|
$display_language = field_language($entity_type, $entity, $field_name, $langcode);
|
874
|
$options = array('field_name' => $field_name, 'language' => $display_language);
|
875
|
$null = NULL;
|
876
|
|
877
|
// Invoke prepare_view steps if needed.
|
878
|
if (empty($entity->_field_view_prepared)) {
|
879
|
list($id) = entity_extract_ids($entity_type, $entity);
|
880
|
|
881
|
// First let the field types do their preparation.
|
882
|
_field_invoke_multiple('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
|
883
|
// Then let the formatters do their own specific massaging.
|
884
|
_field_invoke_multiple_default('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
|
885
|
}
|
886
|
|
887
|
// Build the renderable array.
|
888
|
$result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);
|
889
|
|
890
|
// Invoke hook_field_attach_view_alter() to let other modules alter the
|
891
|
// renderable array, as in a full field_attach_view() execution.
|
892
|
$context = array(
|
893
|
'entity_type' => $entity_type,
|
894
|
'entity' => $entity,
|
895
|
'view_mode' => '_custom',
|
896
|
'display' => $display,
|
897
|
'language' => $langcode,
|
898
|
);
|
899
|
drupal_alter('field_attach_view', $result, $context);
|
900
|
|
901
|
if (isset($result[$field_name])) {
|
902
|
$output = $result[$field_name];
|
903
|
}
|
904
|
}
|
905
|
|
906
|
return $output;
|
907
|
}
|
908
|
|
909
|
/**
|
910
|
* Returns the field items in the language they currently would be displayed.
|
911
|
*
|
912
|
* @param $entity_type
|
913
|
* The type of $entity; e.g., 'node' or 'user'.
|
914
|
* @param $entity
|
915
|
* The entity containing the data to be displayed.
|
916
|
* @param $field_name
|
917
|
* The field to be displayed.
|
918
|
* @param $langcode
|
919
|
* (optional) The language code $entity->{$field_name} has to be displayed in.
|
920
|
* Defaults to the current language.
|
921
|
*
|
922
|
* @return
|
923
|
* An array of field items keyed by delta if available, FALSE otherwise.
|
924
|
*/
|
925
|
function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
|
926
|
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
|
927
|
return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
|
928
|
}
|
929
|
|
930
|
/**
|
931
|
* Determine whether a field has any data.
|
932
|
*
|
933
|
* @param $field
|
934
|
* A field structure.
|
935
|
* @return
|
936
|
* TRUE if the field has data for any entity; FALSE otherwise.
|
937
|
*/
|
938
|
function field_has_data($field) {
|
939
|
$query = new EntityFieldQuery();
|
940
|
$query = $query->fieldCondition($field)
|
941
|
->range(0, 1)
|
942
|
->count()
|
943
|
// Neutralize the 'entity_field_access' query tag added by
|
944
|
// field_sql_storage_field_storage_query(). The result cannot depend on the
|
945
|
// access grants of the current user.
|
946
|
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
|
947
|
|
948
|
return (bool) $query
|
949
|
->execute() || (bool) $query
|
950
|
->age(FIELD_LOAD_REVISION)
|
951
|
->execute();
|
952
|
}
|
953
|
|
954
|
/**
|
955
|
* Determine whether the user has access to a given field.
|
956
|
*
|
957
|
* This function does not determine whether access is granted to the entity
|
958
|
* itself, only the specific field. Callers are responsible for ensuring that
|
959
|
* entity access is also respected. For example, when checking field access for
|
960
|
* nodes, check node_access() before checking field_access(), and when checking
|
961
|
* field access for entities using the Entity API contributed module,
|
962
|
* check entity_access() before checking field_access().
|
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
|
}
|