forked from backdrop/backdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.api.php
2804 lines (2656 loc) · 98.7 KB
/
field.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Hooks provided by the Field module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Exposes "pseudo-field" components on fieldable entities.
*
* Field UI's "Manage fields" and "Manage displays" pages let users re-order
* fields, but also non-field components. For nodes, these include the title,
* URL aliases, and other elements exposed by modules through hook_form() or
* hook_form_alter().
*
* Fieldable entities or modules that want to have their components supported
* should expose them using this hook. The user-defined settings (weight,
* visible) are automatically applied on rendered forms and displayed
* entities in a #pre_render callback added by field_attach_form() and
* field_attach_view().
*
* @see _field_extra_fields_pre_render()
* @see hook_field_extra_fields_alter()
*
* @return
* A nested array of 'pseudo-field' elements. Each list is nested within the
* following keys: entity type, bundle name, context (either 'form' or
* 'display'). The keys are the name of the elements as appearing in the
* renderable array (either the entity form or the displayed entity). The
* value is an associative array:
* - label: The human readable name of the element.
* - description: A short description of the element contents.
* - weight: The default weight of the element.
* - edit: (optional) String containing markup (normally a link) used as the
* element's 'edit' operation in the administration interface. Only for
* 'form' context.
* - delete: (optional) String containing markup (normally a link) used as the
* element's 'delete' operation in the administration interface. Only for
* 'form' context.
*
* @ingroup field_types
*/
function hook_field_extra_fields() {
$extra['node']['example'] = array(
'form' => array(
'settings' => array(
'label' => t('settings'),
'description' => t('Example module settings'),
'weight' => -3,
),
),
'display' => array(
'example_data' => array(
'label' => t('Data'),
'description' => t('Example module data'),
'weight' => 0,
),
)
);
return $extra;
}
/**
* Alter "pseudo-field" components on fieldable entities.
*
* @param $info
* The associative array of 'pseudo-field' components.
*
* @see hook_field_extra_fields()
*
* @ingroup field_types
*/
function hook_field_extra_fields_alter(&$info) {
// Force node title to always be at the top of the list by default.
foreach (node_type_get_types() as $bundle) {
if (isset($info['node'][$bundle->type]['form']['title'])) {
$info['node'][$bundle->type]['form']['title']['weight'] = -20;
}
}
}
/**
* @defgroup field_types Field Types API
* @{
* Define field types.
*
* In the Field API, each field has a type, which determines what kind of data
* (integer, string, date, etc.) the field can hold, which settings it provides,
* and so on. The data type(s) accepted by a field are defined in
* hook_field_schema(); other basic properties of a field are defined in
* hook_field_info(). The other hooks below are called by the Field Attach API
* to perform field-type-specific actions.
*
* The Field Types API also defines two kinds of pluggable handlers: widgets
* and formatters. @link field_widget Widgets @endlink specify how the field
* appears in edit forms, while @link field_formatter formatters @endlink
* specify how the field appears in displayed entities.
*
* A third kind of pluggable handlers, storage backends, is defined by the
* @link field_storage Field Storage API @endlink.
*
* See @link field Field API @endlink for information about the other parts of
* the Field API.
*/
/**
* Define Field API field types.
*
* Along with this hook, you also need to implement other hooks. See
* @link field_types Field Types API @endlink for more information.
*
* @return
* An array whose keys are field type names and whose values are arrays
* describing the field type, with the following key/value pairs:
* - label: The human-readable name of the field type.
* - description: A short description for the field type.
* - settings: An array whose keys are the names of the settings available
* for the field type, and whose values are the default values for those
* settings.
* - instance_settings: An array whose keys are the names of the settings
* available for instances of the field type, and whose values are the
* default values for those settings. Instance-level settings can have
* different values on each field instance, and thus allow greater
* flexibility than field-level settings. It is recommended to put settings
* at the instance level whenever possible. Notable exceptions: settings
* acting on the schema definition, or settings that Views needs to use
* across field instances (for example, the list of allowed values).
* - default_widget: The machine name of the default widget to be used by
* instances of this field type, when no widget is specified in the
* instance definition. This widget must be available whenever the field
* type is available (i.e. provided by the field type module, or by a module
* the field type module depends on).
* - default_formatter: The machine name of the default formatter to be used
* by instances of this field type, when no formatter is specified in the
* instance definition. This formatter must be available whenever the field
* type is available (i.e. provided by the field type module, or by a module
* the field type module depends on).
* - no_ui: (optional) A boolean specifying that users should not be allowed
* to create fields and instances of this field type through the UI. Such
* fields can only be created programmatically with field_create_field()
* and field_create_instance(). Defaults to FALSE.
*
* @see hook_field_info_alter()
*/
function hook_field_info() {
return array(
'text' => array(
'label' => t('Text'),
'description' => t('This field stores varchar text in the database.'),
'settings' => array('max_length' => 255),
'instance_settings' => array('text_processing' => 0),
'default_widget' => 'text_textfield',
'default_formatter' => 'text_default',
),
'text_long' => array(
'label' => t('Long text'),
'description' => t('This field stores long text in the database.'),
'settings' => array('max_length' => ''),
'instance_settings' => array('text_processing' => 0),
'default_widget' => 'text_textarea',
'default_formatter' => 'text_default',
),
'text_with_summary' => array(
'label' => t('Long text and summary'),
'description' => t('This field stores long text in the database along with optional summary text.'),
'settings' => array('max_length' => ''),
'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
'default_widget' => 'text_textarea_with_summary',
'default_formatter' => 'text_summary_or_trimmed',
),
);
}
/**
* Perform alterations on Field API field types.
*
* @param $info
* Array of information on field types exposed by hook_field_info()
* implementations.
*/
function hook_field_info_alter(&$info) {
// Add a setting to all field types.
foreach ($info as $field_type => $field_type_info) {
$info[$field_type]['settings'] += array(
'my_module_additional_setting' => 'default value',
);
}
// Change the default widget for fields of type 'foo'.
if (isset($info['foo'])) {
$info['foo']['default widget'] = 'my_module_widget';
}
}
/**
* Define the Field API schema for a field structure.
*
* This is invoked when a field is created, in order to obtain the database
* schema from the module that defines the field's type.
*
* This hook must be defined in the module's .install file for it to be detected
* during installation and upgrade.
*
* @param $field
* A field structure.
*
* @return
* An associative array with the following keys:
* - columns: An array of Schema API column specifications, keyed by column
* name. This specifies what comprises a value for a given field. For
* example, a value for a number field is 'value', while a value for a
* formatted text field is the combination of 'value' and 'format'. It is
* recommended to avoid having the column definitions depend on field
* settings when possible. No assumptions should be made on how storage
* engines internally use the original column name to structure their
* storage.
* - indexes: (optional) An array of Schema API indexes definitions. Only
* columns that appear in the 'columns' array are allowed. Those indexes
* will be used as default indexes. Callers of field_create_field() can
* specify additional indexes, or, at their own risk, modify the default
* indexes specified by the field-type module. Some storage engines might
* not support indexes.
* - foreign keys: (optional) An array of Schema API foreign keys
* definitions.
*/
function hook_field_schema($field) {
if ($field['type'] == 'text_long') {
$columns = array(
'value' => array(
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
),
);
}
else {
$columns = array(
'value' => array(
'type' => 'varchar',
'length' => $field['settings']['max_length'],
'not null' => FALSE,
),
);
}
$columns += array(
'format' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
);
return array(
'columns' => $columns,
'indexes' => array(
'format' => array('format'),
),
);
}
/**
* Allow modules to alter the schema for a field.
*
* @param array $schema
* The schema definition as returned by hook_field_schema().
* @param array $field
* The field definition.
*
* @see field_retrieve_schema()
*
* @since 1.26.4 Hook added.
*/
function hook_field_schema_alter(&$schema, $field) {
if ($field['type'] == 'image') {
// Alter the length of a field.
$schema['columns']['alt']['length'] = 2048;
// Add an additional column of data.
$schema['columns']['additional_column'] = array(
'description' => "Additional column added to image field table.",
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
);
// Add an additional index.
$schema['indexes']['fid_additional_column'] = array('fid', 'additional_column');
}
}
/**
* Define custom load behavior for this module's field types.
*
* Unlike most other field hooks, this hook operates on multiple entities. The
* $entities, $instances and $items parameters are arrays keyed by entity ID.
* For performance reasons, information for all available entity should be
* loaded in a single query where possible.
*
* Note that the changes made to the field values get cached by the field cache
* for subsequent loads. You should never use this hook to load fieldable
* entities, since this is likely to cause infinite recursions when
* hook_field_load() is run on those as well. Use
* hook_field_formatter_prepare_view() instead.
*
* Make changes or additions to field values by altering the $items parameter by
* reference. There is no return value.
*
* @param $entity_type
* The type of $entity.
* @param $entities
* Array of entities being loaded, keyed by entity ID.
* @param $field
* The field structure for the operation.
* @param $instances
* Array of instance structures for $field for each entity, keyed by entity
* ID.
* @param $langcode
* The language code associated with $items.
* @param $items
* Array of field values already loaded for the entities, keyed by entity ID.
* Store your changes in this parameter (passed by reference).
* @param $age
* FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
* FIELD_LOAD_REVISION to load the version indicated by each entity.
*/
function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
// Sample code from text.module: pre-compute sanitized strings so they are
// stored in the field cache.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// Only process items with a cacheable format, the rest will be handled
// by formatters if needed.
if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) {
$items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : '';
if ($field['type'] == 'text_with_summary') {
$items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : '';
}
}
}
}
}
/**
* Prepare field values prior to display.
*
* This hook is invoked before the field values are handed to formatters
* for display, and runs before the formatters' own
* hook_field_formatter_prepare_view().
*
* Unlike most other field hooks, this hook operates on multiple entities. The
* $entities, $instances and $items parameters are arrays keyed by entity ID.
* For performance reasons, information for all available entities should be
* loaded in a single query where possible.
*
* Make changes or additions to field values by altering the $items parameter by
* reference. There is no return value.
*
* @param $entity_type
* The type of $entity.
* @param $entities
* Array of entities being displayed, keyed by entity ID.
* @param $field
* The field structure for the operation.
* @param $instances
* Array of instance structures for $field for each entity, keyed by entity
* ID.
* @param $langcode
* The language associated to $items.
* @param $items
* $entity->{$field['field_name']}, or an empty array if unset.
*/
function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
// Sample code from image.module: if there are no images specified at all,
// use the default image.
foreach ($entities as $id => $entity) {
if (empty($items[$id]) && $field['settings']['default_image']) {
if ($file = file_load($field['settings']['default_image'])) {
$items[$id][0] = (array) $file + array(
'is_default' => TRUE,
'alt' => '',
'title' => '',
);
}
}
}
}
/**
* Validate this module's field data.
*
* If there are validation problems, add to the $errors array (passed by
* reference). There is no return value.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated with $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
* @param $errors
* The array of errors (keyed by field name, language code, and delta) that
* have already been reported for the entity. The function should add its
* errors to this array. Each error is an associative array with the following
* keys and values:
* - error: An error code (should be a string prefixed with the module name).
* - message: The human readable message to be displayed.
*/
function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['value'])) {
if (!empty($field['settings']['max_length']) && backdrop_strlen($item['value']) > $field['settings']['max_length']) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'text_max_length',
'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
);
}
}
}
}
/**
* Define custom presave behavior for this module's field types.
*
* Make changes or additions to field values by altering the $items parameter by
* reference. There is no return value.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated with $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
*/
function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($field['type'] == 'number_decimal') {
// Let PHP round the value to ensure consistent behavior across storage
// backends.
foreach ($items as $delta => $item) {
if (isset($item['value'])) {
$items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
}
}
}
}
/**
* Define custom insert behavior for this module's field data.
*
* This hook is invoked from field_attach_insert() on the module that defines a
* field, during the process of inserting an entity object (node, taxonomy term,
* etc.). It is invoked just before the data for this field on the particular
* entity object is inserted into field storage. Only field modules that are
* storing or tracking information outside the standard field storage mechanism
* need to implement this hook.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated with $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
*
* @see hook_field_update()
* @see hook_field_delete()
*/
function hook_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
if (config_get('taxonomy.settings', 'maintain_index_table') && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) {
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
foreach ($items as $item) {
$query->values(array(
'nid' => $entity->nid,
'tid' => $item['tid'],
'sticky' => $entity->sticky,
'created' => $entity->created,
));
}
$query->execute();
}
}
/**
* Define custom update behavior for this module's field data.
*
* This hook is invoked from field_attach_update() on the module that defines a
* field, during the process of updating an entity object (node, taxonomy term,
* etc.). It is invoked just before the data for this field on the particular
* entity object is updated into field storage. Only field modules that are
* storing or tracking information outside the standard field storage mechanism
* need to implement this hook.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated with $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
*
* @see hook_field_insert()
* @see hook_field_delete()
*/
function hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
if (config_get('taxonomy.settings', 'maintain_index_table') && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') {
$first_call = &backdrop_static(__FUNCTION__, array());
// We don't maintain data for old revisions, so clear all previous values
// from the table. Since this hook runs once per field, per object, make
// sure we only wipe values once.
if (!isset($first_call[$entity->nid])) {
$first_call[$entity->nid] = FALSE;
db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
}
// Only save data to the table if the node is published.
if ($entity->status) {
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
foreach ($items as $item) {
$query->values(array(
'nid' => $entity->nid,
'tid' => $item['tid'],
'sticky' => $entity->sticky,
'created' => $entity->created,
));
}
$query->execute();
}
}
}
/**
* Update the storage information for a field.
*
* This is invoked on the field's storage module from field_update_field(),
* before the new field information is saved to the database. The field storage
* module should update its storage tables to agree with the new field
* information. If there is a problem, the field storage module should throw an
* exception.
*
* @param $field
* The updated field structure to be saved.
* @param $prior_field
* The previously-saved field structure.
* @param $has_data
* TRUE if the field has data in storage currently.
*/
function hook_field_storage_update_field($field, $prior_field, $has_data) {
if (!$has_data) {
// There is no data. Re-create the tables completely.
$prior_schema = _field_sql_storage_schema($prior_field);
foreach ($prior_schema as $name => $table) {
db_drop_table($name, $table);
}
$schema = _field_sql_storage_schema($field);
foreach ($schema as $name => $table) {
db_create_table($name, $table);
}
}
else {
// There is data. See field_sql_storage_field_storage_update_field() for
// an example of what to do to modify the schema in place, preserving the
// old data as much as possible.
}
backdrop_get_schema(NULL, TRUE);
}
/**
* Define custom delete behavior for this module's field data.
*
* This hook is invoked from field_attach_delete() on the module that defines a
* field, during the process of deleting an entity object (node, taxonomy term,
* etc.). It is invoked just before the data for this field on the particular
* entity object is deleted from field storage. Only field modules that are
* storing or tracking information outside the standard field storage mechanism
* need to implement this hook.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated with $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
*
* @see hook_field_insert()
* @see hook_field_update()
*/
function hook_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
foreach ($items as $delta => $item) {
// For hook_file_references(), remember that this is being deleted.
$item['file_field_name'] = $field['field_name'];
// Pass in the ID of the object that is being removed so all references can
// be counted in hook_file_references().
$item['file_field_type'] = $entity_type;
$item['file_field_id'] = $id;
file_field_delete_file($item, $field, $entity_type, $id);
}
}
/**
* Define custom revision delete behavior for this module's field types.
*
* This hook is invoked just before the data is deleted from field storage
* in field_attach_delete_revision(), and will only be called for fieldable
* types that are versioned.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated with $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
*/
function hook_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
foreach ($items as $delta => $item) {
// For hook_file_references, remember that this file is being deleted.
$item['file_field_name'] = $field['field_name'];
if (file_field_delete_file($item, $field, $entity_type, $id)) {
$items[$delta] = NULL;
}
}
}
/**
* Define custom prepare_translation behavior for this module's field types.
*
* @param $entity_type
* The type of $entity.
* @param $entity
* The entity for the operation.
* @param $field
* The field structure for the operation.
* @param $instance
* The instance structure for $field on $entity's bundle.
* @param $langcode
* The language associated to $items.
* @param $items
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
* @param $source_entity
* The source entity from which field values are being copied.
* @param $source_langcode
* The source language from which field values are being copied.
*
* @ingroup field_language
*/
function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
// If the translating user is not permitted to use the assigned text format,
// we must not expose the source values.
$field_name = $field['field_name'];
$formats = filter_formats();
$format_id = $source_entity->{$field_name}[$source_langcode][0]['format'];
if (!filter_access($formats[$format_id])) {
$items = array();
}
}
/**
* Define what constitutes an empty item for a field type.
*
* @param $item
* An item that may or may not be empty.
* @param $field
* The field to which $item belongs.
*
* @return
* TRUE if $field's type considers $item not to contain any data;
* FALSE otherwise.
*/
function hook_field_is_empty($item, $field) {
if (empty($item['value']) && (string) $item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
/**
* @} End of "defgroup field_types".
*/
/**
* @defgroup field_widget Field Widget API
* @{
* Define Field API widget types.
*
* Field API widgets specify how fields are displayed in edit forms. Fields of a
* given @link field_types field type @endlink may be edited using more than one
* widget. In this case, the Field UI module allows the site builder to choose
* which widget to use. Widget types are defined by implementing
* hook_field_widget_info().
*
* Widgets are @link forms_api_reference.html Form API @endlink elements with
* additional processing capabilities. Widget hooks are typically called by the
* Field Attach API during the creation of the field form structure with
* field_attach_form().
*
* @see field
* @see field_types
* @see field_formatter
*/
/**
* Expose Field API widget types.
*
* @return
* An array describing the widget types implemented by the module.
* The keys are widget type names. To avoid name clashes, widget type
* names should be prefixed with the name of the module that exposes them.
* The values are arrays describing the widget type, with the following
* key/value pairs:
* - label: The human-readable name of the widget type.
* - description: A short description for the widget type.
* - field types: An array of field types the widget supports.
* - settings: An array whose keys are the names of the settings available
* for the widget type, and whose values are the default values for those
* settings.
* - behaviors: (optional) An array describing behaviors of the widget, with
* the following elements:
* - multiple values: One of the following constants:
* - FIELD_BEHAVIOR_DEFAULT: (default) If the widget allows the input of
* one single field value (most common case). The widget will be
* repeated for each value input.
* - FIELD_BEHAVIOR_CUSTOM: If one single copy of the widget can receive
* several field values. Examples: checkboxes, multiple select,
* comma-separated textfield.
* - default value: One of the following constants:
* - FIELD_BEHAVIOR_DEFAULT: (default) If the widget accepts default
* values.
* - FIELD_BEHAVIOR_NONE: if the widget does not support default values.
* - weight: (optional) An integer to determine the weight of this widget
* relative to other widgets in the Field UI when selecting a widget for a
* given field instance.
*
* @see hook_field_widget_info_alter()
* @see hook_field_widget_form()
* @see hook_field_widget_form_alter()
* @see hook_field_widget_WIDGET_TYPE_form_alter()
* @see hook_field_widget_error()
* @see hook_field_widget_settings_form()
*/
function hook_field_widget_info() {
return array(
'text_textfield' => array(
'label' => t('Text field'),
'field types' => array('text'),
'settings' => array('size' => 60),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
'text_textarea' => array(
'label' => t('Text area (multiple rows)'),
'field types' => array('text_long'),
'settings' => array('rows' => 5),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
'text_textarea_with_summary' => array(
'label' => t('Text area with a summary'),
'field types' => array('text_with_summary'),
'settings' => array('rows' => 10, 'summary_rows' => 5),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
// As an advanced widget, force it to sink to the bottom of the choices.
'weight' => 2,
),
);
}
/**
* Perform alterations on Field API widget types.
*
* @param $info
* Array of informations on widget types exposed by hook_field_widget_info()
* implementations.
*/
function hook_field_widget_info_alter(&$info) {
// Add a setting to a widget type.
$info['text_textfield']['settings'] += array(
'my_module_additional_setting' => 'default value',
);
// Let a new field type re-use an existing widget.
$info['options_select']['field types'][] = 'my_field_type';
}
/**
* Return the form for a single field widget.
*
* Field widget form elements should be based on the passed-in $element, which
* contains the base form element properties derived from the field
* configuration.
*
* Field API will set the weight, field name and delta values for each form
* element. If there are multiple values for this field, the Field API will
* invoke this hook as many times as needed.
*
* Note that, depending on the context in which the widget is being included
* (regular entity form, field configuration form, advanced search form...),
* the values for $field and $instance might be different from the "official"
* definitions returned by field_info_field() and field_info_instance().
* Examples: mono-value widget even if the field is multi-valued, non-required
* widget even if the field is 'required'...
*
* Therefore, the FAPI element callbacks (such as #process, #element_validate,
* #value_callback...) used by the widget cannot use the field_info_field()
* or field_info_instance() functions to retrieve the $field or $instance
* definitions they should operate on. The field_widget_field() and
* field_widget_instance() functions should be used instead to fetch the
* current working definitions from $form_state, where Field API stores them.
*
* Alternatively, hook_field_widget_form() can extract the needed specific
* properties from $field and $instance and set them as ad-hoc
* $element['#custom'] properties, for later use by its element callbacks.
*
* Other modules may alter the form element provided by this function using
* hook_field_widget_form_alter().
*
* @param $form
* The form structure where widgets are being attached to. This might be a
* full form structure, or a sub-element of a larger form.
* @param $form_state
* An associative array containing the current state of the form.
* @param $field
* The field structure.
* @param $instance
* The field instance.
* @param $langcode
* The language associated with $items.
* @param $items
* Array of default values for this field.
* @param $delta
* The order of this item in the array of subelements (0, 1, 2, etc).
* @param $element
* A form element array containing basic properties for the widget:
* - #entity_type: The name of the entity the field is attached to.
* - #bundle: The name of the field bundle the field is contained in.
* - #field_name: The name of the field.
* - #language: The language the field is being edited in.
* - #field_parents: The 'parents' space for the field in the form. Most
* widgets can overlook this property. This identifies the location where
* the field values are placed within $form_state['values'], and is used
* to access processing information for the field through the
* field_form_get_state() and field_form_set_state() functions.
* - #columns: A list of field storage columns of the field.
* - #title: The sanitized element label for the field instance, ready for
* output.
* - #description: The sanitized element description for the field instance,
* ready for output.
* - #required: A Boolean indicating whether the element value is required;
* for required multiple value fields, only the first widget's values are
* required.
* - #delta: The order of this item in the array of subelements; see $delta
* above.
*
* @return
* The form elements for a single widget for this field.
*
* @see field_widget_field()
* @see field_widget_instance()
* @see hook_field_widget_form_alter()
* @see hook_field_widget_WIDGET_TYPE_form_alter()
*/
function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return array('value' => $element);
}
/**
* Alter forms for field widgets provided by other modules.
*
* @param $element
* The field widget form element as constructed by hook_field_widget_form().
* @param $form_state
* An associative array containing the current state of the form.
* @param $context
* An associative array containing the following key-value pairs, matching the
* arguments received by hook_field_widget_form():
* - form: The form structure to which widgets are being attached. This may be
* a full form structure, or a sub-element of a larger form.
* - field: The field structure.
* - instance: The field instance structure.
* - langcode: The language associated with $items.
* - items: Array of default values for this field.
* - delta: The order of this item in the array of subelements (0, 1, 2, etc).
*
* @see hook_field_widget_form()
* @see hook_field_widget_WIDGET_TYPE_form_alter()
*/
function hook_field_widget_form_alter(&$element, &$form_state, $context) {
// Add a css class to widget form elements for all fields of type mytype.
if ($context['field']['type'] == 'mytype') {
// Be sure not to overwrite existing attributes.
$element['#attributes']['class'][] = 'myclass';
}
}
/**
* Alter widget forms for a specific widget provided by another module.
*
* Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
* specific widget form, rather than using hook_field_widget_form_alter() and
* checking the widget type.
*
* @param $element
* The field widget form element as constructed by hook_field_widget_form().
* @param $form_state
* An associative array containing the current state of the form.
* @param $context
* An associative array containing the following key-value pairs, matching the
* arguments received by hook_field_widget_form():
* - "form": The form structure where widgets are being attached to. This
* might be a full form structure, or a sub-element of a larger form.
* - "field": The field structure.
* - "instance": The field instance structure.
* - "langcode": The language associated with $items.
* - "items": Array of default values for this field.
* - "delta": The order of this item in the array of subelements (0, 1, 2,
* etc).
*
* @see hook_field_widget_form()
* @see hook_field_widget_form_alter()
*/
function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) {
// Code here will only act on widgets of type WIDGET_TYPE. For example,
// hook_field_widget_my_module_autocomplete_form_alter() will only act on
// widgets of type 'my_module_autocomplete'.
$element['#autocomplete_path'] = 'my_module/autocomplete_path';
}
/**
* Alters the widget properties of a field instance before it gets displayed.
*
* Note that instead of hook_field_widget_properties_alter(), which is called
* for all fields on all entity types,
* hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget
* properties for fields on a specific entity type only.
*
* This hook is called once per field per added or edit entity. If the result
* of the hook involves reading from the database, it is highly recommended to
* statically cache the information.
*
* @param $widget
* The instance's widget properties.
* @param $context
* An associative array containing:
* - entity_type: The entity type; e.g., 'node' or 'user'.
* - entity: The entity object.
* - field: The field that the widget belongs to.
* - instance: The instance of the field.
*
* @see hook_field_widget_properties_ENTITY_TYPE_alter()
*/
function hook_field_widget_properties_alter(&$widget, $context) {
// Change a widget's type according to the time of day.
$field = $context['field'];
if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') {