-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontent.module
2711 lines (2506 loc) · 93.4 KB
/
content.module
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
* Allows administrators to associate custom fields to content types.
*/
define('CONTENT_DB_STORAGE_PER_FIELD', 0);
define('CONTENT_DB_STORAGE_PER_CONTENT_TYPE', 1);
define('CONTENT_CALLBACK_NONE', 0x0001);
define('CONTENT_CALLBACK_DEFAULT', 0x0002);
define('CONTENT_CALLBACK_CUSTOM', 0x0004);
define('CONTENT_HANDLE_CORE', 0x0001);
define('CONTENT_HANDLE_MODULE', 0x0002);
// We do not use hook_init(), since that hook is not fired in update.php, and we
// need token generation to be active within hook_update_N() (e.g. for
// node_save() calls.)
require_once dirname(__FILE__) . '/includes/content.token.inc';
function content_help($path, $arg) {
switch ($path) {
case 'admin/help#content':
$output = '<p>'. t('The content module, a required component of the Content Construction Kit (CCK), allows administrators to associate custom fields with content types. In Drupal, content types are used to define the characteristics of a post, including the title and description of the fields displayed on its add and edit pages. Using the content module (and the other helper modules included in CCK), custom fields beyond the default "Title" and "Body" may be added. CCK features are accessible through tabs on the <a href="@content-types">content types administration page</a>. (See the <a href="@node-help">node module help page</a> for more information about content types.)', array('@content-types' => url('admin/content/types'), '@node-help' => url('admin/help/node'))) .'</p>';
$output .= '<p>'. t('When adding a custom field to a content type, you determine its type (whether it will contain text, numbers, or references to other objects) and how it will be displayed (either as a text field or area, a select box, checkbox, radio button, or autocompleting field). A field may have multiple values (i.e., a "person" may have multiple e-mail addresses) or a single value (i.e., an "employee" has a single employee identification number). As you add and edit fields, CCK automatically adjusts the structure of the database as necessary. CCK also provides a number of other features, including intelligent caching for your custom data, an import and export facility for content type definitions, and integration with other contributed modules.') .'</p>';
$output .= '<p>'. t('Custom field types are provided by a set of optional modules included with CCK (each module provides a different type). The <a href="@modules">modules page</a> allows you to enable or disable CCK components. A default installation of CCK includes:', array('@modules' => url('admin/build/modules'))) .'</p>';
$output .= '<ul>';
$output .= '<li>'. t('<em>number</em>, which adds numeric field types, in integer, decimal or floating point form. You may define a set of allowed inputs, or specify an allowable range of values. A variety of common formats for displaying numeric data are available.') .'</li>';
$output .= '<li>'. t("<em>text</em>, which adds text field types. A text field may contain plain text only, or optionally, may use Drupal's input format filters to securely manage rich text input. Text input fields may be either a single line (text field), multiple lines (text area), or for greater input control, a select box, checkbox, or radio buttons. If desired, CCK can validate the input to a set of allowed values.") .'</li>';
$output .= '<li>'. t('<em>nodereference</em>, which creates custom references between Drupal nodes. By adding a <em>nodereference</em> field and two different content types, for instance, you can easily create complex parent/child relationships between data (multiple "employee" nodes may contain a <em>nodereference</em> field linking to an "employer" node).') .'</li>';
$output .= '<li>'. t('<em>userreference</em>, which creates custom references to your sites\' user accounts. By adding a <em>userreference</em> field, you can create complex relationships between your site\'s users and posts. To track user involvement in a post beyond Drupal\'s standard <em>Authored by</em> field, for instance, add a <em>userreference</em> field named "Edited by" to a content type to store a link to an editor\'s user account page.') .'</li>';
$output .= '<li>'. t('<em>fieldgroup</em>, which creates collapsible fieldsets to hold a group of related fields. A fieldset may either be open or closed by default. The order of your fieldsets, and the order of fields within a fieldset, is managed via a drag-and-drop interface provided by content module.') .'</li>';
$output .= '</ul>';
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@handbook-cck">CCK</a> or the <a href="@project-cck">CCK project page</a>.', array('@handbook-cck' => 'http://drupal.org/handbook/modules/cck', '@project-cck' => 'http://drupal.org/project/cck')) .'</p>';
return $output;
}
}
/**
* Implementation of hook_flush_caches.
*/
function content_flush_caches() {
return array(content_cache_tablename());
}
/**
* Implementation of hook_init().
*/
function content_init() {
drupal_add_css(drupal_get_path('module', 'content') .'/theme/content-module.css');
if (module_exists('diff') && !function_exists('content_diff')) {
module_load_include('inc', 'content', 'includes/content.diff');
}
}
/**
* Implementation of hook_perm().
*/
function content_perm() {
return array('Use PHP input for field settings (dangerous - grant with care)');
}
/**
* Implementation of hook_menu_alter().
*/
function content_menu_alter(&$items) {
// Customize the content types page with our own callback
$items['admin/content/types']['page callback'] = 'content_types_overview';
$items['admin/content/types']['file'] = 'content.admin.inc';
$items['admin/content/types']['file path'] = drupal_get_path('module', 'content') .'/includes';
}
/**
* Implementation of hook_menu().
*/
function content_menu() {
$items = array();
$items['admin/content/types/fields'] = array(
'title' => 'Fields',
'page callback' => 'content_fields_list',
'access arguments' => array('administer content types'),
'file' => 'includes/content.admin.inc',
'type' => MENU_LOCAL_TASK,
);
// Callback for AHAH add more buttons.
$items['content/js_add_more'] = array(
'page callback' => 'content_add_more_js',
'access arguments' => array('access content'),
'file' => 'includes/content.node_form.inc',
'type' => MENU_CALLBACK,
);
// Make sure this doesn't fire until content_types is working,
// and tables are updated, needed to avoid errors on initial installation.
if (!defined('MAINTENANCE_MODE') && variable_get('content_schema_version', -1) >= 6007) {
foreach (node_get_types() as $type) {
$type_name = $type->type;
$content_type = content_types($type_name);
$type_url_str = $content_type['url_str'];
$items['admin/content/node-type/'. $type_url_str .'/fields'] = array(
'title' => 'Manage fields',
'page callback' => 'drupal_get_form',
'page arguments' => array('content_field_overview_form', $type_name),
'access arguments' => array('administer content types'),
'file' => 'includes/content.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
$items['admin/content/node-type/'. $type_url_str .'/display'] = array(
'title' => 'Display fields',
'page callback' => 'drupal_get_form',
'page arguments' => array('content_display_overview_form', $type_name),
'access arguments' => array('administer content types'),
'file' => 'includes/content.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$contexts = content_build_modes('_tabs');
foreach ($contexts as $key => $tab) {
$items['admin/content/node-type/'. $type_url_str .'/display/'. $key] = array(
'title' => $tab['title'],
'page arguments' => array('content_display_overview_form', $type_name, $key),
'access arguments' => array('administer content types'),
'type' => $key == 'basic' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
'weight' => $key == 'basic' ? 0 : 1,
);
}
// Cast as an array in case this is called before any fields have
// been added, like when a new content type is created.
foreach ((array) $content_type['fields'] as $field) {
$field_name = $field['field_name'];
$items['admin/content/node-type/'. $type_url_str .'/fields/'. $field_name] = array(
'title' => $field['widget']['label'],
'page callback' => 'drupal_get_form',
'page arguments' => array('content_field_edit_form', $type_name, $field_name),
'access arguments' => array('administer content types'),
'file' => 'includes/content.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/content/node-type/'. $type_url_str .'/fields/'. $field_name .'/remove'] = array(
'title' => 'Remove field',
'page callback' => 'drupal_get_form',
'page arguments' => array('content_field_remove_form', $type_name, $field_name),
'access arguments' => array('administer content types'),
'file' => 'includes/content.admin.inc',
'type' => MENU_CALLBACK,
);
}
}
}
return $items;
}
/**
* Hook elements().
*
* Used to add multiple value processing, validation, and themes.
*
* FAPI callbacks can be declared here, and the element will be
* passed to those callbacks.
*
* Drupal will automatically theme the element using a theme with
* the same name as the hook_elements key.
*/
function content_elements() {
return array(
'content_multiple_values' => array(),
'content_field' => array(),
);
}
/**
* Implementation of hook_theme().
*/
function content_theme() {
$path = drupal_get_path('module', 'content') .'/theme';
module_load_include('inc', 'content', 'theme/theme');
return array(
'content_field' => array(
'template' => 'content-field',
'arguments' => array('element' => NULL),
'path' => $path,
),
'content_overview_links' => array(
'arguments' => array(),
),
'content_field_overview_form' => array(
'template' => 'content-admin-field-overview-form',
'file' => 'theme.inc',
'path' => $path,
'arguments' => array('form' => NULL),
),
'content_display_overview_form' => array(
'template' => 'content-admin-display-overview-form',
'file' => 'theme.inc',
'path' => $path,
'arguments' => array('form' => NULL),
),
'content_exclude' => array(
'arguments' => array('content' => NULL, 'object' => array(), 'context' => NULL),
),
'content_view_multiple_field' => array(
'arguments' => array('items' => NULL, 'field' => NULL, 'data' => NULL),
),
'content_multiple_values' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Implementation of hook_views_api().
*/
function content_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'content') . '/includes/views',
);
}
/**
* Implementation of hook_ctools_plugin_directory().
*/
function content_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && $plugin == 'content_types') {
return 'includes/panels/' . $plugin;
}
}
/**
* Load data for a node type's fields.
* Implementation of hook_nodeapi 'load' op.
*
* When loading one of the content.module nodes, we need to let each field handle
* its own loading. This can make for a number of queries in some cases, so we
* cache the loaded object structure and invalidate it during the update process.
*/
function content_load(&$node) {
$cid = 'content:'. $node->nid .':'. $node->vid;
if ($cached = cache_get($cid, content_cache_tablename())) {
foreach ($cached->data as $key => $value) {
$node->$key = $value;
}
}
else {
$default_additions = _content_field_invoke_default('load', $node);
if ($default_additions) {
foreach ($default_additions as $key => $value) {
$node->$key = $value;
}
}
$additions = _content_field_invoke('load', $node);
if ($additions) {
foreach ($additions as $key => $value) {
$node->$key = $value;
$default_additions[$key] = $value;
}
}
cache_set($cid, $default_additions, content_cache_tablename());
}
}
/**
* Implementation of hook_nodeapi 'validate' op.
*
*/
function content_validate(&$node, $form = NULL) {
_content_field_invoke('validate', $node, $form);
_content_field_invoke_default('validate', $node, $form);
}
/**
* Implementation of hook_nodeapi 'presave' op.
*
*/
function content_presave(&$node) {
_content_field_invoke('presave', $node);
_content_field_invoke_default('presave', $node);
}
/**
* Implementation of hook_nodeapi 'insert' op.
*
* Insert node type fields.
*/
function content_insert(&$node) {
_content_field_invoke('insert', $node);
_content_field_invoke_default('insert', $node);
}
/**
* Implementation of hook_nodeapi 'update' op.
*
* Update node type fields.
*/
function content_update(&$node) {
_content_field_invoke('update', $node);
_content_field_invoke_default('update', $node);
cache_clear_all('content:'. $node->nid .':'. $node->vid, content_cache_tablename());
}
/**
* Implementation of hook_nodeapi 'delete' op.
*
* Delete node type fields.
*/
function content_delete(&$node) {
_content_field_invoke('delete', $node);
_content_field_invoke_default('delete', $node);
cache_clear_all('content:'. $node->nid .':', content_cache_tablename(), TRUE);
}
/**
* Implementation of hook_nodeapi 'delete_revision' op.
*
* Delete node type fields for a revision.
*/
function content_delete_revision(&$node) {
_content_field_invoke('delete revision', $node);
_content_field_invoke_default('delete revision', $node);
cache_clear_all('content:'. $node->nid .':'. $node->vid, content_cache_tablename());
}
/**
* Implementation of hook_nodeapi 'view' op.
*
* Generate field render arrays.
*/
function content_view(&$node, $teaser = FALSE, $page = FALSE) {
// Let field modules sanitize their data for output.
_content_field_invoke('sanitize', $node, $teaser, $page);
// Merge fields.
$additions = _content_field_invoke_default('view', $node, $teaser, $page);
$old = isset($node->content) ? (array) $node->content : array();
$node->content = array_merge($old, $additions);
}
/**
* Render a single field, fully themed with label and multiple values.
*
* To be used by third-party code (Views, Panels...) that needs to output
* an isolated field. Do *not* use inside node templates, use the
* $FIELD_NAME_rendered variables instead.
*
* By default, the field is displayed using the settings defined for the
* 'full node' or 'teaser' contexts (depending on the value of the $teaser param).
* Set $node->build_mode to a different value to use a different context.
*
* Different settings can be specified by adjusting $field['display_settings'].
*
* @param $field
* The field definition.
* @param $node
* The node containing the field to display. Can be a 'pseudo-node', containing
* at least 'type', 'nid', 'vid', and the field data.
* @param $teaser
* @param $page
* Similar to hook_nodeapi('view')
* @return
* The themed output for the field.
*/
function content_view_field($field, $node, $teaser = FALSE, $page = FALSE) {
$output = '';
if (isset($node->{$field['field_name']})) {
$items = $node->{$field['field_name']};
// Use 'full'/'teaser' if not specified otherwise.
$node->build_mode = isset($node->build_mode) ? $node->build_mode : NODE_BUILD_NORMAL;
// One-field equivalent to _content_field_invoke('sanitize').
$field_types = _content_field_types();
$module = $field_types[$field['type']]['module'];
$function = $module .'_field';
if (function_exists($function)) {
$function('sanitize', $node, $field, $items, $teaser, $page);
$node->{$field['field_name']} = $items;
}
$view = content_field('view', $node, $field, $items, $teaser, $page);
// content_field('view') adds a wrapper to handle variables and 'excluded'
// fields for node templates. We bypass it and render the actual field.
$output = drupal_render($view[$field['field_name']]['field']);
}
return $output;
}
/**
* Implementation of hook_nodeapi 'alter' op.
*
* Add back the formatted values in the 'view' element for all fields,
* so that node templates can use it.
*/
function content_alter(&$node, $teaser = FALSE, $page = FALSE) {
_content_field_invoke_default('alter', $node, $teaser, $page);
}
/**
* Implementation of hook_nodeapi 'prepare translation' op.
*
* Generate field render arrays.
*/
function content_prepare_translation(&$node) {
$default_additions = _content_field_invoke_default('prepare translation', $node);
$additions = _content_field_invoke('prepare translation', $node);
// Merge module additions after the default ones to enable overriding
// of field values.
$node = (object) array_merge((array) $node, $default_additions, $additions);
}
/**
* Implementation of hook_nodeapi().
*/
function content_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
// Prevent against invalid 'nodes' built by broken 3rd party code.
if (isset($node->type)) {
$type = content_types($node->type);
// Save cycles if the type has no CCK fields.
if (!empty($type['fields'])) {
$callback = 'content_'. str_replace(' ', '_', $op);
if (function_exists($callback)) {
$callback($node, $a3, $a4);
}
}
// Special case for 'view' op, we want to adjust weights of non-cck fields
// even if there are no actual fields for this type.
if ($op == 'view') {
$node->content['#pre_render'][] = 'content_alter_extra_weights';
$node->content['#content_extra_fields'] = $type['extra'];
}
}
}
/**
* Implementation of hook_form_alter().
*/
function content_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
$type = content_types($form['#node']->type);
if (!empty($type['fields'])) {
module_load_include('inc', 'content', 'includes/content.node_form');
// Merge field widgets.
$form = array_merge($form, content_form($form, $form_state));
}
$form['#pre_render'][] = 'content_alter_extra_weights';
$form['#content_extra_fields'] = $type['extra'];
}
}
/**
* Pre-render callback to adjust weights of non-CCK fields.
*/
function content_alter_extra_weights($elements) {
if (isset($elements['#content_extra_fields'])) {
foreach ($elements['#content_extra_fields'] as $key => $value) {
// Some core 'fields' use a different key in node forms and in 'view'
// render arrays. Check we're not on a form first.
if (!isset($elements['#build_id']) && isset($value['view']) && isset($elements[$value['view']])) {
$elements[$value['view']]['#weight'] = $value['weight'];
}
elseif (isset($elements[$key])) {
$elements[$key]['#weight'] = $value['weight'];
}
}
}
return $elements;
}
/**
* Proxy function to call content_add_more_submit(), because it might not be
* included yet when the form is processed and invokes the callback.
*/
function content_add_more_submit_proxy($form, &$form_state) {
module_load_include('inc', 'content', 'includes/content.node_form');
content_add_more_submit($form, $form_state);
}
/**
* Theme an individual form element.
*
* Combine multiple values into a table with drag-n-drop reordering.
*/
function theme_content_multiple_values($element) {
$field_name = $element['#field_name'];
$field = content_fields($field_name);
$output = '';
if ($field['multiple'] >= 1) {
$table_id = $element['#field_name'] .'_values';
$order_class = $element['#field_name'] .'-delta-order';
$required = !empty($element['#required']) ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';
$header = array(
array(
'data' => t('!title: !required', array('!title' => $element['#title'], '!required' => $required)),
'colspan' => 2
),
t('Order'),
);
$rows = array();
// Sort items according to '_weight' (needed when the form comes back after
// preview or failed validation)
$items = array();
foreach (element_children($element) as $key) {
if ($key !== $element['#field_name'] .'_add_more') {
$items[] = &$element[$key];
}
}
usort($items, '_content_sort_items_value_helper');
// Add the items as table rows.
foreach ($items as $key => $item) {
$item['_weight']['#attributes']['class'] = $order_class;
$delta_element = drupal_render($item['_weight']);
$cells = array(
array('data' => '', 'class' => 'content-multiple-drag'),
drupal_render($item),
array('data' => $delta_element, 'class' => 'delta-order'),
);
$rows[] = array(
'data' => $cells,
'class' => 'draggable',
);
}
$output .= theme('table', $header, $rows, array('id' => $table_id, 'class' => 'content-multiple-table'));
$output .= $element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '';
$output .= drupal_render($element[$element['#field_name'] .'_add_more']);
drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
}
else {
foreach (element_children($element) as $key) {
$output .= drupal_render($element[$key]);
}
}
return $output;
}
/**
* Modules notify Content module when uninstalled, disabled, etc.
*
* @param string $op
* the module operation: uninstall, install, enable, disable
* @param string $module
* the name of the affected module.
* @TODO
* figure out exactly what needs to be done by content module when
* field modules are installed, uninstalled, enabled or disabled.
*/
function content_notify($op, $module) {
switch ($op) {
case 'install':
content_clear_type_cache();
break;
case 'uninstall':
module_load_include('inc', 'content', 'includes/content.crud');
content_module_delete($module);
break;
case 'enable':
content_associate_fields($module);
content_clear_type_cache();
break;
case 'disable':
// When CCK modules are disabled before content module's update is run
// to add the active column, we can't do this.
if (variable_get('content_schema_version', -1) < 6007) {
return FALSE;
}
db_query("UPDATE {". content_field_tablename() ."} SET active=0 WHERE module='%s'", $module);
db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=0 WHERE widget_module='%s'", $module);
content_clear_type_cache(TRUE);
break;
}
}
/**
* Allows a module to update the database for fields and columns it controls.
*
* @param string $module
* The name of the module to update on.
*/
function content_associate_fields($module) {
// When CCK modules are enabled before content module's update is run,
// to add module and active columns, we can't do this.
if (variable_get('content_schema_version', -1) < 6007) {
return FALSE;
}
$module_fields = module_invoke($module, 'field_info');
if ($module_fields) {
foreach ($module_fields as $name => $field_info) {
watchdog('content', 'Updating field type %type with module %module.', array('%type' => $name, '%module' => $module));
db_query("UPDATE {". content_field_tablename() ."} SET module = '%s', active = %d WHERE type = '%s'", $module, 1, $name);
}
}
$module_widgets = module_invoke($module, 'widget_info');
if ($module_widgets) {
foreach ($module_widgets as $name => $widget_info) {
watchdog('content', 'Updating widget type %type with module %module.', array('%type' => $name, '%module' => $module));
db_query("UPDATE {". content_instance_tablename() ."} SET widget_module = '%s', widget_active = %d WHERE widget_type = '%s'", $module, 1, $name);
}
}
// This is called from updates and installs, so get the install-safe
// version of a fields array.
$fields_set = array();
module_load_include('install', 'content');
$types = content_types_install();
foreach ($types as $type_name => $fields) {
foreach ($fields as $field) {
if ($field['module'] == $module && !in_array($field['field_name'], $fields_set)) {
$columns = (array) module_invoke($field['module'], 'field_settings', 'database columns', $field);
db_query("UPDATE {". content_field_tablename() ."} SET db_columns = '%s' WHERE field_name = '%s'", serialize($columns), $field['field_name']);
$fields_set[] = $field['field_name'];
}
}
}
}
/**
* Implementation of hook_field(). Handles common field housekeeping.
*
* This implementation is special, as content.module does not define any field
* types. Instead, this function gets called after the type-specific hook, and
* takes care of default stuff common to all field types.
*
* Db-storage ops ('load', 'insert', 'update', 'delete', 'delete revisions')
* are not executed field by field, and are thus handled separately in
* content_storage.
*
* The 'view' operation constructs the $node in a way that you can use
* drupal_render() to display the formatted output for an individual field.
* i.e. print drupal_render($node->countent['field_foo']);
*
* The code now supports both single value formatters, which theme an
* individual item value as has been done in previous version of CCK,
* and multiple value formatters, which theme all values for the field
* in a single theme. The multiple value formatters could be used, for
* instance, to plot field values on a single map or display them
* in a graph. Single value formatters are the default, multiple value
* formatters can be designated as such in formatter_info().
*
* The node array will look like:
* $node->content['field_foo']['wrapper'] = array(
* '#type' => 'content_field',
* '#title' => 'label'
* '#field_name' => 'field_name',
* '#node' => $node,
* // Value of the $teaser param of hook_nodeapi('view').
* '#teaser' => $teaser,
* // Value of the $page param of hook_nodeapi('view').
* '#page' => $page,
* // The curent rendering context ('teaser', 'full', NODE_BUILD_SEARCH_INDEX...).
* '#context' => $context,
* 'items' =>
* 0 => array(
* '#item' => $items[0],
* // Only for 'single-value' formatters
* '#theme' => $theme,
* '#field_name' => 'field_name',
* '#type_name' => $node->type,
* '#formatter' => $formatter_name,
* '#node' => $node,
* '#delta' => 0,
* ),
* 1 => array(
* '#item' => $items[1],
* // Only for 'single-value' formatters
* '#theme' => $theme,
* '#field_name' => 'field_name',
* '#type_name' => $node->type,
* '#formatter' => $formatter_name,
* '#node' => $node,
* '#delta' => 1,
* ),
* // Only for 'multiple-value' formatters
* '#theme' => $theme,
* '#field_name' => 'field_name',
* '#type_name' => $node->type,
* '#formatter' => $formatter_name,
* ),
* );
*/
function content_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
// TODO: here we could validate that the number of multiple data is correct ?
// We're controlling the number of fields to fill out and saving empty
// ones if a specified number is requested, so no reason to do any validation
// here right now, but if later create a method to indicate whether
// 'required' means all values must be filled out, we can come back
// here and check that they're not empty.
break;
case 'presave':
if (!empty($node->devel_generate)) {
include_once('./'. drupal_get_path('module', 'content') .'/includes/content.devel.inc');
content_generate_fields($node, $field);
$items = $node->{$field['field_name']};
}
// Manual node_save calls might not have all fields filled in.
// On node insert, we need to make sure all tables get at least an empty
// record, or subsequent edits, using drupal_write_record() in update mode,
// won't insert any data.
// Missing fields on node update are handled in content_storage().
if (empty($items) && !isset($node->nid)) {
foreach (array_keys($field['columns']) as $column) {
$items[0][$column] = NULL;
}
$node->{$field['field_name']} = $items;
}
// If there was an AHAH add more button in this field, don't save it.
// TODO: is it still needed ?
unset($items[$field['field_name'] .'_add_more']);
if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
// Reorder items to account for drag-n-drop reordering.
$items = _content_sort_items($field, $items);
}
// Filter out empty values.
$items = content_set_empty($field, $items);
break;
case 'view':
$addition = array();
// Previewed nodes bypass the 'presave' op, so we need to some massaging.
if ($node->build_mode == NODE_BUILD_PREVIEW && content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
// Reorder items to account for drag-n-drop reordering.
$items = _content_sort_items($field, $items);
}
// Filter out empty values.
$items = content_set_empty($field, $items);
}
// NODE_BUILD_NORMAL is 0, and ('whatever' == 0) is TRUE, so we need a ===.
if ($node->build_mode === NODE_BUILD_NORMAL || $node->build_mode == NODE_BUILD_PREVIEW) {
$context = $teaser ? 'teaser' : 'full';
}
else {
$context = $node->build_mode;
}
// The field may be missing info for $contexts added by modules
// enabled after the field was last edited.
$formatter_name = isset($field['display_settings'][$context]) && isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
if ($formatter = _content_get_formatter($formatter_name, $field['type'])) {
$theme = $formatter['module'] .'_formatter_'. $formatter_name;
$single = (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE);
$label_display = isset($field['display_settings']['label']['format']) ? $field['display_settings']['label']['format'] : 'above';
// Do not include field labels when indexing content.
if ($context == NODE_BUILD_SEARCH_INDEX) {
$label_display = 'hidden';
}
$element = array(
'#type' => 'content_field',
'#title' => check_plain(t($field['widget']['label'])),
'#field_name' => $field['field_name'],
'#access' => $formatter_name != 'hidden' && content_access('view', $field, NULL, $node),
'#label_display' => $label_display,
'#node' => $node,
'#teaser' => $teaser,
'#page' => $page,
'#context' => $context,
'#single' => $single,
'items' => array(),
);
// Fill-in items.
foreach ($items as $delta => $item) {
$element['items'][$delta] = array(
'#item' => $item,
'#weight' => $delta,
);
}
// Append formatter information either on each item ('single-value' formatter)
// or at the upper 'items' level ('multiple-value' formatter)
$format_info = array(
'#theme' => $theme,
'#field_name' => $field['field_name'],
'#type_name' => $node->type,
'#formatter' => $formatter_name,
'#node' => $node,
);
if ($single) {
foreach ($items as $delta => $item) {
$element['items'][$delta] += $format_info;
$element['items'][$delta]['#item']['#delta'] = $delta;
}
}
else {
$element['items'] += $format_info;
}
// The wrapper lets us get the themed output for the whole field
// to populate the $FIELD_NAME_rendered variable for node templates,
// and hide it from the $content variable if needed.
// See 'preprocess_node' op and theme_content_field_wrapper()?
$wrapper = array(
'field' => $element,
'#weight' => $field['widget']['weight'],
'#post_render' => array('content_field_wrapper_post_render'),
'#field_name' => $field['field_name'],
'#type_name' => $node->type,
'#context' => $context,
);
$addition = array($field['field_name'] => $wrapper);
}
return $addition;
case 'alter':
// Add back the formatted values in the 'view' element,
// so that tokens and node templates can use it.
// Note: Doing this in 'preprocess_node' breaks token integration.
// The location of the field's rendered output depends on whether the
// field is in a fieldgroup or not.
$wrapper = NULL;
if (isset($node->content[$field['field_name']])) {
$wrapper = $node->content[$field['field_name']];
}
elseif (module_exists('fieldgroup') && ($group_name = fieldgroup_get_group($node->type, $field['field_name'])) && isset($node->content[$group_name]['group'][$field['field_name']])) {
$wrapper = $node->content[$group_name]['group'][$field['field_name']];
}
if ($wrapper) {
$element = $wrapper['field'];
// '#single' is not set if the field is hidden or inaccessible.
if (isset($element['#single'])) {
if (!empty($element['#single'])) {
// Single value formatter.
foreach (element_children($element['items']) as $delta) {
// '#chilren' is not set if the field is empty.
$items[$delta]['view'] = isset($element['items'][$delta]['#children']) ? $element['items'][$delta]['#children'] : '';
}
}
elseif (isset($element['items']['#children'])) {
// Multiple values formatter.
$items[0]['view'] = $element['items']['#children'];
}
}
else {
// Hidden or inaccessible field.
$items[0]['view'] = '';
}
}
break;
case 'preprocess_node':
// Add $FIELD_NAME_rendered variables.
$addition = array($field['field_name'] .'_rendered' => '');
// The location of the field's rendered output depends on whether the
// field is in a fieldgroup or not.
$wrapper = NULL;
if (isset($node->content[$field['field_name']])) {
$wrapper = $node->content[$field['field_name']];
}
elseif (module_exists('fieldgroup') && ($group_name = fieldgroup_get_group($node->type, $field['field_name'])) && isset($node->content[$group_name]['group'][$field['field_name']])) {
$wrapper = $node->content[$group_name]['group'][$field['field_name']];
}
if ($wrapper) {
// '#children' is not set if the field is empty.
$addition[$field['field_name'] .'_rendered'] = isset($wrapper['#children']) ? $wrapper['#children'] : '';
}
return $addition;
case 'prepare translation':
$addition = array();
if (isset($node->translation_source->{$field['field_name']})) {
$addition[$field['field_name']] = $node->translation_source->{$field['field_name']};
}
return $addition;
}
}
/**
* Helper function to filter out empty values.
*
* On order to keep marker rows in the database, the function ensures
* that the right number of 'all columns NULL' values is kept.
*
* @param array $field
* @param array $items
* @return array
* returns filtered and adjusted item array
*/
function content_set_empty($field, $items) {
// Filter out empty values.
$filtered = array();
$function = $field['module'] .'_content_is_empty';
foreach ((array) $items as $delta => $item) {
if (!$function($item, $field)) {
$filtered[] = $item;
}
}
// Make sure we store the right number of 'empty' values.
$empty = array();
foreach (array_keys($field['columns']) as $column) {
$empty[$column] = NULL;
}
$pad = $field['multiple'] > 1 ? $field['multiple'] : 1;
$filtered = array_pad($filtered, $pad, $empty);
return $filtered;
}
/**
* Helper function to sort items in a field according to
* user drag-n-drop reordering.
*/
function _content_sort_items($field, $items) {
if ($field['multiple'] >= 1 && isset($items[0]['_weight'])) {
usort($items, '_content_sort_items_helper');
foreach ($items as $delta => $item) {
if (is_array($items[$delta])) {
unset($items[$delta]['_weight']);
}
}
}
return $items;
}
/**
* Sort function for items order.
* (copied form element_sort(), which acts on #weight keys)
*/
function _content_sort_items_helper($a, $b) {
$a_weight = (is_array($a) && isset($a['_weight'])) ? $a['_weight'] : 0;
$b_weight = (is_array($b) && isset($b['_weight'])) ? $b['_weight'] : 0;
if ($a_weight == $b_weight) {
return 0;
}
return ($a_weight < $b_weight) ? -1 : 1;
}
/**
* Same as above, using ['_weight']['#value']
*/
function _content_sort_items_value_helper($a, $b) {
$a_weight = (is_array($a) && isset($a['_weight']['#value'])) ? $a['_weight']['#value'] : 0;
$b_weight = (is_array($b) && isset($b['_weight']['#value'])) ? $b['_weight']['#value'] : 0;
if ($a_weight == $b_weight) {
return 0;
}
return ($a_weight < $b_weight) ? -1 : 1;
}
/**
* Handle storage ops for _content_field_invoke_default().
*/
function content_storage($op, $node) {
// Don't try this before content module's update is run to add
// the active and module columns.
if (variable_get('content_schema_version', -1) < 6007) {
return FALSE;
}
$type_name = $node->type;
$type = content_types($type_name);
switch ($op) {
case 'load':
// OPTIMIZE: load all non multiple fields in a single JOIN query ?
// warning: 61-join limit in MySQL ?
$additions = array();
// For each table used by this content type,
foreach ($type['tables'] as $table) {
$schema = drupal_get_schema($table);
// The per-type table might not have any fields actually stored in it.
if (!$schema['content fields']) {
continue;
}
$query = 'SELECT * FROM {'. $table .'} WHERE vid = %d';
// If we're loading a table for a multiple field,
// we fetch all rows (values) ordered by delta,
// else we only fetch one row.
$result = isset($schema['fields']['delta']) ? db_query($query .' ORDER BY delta', $node->vid) : db_query_range($query, $node->vid, 0, 1);
// For each table row, populate the fields.
while ($row = db_fetch_array($result)) {
// For each field stored in the table, add the field item.
foreach ($schema['content fields'] as $field_name) {