5 * Allows administrators to associate custom fields to content types.
8 define('CONTENT_DB_STORAGE_PER_FIELD', 0);
9 define('CONTENT_DB_STORAGE_PER_CONTENT_TYPE', 1);
11 define('CONTENT_CALLBACK_NONE', 0x0001);
12 define('CONTENT_CALLBACK_DEFAULT', 0x0002);
13 define('CONTENT_CALLBACK_CUSTOM', 0x0004);
15 define('CONTENT_HANDLE_CORE', 0x0001);
16 define('CONTENT_HANDLE_MODULE', 0x0002);
18 function content_help($path, $arg) {
20 case
'admin/help#content':
21 $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>';
22 $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>';
23 $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>';
25 $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>';
26 $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>';
27 $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>';
28 $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>';
29 $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>';
31 $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>';
37 * Implementation of hook_flush_caches.
39 function content_flush_caches() {
40 return array(content_cache_tablename());
44 * Implementation of hook_init().
46 function content_init() {
47 drupal_add_css(drupal_get_path('module', 'content') .
'/theme/content-module.css');
48 if (module_exists('token') && !function_exists('content_token_values')) {
49 module_load_include('inc', 'content', 'includes/content.token');
51 if (module_exists('diff') && !function_exists('content_diff')) {
52 module_load_include('inc', 'content', 'includes/content.diff');
57 * Implementation of hook_perm().
59 function content_perm() {
60 return array('Use PHP input for field settings (dangerous - grant with care)');
64 * Implementation of hook_menu_alter().
66 function content_menu_alter(&$items) {
67 // Customize the content types page with our own callback
68 $items['admin/content/types']['page callback'] = 'content_types_overview';
69 $items['admin/content/types']['file'] = 'content.admin.inc';
70 $items['admin/content/types']['file path'] = drupal_get_path('module', 'content') .
'/includes';
74 * Implementation of hook_menu().
76 function content_menu() {
78 $items['admin/content/types/fields'] = array(
80 'page callback' => 'content_fields_list',
81 'access arguments' => array('administer content types'),
82 'file' => 'includes/content.admin.inc',
83 'type' => MENU_LOCAL_TASK
,
85 // Callback for AHAH add more buttons.
86 $items['content/js_add_more'] = array(
87 'page callback' => 'content_add_more_js',
88 'access arguments' => array('access content'),
89 'file' => 'includes/content.node_form.inc',
90 'type' => MENU_CALLBACK
,
93 // Make sure this doesn't fire until content_types is working,
94 // and tables are updated, needed to avoid errors on initial installation.
95 if (!defined('MAINTENANCE_MODE') && variable_get('content_schema_version', -1) >= 6007) {
96 foreach (node_get_types() as
$type) {
97 $type_name = $type->type
;
98 $content_type = content_types($type_name);
99 $type_url_str = $content_type['url_str'];
100 $items['admin/content/node-type/'.
$type_url_str .
'/fields'] = array(
101 'title' => 'Manage fields',
102 'page callback' => 'drupal_get_form',
103 'page arguments' => array('content_field_overview_form', $type_name),
104 'access arguments' => array('administer content types'),
105 'file' => 'includes/content.admin.inc',
106 'type' => MENU_LOCAL_TASK
,
109 $items['admin/content/node-type/'.
$type_url_str .
'/display'] = array(
110 'title' => 'Display fields',
111 'page callback' => 'drupal_get_form',
112 'page arguments' => array('content_display_overview_form', $type_name),
113 'access arguments' => array('administer content types'),
114 'file' => 'includes/content.admin.inc',
115 'type' => MENU_LOCAL_TASK
,
118 $contexts = content_build_modes('_tabs');
119 foreach ($contexts as
$key => $tab) {
120 $items['admin/content/node-type/'.
$type_url_str .
'/display/'.
$key] = array(
121 'title' => $tab['title'],
122 'page arguments' => array('content_display_overview_form', $type_name, $key),
123 'access arguments' => array('administer content types'),
124 'type' => $key == 'basic' ? MENU_DEFAULT_LOCAL_TASK
: MENU_LOCAL_TASK
,
125 'weight' => $key == 'basic' ?
0 : 1,
128 // Cast as an array in case this is called before any fields have
129 // been added, like when a new content type is created.
130 foreach ((array) $content_type['fields'] as
$field) {
131 $field_name = $field['field_name'];
132 $items['admin/content/node-type/'.
$type_url_str .
'/fields/'.
$field_name] = array(
133 'title' => $field['widget']['label'],
134 'page callback' => 'drupal_get_form',
135 'page arguments' => array('content_field_edit_form', $type_name, $field_name),
136 'access arguments' => array('administer content types'),
137 'file' => 'includes/content.admin.inc',
138 'type' => MENU_LOCAL_TASK
,
140 $items['admin/content/node-type/'.
$type_url_str .
'/fields/'.
$field_name .
'/remove'] = array(
141 'title' => 'Remove field',
142 'page callback' => 'drupal_get_form',
143 'page arguments' => array('content_field_remove_form', $type_name, $field_name),
144 'access arguments' => array('administer content types'),
145 'file' => 'includes/content.admin.inc',
146 'type' => MENU_CALLBACK
,
157 * Used to add multiple value processing, validation, and themes.
159 * FAPI callbacks can be declared here, and the element will be
160 * passed to those callbacks.
162 * Drupal will automatically theme the element using a theme with
163 * the same name as the hook_elements key.
165 function content_elements() {
167 'content_multiple_values' => array(),
168 'content_field' => array(),
173 * Implementation of hook_theme().
175 function content_theme() {
176 $path = drupal_get_path('module', 'content') .
'/theme';
177 require_once
"./$path/theme.inc";
180 'content_field' => array(
181 'template' => 'content-field',
182 'arguments' => array('element' => NULL
),
185 'content_overview_links' => array(
186 'arguments' => array(),
188 'content_field_overview_form' => array(
189 'template' => 'content-admin-field-overview-form',
190 'file' => 'theme.inc',
192 'arguments' => array('form' => NULL
),
194 'content_display_overview_form' => array(
195 'template' => 'content-admin-display-overview-form',
196 'file' => 'theme.inc',
198 'arguments' => array('form' => NULL
),
200 'content_exclude' => array(
201 'arguments' => array('content' => NULL
, 'object' => array(), 'context' => NULL
),
203 'content_view_multiple_field' => array(
204 'arguments' => array('items' => NULL
, 'field' => NULL
, 'data' => NULL
),
206 'content_multiple_values' => array(
207 'arguments' => array('element' => NULL
),
213 * Implementation of hook_views_api().
215 function content_views_api() {
218 'path' => drupal_get_path('module', 'content') .
'/includes/views',
223 * Implementation of hook_ctools_plugin_directory().
225 function content_ctools_plugin_directory($module, $plugin) {
226 if ($module == 'ctools' && $plugin == 'content_types') {
227 return 'includes/panels/' .
$plugin;
232 * Load data for a node type's fields.
233 * Implementation of hook_nodeapi 'load' op.
235 * When loading one of the content.module nodes, we need to let each field handle
236 * its own loading. This can make for a number of queries in some cases, so we
237 * cache the loaded object structure and invalidate it during the update process.
239 function content_load(&$node) {
240 $cid = 'content:'.
$node->nid .
':'.
$node->vid
;
241 if ($cached = cache_get($cid, content_cache_tablename())) {
242 foreach ($cached->data as
$key => $value) {
243 $node->$key = $value;
247 $default_additions = _content_field_invoke_default('load', $node);
248 if ($default_additions) {
249 foreach ($default_additions as
$key => $value) {
250 $node->$key = $value;
253 $additions = _content_field_invoke('load', $node);
255 foreach ($additions as
$key => $value) {
256 $node->$key = $value;
257 $default_additions[$key] = $value;
260 cache_set($cid, $default_additions, content_cache_tablename());
265 * Implementation of hook_nodeapi 'validate' op.
268 function content_validate(&$node, $form = NULL
) {
269 _content_field_invoke('validate', $node, $form);
270 _content_field_invoke_default('validate', $node, $form);
274 * Implementation of hook_nodeapi 'presave' op.
277 function content_presave(&$node) {
278 _content_field_invoke('presave', $node);
279 _content_field_invoke_default('presave', $node);
283 * Implementation of hook_nodeapi 'insert' op.
285 * Insert node type fields.
287 function content_insert(&$node) {
288 _content_field_invoke('insert', $node);
289 _content_field_invoke_default('insert', $node);
293 * Implementation of hook_nodeapi 'update' op.
295 * Update node type fields.
297 function content_update(&$node) {
298 _content_field_invoke('update', $node);
299 _content_field_invoke_default('update', $node);
300 cache_clear_all('content:'.
$node->nid .
':'.
$node->vid
, content_cache_tablename());
304 * Implementation of hook_nodeapi 'delete' op.
306 * Delete node type fields.
308 function content_delete(&$node) {
309 _content_field_invoke('delete', $node);
310 _content_field_invoke_default('delete', $node);
311 cache_clear_all('content:'.
$node->nid .
':', content_cache_tablename(), TRUE
);
315 * Implementation of hook_nodeapi 'delete_revision' op.
317 * Delete node type fields for a revision.
319 function content_delete_revision(&$node) {
320 _content_field_invoke('delete revision', $node);
321 _content_field_invoke_default('delete revision', $node);
322 cache_clear_all('content:'.
$node->nid .
':'.
$node->vid
, content_cache_tablename());
326 * Implementation of hook_nodeapi 'view' op.
328 * Generate field render arrays.
330 function content_view(&$node, $teaser = FALSE
, $page = FALSE
) {
331 // Let field modules sanitize their data for output.
332 _content_field_invoke('sanitize', $node, $teaser, $page);
335 $additions = _content_field_invoke_default('view', $node, $teaser, $page);
336 $node->content
= array_merge((array) $node->content
, $additions);
340 * Render a single field, fully themed with label and multiple values.
342 * To be used by third-party code (Views, Panels...) that needs to output
343 * an isolated field. Do *not* use inside node templates, use the
344 * $FIELD_NAME_rendered variables instead.
346 * By default, the field is displayed using the settings defined for the
347 * 'full node' or 'teaser' contexts (depending on the value of the $teaser param).
348 * Set $node->build_mode to a different value to use a different context.
350 * Different settings can be specified by adjusting $field['display_settings'].
353 * The field definition.
355 * The node containing the field to display. Can be a 'pseudo-node', containing
356 * at least 'type', 'nid', 'vid', and the field data.
359 * Similar to hook_nodeapi('view')
361 * The themed output for the field.
363 function content_view_field($field, $node, $teaser = FALSE
, $page = FALSE
) {
365 if (isset($node->$field['field_name'])) {
366 $items = $node->$field['field_name'];
368 // Use 'full'/'teaser' if not specified otherwise.
369 $node->build_mode
= isset($node->build_mode
) ?
$node->build_mode
: NODE_BUILD_NORMAL
;
371 // One-field equivalent to _content_field_invoke('sanitize').
372 $field_types = _content_field_types();
373 $module = $field_types[$field['type']]['module'];
374 $function = $module .
'_field';
375 if (function_exists($function)) {
376 $function('sanitize', $node, $field, $items, $teaser, $page);
377 $node->$field['field_name'] = $items;
380 $view = content_field('view', $node, $field, $items, $teaser, $page);
381 // content_field('view') adds a wrapper to handle variables and 'excluded'
382 // fields for node templates. We bypass it and render the actual field.
383 $output = drupal_render($view[$field['field_name']]['field']);
389 * Implementation of hook_nodeapi 'alter' op.
391 * Add back the formatted values in the 'view' element for all fields,
392 * so that node templates can use it.
394 function content_alter(&$node, $teaser = FALSE
, $page = FALSE
) {
395 _content_field_invoke_default('alter', $node, $teaser, $page);
399 * Implementation of hook_nodeapi 'prepare translation' op.
401 * Generate field render arrays.
403 function content_prepare_translation(&$node) {
404 $default_additions = _content_field_invoke_default('prepare translation', $node);
405 $additions = _content_field_invoke('prepare translation', $node);
406 // Merge module additions after the default ones to enable overriding
408 $node = (object) array_merge((array) $node, $default_additions, $additions);
412 * Implementation of hook_nodeapi().
414 function content_nodeapi(&$node, $op, $a3 = NULL
, $a4 = NULL
) {
415 // Prevent against invalid 'nodes' built by broken 3rd party code.
416 if (isset($node->type
)) {
417 $type = content_types($node->type
);
418 // Save cycles if the type has no CCK fields.
419 if (!empty($type['fields'])) {
420 $callback = 'content_'.
str_replace(' ', '_', $op);
421 if (function_exists($callback)) {
422 $callback($node, $a3, $a4);
426 // Special case for 'view' op, we want to adjust weights of non-cck fields
427 // even if there are no actual fields for this type.
429 $node->content
['#pre_render'][] = 'content_alter_extra_weights';
430 $node->content
['#content_extra_fields'] = $type['extra'];
436 * Implementation of hook_form_alter().
438 function content_form_alter(&$form, $form_state, $form_id) {
439 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .
'_node_form' == $form_id) {
440 $type = content_types($form['#node']->type
);
441 if (!empty($type['fields'])) {
442 module_load_include('inc', 'content', 'includes/content.node_form');
443 // Merge field widgets.
444 $form = array_merge($form, content_form($form, $form_state));
446 $form['#pre_render'][] = 'content_alter_extra_weights';
447 $form['#content_extra_fields'] = $type['extra'];
452 * Pre-render callback to adjust weights of non-CCK fields.
454 function content_alter_extra_weights($elements) {
455 if (isset($elements['#content_extra_fields'])) {
456 foreach ($elements['#content_extra_fields'] as
$key => $value) {
457 // Some core 'fields' use a different key in node forms and in 'view'
458 // render arrays. Check we're not on a form first.
459 if (!isset($elements['#build_id']) && isset($value['view']) && isset($elements[$value['view']])) {
460 $elements[$value['view']]['#weight'] = $value['weight'];
462 elseif (isset($elements[$key])) {
463 $elements[$key]['#weight'] = $value['weight'];
471 * Proxy function to call content_add_more_submit(), because it might not be
472 * included yet when the form is processed and invokes the callback.
474 function content_add_more_submit_proxy($form, &$form_state) {
475 module_load_include('inc', 'content', 'includes/content.node_form');
476 content_add_more_submit($form, $form_state);
480 * Theme an individual form element.
482 * Combine multiple values into a table with drag-n-drop reordering.
484 function theme_content_multiple_values($element) {
485 $field_name = $element['#field_name'];
486 $field = content_fields($field_name);
489 if ($field['multiple'] >= 1) {
490 $table_id = $element['#field_name'] .
'_values';
491 $order_class = $element['#field_name'] .
'-delta-order';
492 $required = !empty($element['#required']) ?
'<span class="form-required" title="'.
t('This field is required.') .
'">*</span>' : '';
496 'data' => t('!title: !required', array('!title' => $element['#title'], '!required' => $required)),
503 // Sort items according to '_weight' (needed when the form comes back after
504 // preview or failed validation)
506 foreach (element_children($element) as
$key) {
507 if ($key !== $element['#field_name'] .
'_add_more') {
508 $items[] = &$element[$key];
511 usort($items, '_content_sort_items_value_helper');
513 // Add the items as table rows.
514 foreach ($items as
$key => $item) {
515 $item['_weight']['#attributes']['class'] = $order_class;
516 $delta_element = drupal_render($item['_weight']);
518 array('data' => '', 'class' => 'content-multiple-drag'),
519 drupal_render($item),
520 array('data' => $delta_element, 'class' => 'delta-order'),
524 'class' => 'draggable',
528 $output .
= theme('table', $header, $rows, array('id' => $table_id, 'class' => 'content-multiple-table'));
529 $output .
= $element['#description'] ?
'<div class="description">'.
$element['#description'] .
'</div>' : '';
530 $output .
= drupal_render($element[$element['#field_name'] .
'_add_more']);
532 drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
535 foreach (element_children($element) as
$key) {
536 $output .
= drupal_render($element[$key]);
544 * Modules notify Content module when uninstalled, disabled, etc.
547 * the module operation: uninstall, install, enable, disable
548 * @param string $module
549 * the name of the affected module.
551 * figure out exactly what needs to be done by content module when
552 * field modules are installed, uninstalled, enabled or disabled.
554 function content_notify($op, $module) {
557 content_clear_type_cache();
560 module_load_include('inc', 'content', 'includes/content.crud');
561 content_module_delete($module);
564 content_associate_fields($module);
565 content_clear_type_cache();
568 // When CCK modules are disabled before content module's update is run
569 // to add the active column, we can't do this.
570 if (variable_get('content_schema_version', -1) < 6007) {
573 db_query("UPDATE {".
content_field_tablename() .
"} SET active=0 WHERE module='%s'", $module);
574 db_query("UPDATE {".
content_instance_tablename() .
"} SET widget_active=0 WHERE widget_module='%s'", $module);
575 content_clear_type_cache(TRUE
);
581 * Allows a module to update the database for fields and columns it controls.
583 * @param string $module
584 * The name of the module to update on.
586 function content_associate_fields($module) {
587 // When CCK modules are enabled before content module's update is run,
588 // to add module and active columns, we can't do this.
589 if (variable_get('content_schema_version', -1) < 6007) {
592 $module_fields = module_invoke($module, 'field_info');
593 if ($module_fields) {
594 foreach ($module_fields as
$name => $field_info) {
595 watchdog('content', 'Updating field type %type with module %module.', array('%type' => $name, '%module' => $module));
596 db_query("UPDATE {".
content_field_tablename() .
"} SET module = '%s', active = %d WHERE type = '%s'", $module, 1, $name);
599 $module_widgets = module_invoke($module, 'widget_info');
600 if ($module_widgets) {
601 foreach ($module_widgets as
$name => $widget_info) {
602 watchdog('content', 'Updating widget type %type with module %module.', array('%type' => $name, '%module' => $module));
603 db_query("UPDATE {".
content_instance_tablename() .
"} SET widget_module = '%s', widget_active = %d WHERE widget_type = '%s'", $module, 1, $name);
606 // This is called from updates and installs, so get the install-safe
607 // version of a fields array.
608 $fields_set = array();
609 module_load_include('install', 'content');
610 $types = content_types_install();
611 foreach ($types as
$type_name => $fields) {
612 foreach ($fields as
$field) {
613 if ($field['module'] == $module && !in_array($field['field_name'], $fields_set)) {
614 $columns = (array) module_invoke($field['module'], 'field_settings', 'database columns', $field);
615 db_query("UPDATE {".
content_field_tablename() .
"} SET db_columns = '%s' WHERE field_name = '%s'", serialize($columns), $field['field_name']);
616 $fields_set[] = $field['field_name'];
623 * Implementation of hook_field(). Handles common field housekeeping.
625 * This implementation is special, as content.module does not define any field
626 * types. Instead, this function gets called after the type-specific hook, and
627 * takes care of default stuff common to all field types.
629 * Db-storage ops ('load', 'insert', 'update', 'delete', 'delete revisions')
630 * are not executed field by field, and are thus handled separately in
633 * The 'view' operation constructs the $node in a way that you can use
634 * drupal_render() to display the formatted output for an individual field.
635 * i.e. print drupal_render($node->countent['field_foo']);
637 * The code now supports both single value formatters, which theme an
638 * individual item value as has been done in previous version of CCK,
639 * and multiple value formatters, which theme all values for the field
640 * in a single theme. The multiple value formatters could be used, for
641 * instance, to plot field values on a single map or display them
642 * in a graph. Single value formatters are the default, multiple value
643 * formatters can be designated as such in formatter_info().
645 * The node array will look like:
646 * $node->content['field_foo']['wrapper'] = array(
647 * '#type' => 'content_field',
648 * '#title' => 'label'
649 * '#field_name' => 'field_name',
651 * // Value of the $teaser param of hook_nodeapi('view').
652 * '#teaser' => $teaser,
653 * // Value of the $page param of hook_nodeapi('view').
655 * // The curent rendering context ('teaser', 'full', NODE_BUILD_SEARCH_INDEX...).
656 * '#context' => $context,
659 * '#item' => $items[0],
660 * // Only for 'single-value' formatters
661 * '#theme' => $theme,
662 * '#field_name' => 'field_name',
663 * '#type_name' => $node->type,
664 * '#formatter' => $formatter_name,
669 * '#item' => $items[1],
670 * // Only for 'single-value' formatters
671 * '#theme' => $theme,
672 * '#field_name' => 'field_name',
673 * '#type_name' => $node->type,
674 * '#formatter' => $formatter_name,
678 * // Only for 'multiple-value' formatters
679 * '#theme' => $theme,
680 * '#field_name' => 'field_name',
681 * '#type_name' => $node->type,
682 * '#formatter' => $formatter_name,
686 function content_field($op, &$node, $field, &$items, $teaser, $page) {
689 // TODO: here we could validate that the number of multiple data is correct ?
690 // We're controlling the number of fields to fill out and saving empty
691 // ones if a specified number is requested, so no reason to do any validation
692 // here right now, but if later create a method to indicate whether
693 // 'required' means all values must be filled out, we can come back
694 // here and check that they're not empty.
698 if (!empty($node->devel_generate
)) {
699 include_once('./'.
drupal_get_path('module', 'content') .
'/includes/content.devel.inc');
700 content_generate_fields($node, $field);
701 $items = $node->{$field['field_name']};
704 // Manual node_save calls might not have all fields filled in.
705 // On node insert, we need to make sure all tables get at least an empty
706 // record, or subsequent edits, using drupal_write_record() in update mode,
707 // won't insert any data.
708 // Missing fields on node update are handled in content_storage().
709 if (empty($items) && !isset($node->nid
)) {
710 foreach (array_keys($field['columns']) as
$column) {
711 $items[0][$column] = NULL
;
713 $node->$field['field_name'] = $items;
716 // If there was an AHAH add more button in this field, don't save it.
717 // TODO: is it still needed ?
718 unset($items[$field['field_name'] .
'_add_more']);
720 if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE
) {
721 // Reorder items to account for drag-n-drop reordering.
722 $items = _content_sort_items($field, $items);
725 // Filter out empty values.
726 $items = content_set_empty($field, $items);
733 // Previewed nodes bypass the 'presave' op, so we need to some massaging.
734 if ($node->build_mode
== NODE_BUILD_PREVIEW
&& content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE
) {
735 if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE
) {
736 // Reorder items to account for drag-n-drop reordering.
737 $items = _content_sort_items($field, $items);
740 // Filter out empty values.
741 $items = content_set_empty($field, $items);
744 // NODE_BUILD_NORMAL is 0, and ('whatever' == 0) is TRUE, so we need a ===.
745 if ($node->build_mode
=== NODE_BUILD_NORMAL
|| $node->build_mode
== NODE_BUILD_PREVIEW
) {
746 $context = $teaser ?
'teaser' : 'full';
749 $context = $node->build_mode
;
751 // The field may be missing info for $contexts added by modules
752 // enabled after the field was last edited.
753 $formatter_name = isset($field['display_settings'][$context]) && isset($field['display_settings'][$context]['format']) ?
$field['display_settings'][$context]['format'] : 'default';
754 if ($formatter = _content_get_formatter($formatter_name, $field['type'])) {
755 $theme = $formatter['module'] .
'_formatter_'.
$formatter_name;
756 $single = (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE
);
758 $label_display = isset($field['display_settings']['label']['format']) ?
$field['display_settings']['label']['format'] : 'above';
759 // Do not include field labels when indexing content.
760 if ($context == NODE_BUILD_SEARCH_INDEX
) {
761 $label_display = 'hidden';
765 '#type' => 'content_field',
766 '#title' => check_plain(t($field['widget']['label'])),
767 '#field_name' => $field['field_name'],
768 '#access' => $formatter_name != 'hidden' && content_access('view', $field, NULL
, $node),
769 '#label_display' => $label_display,
771 '#teaser' => $teaser,
773 '#context' => $context,
774 '#single' => $single,
779 foreach ($items as
$delta => $item) {
780 $element['items'][$delta] = array(
786 // Append formatter information either on each item ('single-value' formatter)
787 // or at the upper 'items' level ('multiple-value' formatter)
788 $format_info = array(
790 '#field_name' => $field['field_name'],
791 '#type_name' => $node->type
,
792 '#formatter' => $formatter_name,
796 foreach ($items as
$delta => $item) {
797 $element['items'][$delta] += $format_info;
798 $element['items'][$delta]['#item']['#delta'] = $delta;
802 $element['items'] += $format_info;
805 // The wrapper lets us get the themed output for the whole field
806 // to populate the $FIELD_NAME_rendered variable for node templates,
807 // and hide it from the $content variable if needed.
808 // See 'preprocess_node' op and theme_content_field_wrapper()?
811 '#weight' => $field['widget']['weight'],
812 '#post_render' => array('content_field_wrapper_post_render'),
813 '#field_name' => $field['field_name'],
814 '#type_name' => $node->type
,
815 '#context' => $context,
818 $addition = array($field['field_name'] => $wrapper);
823 // Add back the formatted values in the 'view' element,
824 // so that tokens and node templates can use it.
825 // Note: Doing this in 'preprocess_node' breaks token integration.
827 // The location of the field's rendered output depends on whether the
828 // field is in a fieldgroup or not.
830 if (isset($node->content
[$field['field_name']])) {
831 $wrapper = $node->content
[$field['field_name']];
833 elseif (module_exists('fieldgroup') && ($group_name = fieldgroup_get_group($node->type
, $field['field_name'])) && isset($node->content
[$group_name]['group'][$field['field_name']])) {
834 $wrapper = $node->content
[$group_name]['group'][$field['field_name']];
838 $element = $wrapper['field'];
839 // '#single' is not set if the field is hidden or inaccessible.
840 if (isset($element['#single'])) {
841 if (!empty($element['#single'])) {
842 // Single value formatter.
843 foreach (element_children($element['items']) as
$delta) {
844 // '#chilren' is not set if the field is empty.
845 $items[$delta]['view'] = isset($element['items'][$delta]['#children']) ?
$element['items'][$delta]['#children'] : '';
848 elseif (isset($element['items']['#children'])) {
849 // Multiple values formatter.
850 $items[0]['view'] = $element['items']['#children'];
854 // Hidden or inaccessible field.
855 $items[0]['view'] = '';
860 case
'preprocess_node':
861 // Add $FIELD_NAME_rendered variables.
864 // The location of the field's rendered output depends on whether the
865 // field is in a fieldgroup or not.
867 if (isset($node->content
[$field['field_name']])) {
868 $wrapper = $node->content
[$field['field_name']];
870 elseif (module_exists('fieldgroup') && ($group_name = fieldgroup_get_group($node->type
, $field['field_name'])) && isset($node->content
[$group_name]['group'][$field['field_name']])) {
871 $wrapper = $node->content
[$group_name]['group'][$field['field_name']];
875 // '#chilren' is not set if the field is empty.
876 $addition[$field['field_name'] .
'_rendered'] = isset($wrapper['#children']) ?
$wrapper['#children'] : '';
881 case
'prepare translation':
883 if (isset($node->translation_source
->$field['field_name'])) {
884 $addition[$field['field_name']] = $node->translation_source
->$field['field_name'];
891 * Helper function to filter out empty values.
893 * On order to keep marker rows in the database, the function ensures
894 * that the right number of 'all columns NULL' values is kept.
896 * @param array $field
897 * @param array $items
899 * returns filtered and adjusted item array
901 function content_set_empty($field, $items) {
902 // Filter out empty values.
904 $function = $field['module'] .
'_content_is_empty';
905 foreach ((array) $items as
$delta => $item) {
906 if (!$function($item, $field)) {
911 // Make sure we store the right number of 'empty' values.
913 foreach (array_keys($field['columns']) as
$column) {
914 $empty[$column] = NULL
;
916 $pad = $field['multiple'] > 1 ?
$field['multiple'] : 1;
917 $filtered = array_pad($filtered, $pad, $empty);
923 * Helper function to sort items in a field according to
924 * user drag-n-drop reordering.
926 function _content_sort_items($field, $items) {
927 if ($field['multiple'] >= 1 && isset($items[0]['_weight'])) {
928 usort($items, '_content_sort_items_helper');
929 foreach ($items as
$delta => $item) {
930 if (is_array($items[$delta])) {
931 unset($items[$delta]['_weight']);
939 * Sort function for items order.
940 * (copied form element_sort(), which acts on #weight keys)
942 function _content_sort_items_helper($a, $b) {
943 $a_weight = (is_array($a) && isset($a['_weight'])) ?
$a['_weight'] : 0;
944 $b_weight = (is_array($b) && isset($b['_weight'])) ?
$b['_weight'] : 0;
945 if ($a_weight == $b_weight) {
948 return ($a_weight < $b_weight) ?
-1 : 1;
952 * Same as above, using ['_weight']['#value']
954 function _content_sort_items_value_helper($a, $b) {
955 $a_weight = (is_array($a) && isset($a['_weight']['#value'])) ?
$a['_weight']['#value'] : 0;
956 $b_weight = (is_array($b) && isset($b['_weight']['#value'])) ?
$b['_weight']['#value'] : 0;
957 if ($a_weight == $b_weight) {
960 return ($a_weight < $b_weight) ?
-1 : 1;
964 * Handle storage ops for _content_field_invoke_default().
966 function content_storage($op, $node) {
967 // Don't try this before content module's update is run to add
968 // the active and module columns.
969 if (variable_get('content_schema_version', -1) < 6007) {
973 $type_name = $node->type
;
974 $type = content_types($type_name);
978 // OPTIMIZE: load all non multiple fields in a single JOIN query ?
979 // warning: 61-join limit in MySQL ?
980 $additions = array();
981 // For each table used by this content type,
982 foreach ($type['tables'] as
$table) {
983 $schema = drupal_get_schema($table);
984 // The per-type table might not have any fields actually stored in it.
985 if (!$schema['content fields']) {
988 $query = 'SELECT * FROM {'.
$table .
'} WHERE vid = %d';
990 // If we're loading a table for a multiple field,
991 // we fetch all rows (values) ordered by delta,
992 // else we only fetch one row.
993 $result = isset($schema['fields']['delta']) ?
db_query($query .
' ORDER BY delta', $node->vid
) : db_query_range($query, $node->vid
, 0, 1);
995 // For each table row, populate the fields.
996 while ($row = db_fetch_array($result)) {
997 // For each field stored in the table, add the field item.
998 foreach ($schema['content fields'] as
$field_name) {
1000 $field = content_fields($field_name, $type_name);
1001 $db_info = content_database_info($field);
1002 // For each column declared by the field, populate the item.
1003 foreach ($db_info['columns'] as
$column => $attributes) {
1004 $item[$column] = $row[$attributes['column']];
1007 // Add the item to the field values for the node.
1008 if (!isset($additions[$field_name])) {
1009 $additions[$field_name] = array();
1011 $additions[$field_name][] = $item;
1019 foreach ($type['tables'] as
$table) {
1020 $schema = drupal_get_schema($table);
1022 foreach ($schema['content fields'] as
$field_name) {
1023 if (isset($node->$field_name)) {
1024 $field = content_fields($field_name, $type_name);
1025 // Multiple fields need specific handling, we'll deal with them later on.
1026 if ($field['multiple']) {
1029 $db_info = content_database_info($field);
1030 foreach ($db_info['columns'] as
$column => $attributes) {
1031 $record[$attributes['column']] = $node->{$field_name}[0][$column];
1035 // $record might be empty because
1036 // - the table stores a multiple field :
1037 // we do nothing, this is handled later on
1038 // - this is the per-type table and no field is actually stored in it :
1039 // we still store the nid and vid
1040 if (count($record) || empty($schema['content fields'])) {
1041 $record['nid'] = $node->nid
;
1042 $record['vid'] = $node->vid
;
1043 // Can't rely on the insert/update op of the node to decide if this
1044 // is an insert or an update, a node or revision may have existed
1045 // before any fields were created, so there may not be an entry here.
1047 // TODO - should we auto create an entry for all existing nodes when
1048 // fields are added to content types -- either a NULL value
1049 // or the default value? May need to offer the user an option of
1050 // how to handle that.
1051 if (db_result(db_query("SELECT COUNT(*) FROM {".
$table .
"} WHERE vid = %d", $node->vid
))) {
1052 content_write_record($table, $record, array('vid'));
1055 content_write_record($table, $record);
1060 // Handle multiple fields.
1061 foreach ($type['fields'] as
$field) {
1062 if ($field['multiple'] && isset($node->$field['field_name'])) {
1063 $db_info = content_database_info($field);
1064 // Delete and insert, rather than update, in case a value was added.
1065 if ($op == 'update') {
1066 db_query('DELETE FROM {'.
$db_info['table'] .
'} WHERE vid = %d', $node->vid
);
1068 foreach ($node->$field['field_name'] as
$delta => $item) {
1070 foreach ($db_info['columns'] as
$column => $attributes) {
1071 $record[$attributes['column']] = $item[$column];
1073 $record['nid'] = $node->nid
;
1074 $record['vid'] = $node->vid
;
1075 $record['delta'] = $delta;
1076 content_write_record($db_info['table'], $record);
1083 foreach ($type['tables'] as
$table) {
1084 db_query('DELETE FROM {'.
$table .
'} WHERE nid = %d', $node->nid
);
1088 case
'delete revision':
1089 foreach ($type['tables'] as
$table) {
1090 db_query('DELETE FROM {'.
$table .
'} WHERE vid = %d', $node->vid
);
1097 * Save a record to the database based upon the schema.
1099 * Directly copied from core's drupal_write_record, which can't update a
1100 * column to NULL. See http://drupal.org/node/227677 and
1101 * http://drupal.org/node/226264 for more details about that problem.
1103 * TODO - get rid of this function and change references back to
1104 * drupal_write_record() if the patch gets into core. Will need a method
1105 * of protecting people on older versions, though.
1107 * Default values are filled in for missing items, and 'serial' (auto increment)
1108 * types are filled in with IDs.
1111 * The name of the table; this must exist in schema API.
1113 * The object to write. This is a reference, as defaults according to
1114 * the schema may be filled in on the object, as well as ID on the serial
1115 * type(s). Both array an object types may be passed.
1117 * If this is an update, specify the primary keys' field names. It is the
1118 * caller's responsibility to know if a record for this object already
1119 * exists in the database. If there is only 1 key, you may pass a simple string.
1121 * Failure to write a record will return FALSE. Otherwise SAVED_NEW or
1122 * SAVED_UPDATED is returned depending on the operation performed. The
1123 * $object parameter contains values for any serial fields defined by
1124 * the $table. For example, $object->nid will be populated after inserting
1127 function content_write_record($table, &$object, $update = array()) {
1128 // Standardize $update to an array.
1129 if (is_string($update)) {
1130 $update = array($update);
1133 // Convert to an object if needed.
1134 if (is_array($object)) {
1135 $object = (object) $object;
1142 $schema = drupal_get_schema($table);
1143 if (empty($schema)) {
1147 $fields = $defs = $values = $serials = $placeholders = array();
1149 // Go through our schema, build SQL, and when inserting, fill in defaults for
1150 // fields that are not set.
1151 foreach ($schema['fields'] as
$field => $info) {
1152 // Special case -- skip serial types if we are updating.
1153 if ($info['type'] == 'serial' && count($update)) {
1157 // For inserts, populate defaults from Schema if not already provided
1158 if (!isset($object->$field) && !count($update) && isset($info['default'])) {
1159 $object->$field = $info['default'];
1162 // Track serial fields so we can helpfully populate them after the query.
1163 if ($info['type'] == 'serial') {
1164 $serials[] = $field;
1165 // Ignore values for serials when inserting data. Unsupported.
1166 unset($object->$field);
1169 // Build arrays for the fields, placeholders, and values in our query.
1170 if (isset($object->$field) || array_key_exists($field, $object)) {
1172 if (isset($object->$field)) {
1173 $placeholders[] = db_type_placeholder($info['type']);
1175 if (empty($info['serialize'])) {
1176 $values[] = $object->$field;
1179 $values[] = serialize($object->$field);
1183 $placeholders[] = 'NULL';
1190 if (!count($update)) {
1191 $query = "INSERT INTO {".
$table .
"} (".
implode(', ', $fields) .
') VALUES ('.
implode(', ', $placeholders) .
')';
1192 $return = SAVED_NEW
;
1196 foreach ($fields as
$id => $field) {
1200 $query .
= $field .
' = '.
$placeholders[$id];
1203 foreach ($update as
$key) {
1204 $conditions[] = "$key = ".
db_type_placeholder($schema['fields'][$key]['type']);
1205 $values[] = $object->$key;
1208 $query = "UPDATE {".
$table .
"} SET $query WHERE ".
implode(' AND ', $conditions);
1209 $return = SAVED_UPDATED
;
1213 if (db_query($query, $values)) {
1215 // Get last insert ids and fill them in.
1216 foreach ($serials as
$field) {
1217 $object->$field = db_last_insert_id($table, $field);
1221 // If we began with an array, convert back so we don't surprise the caller.
1223 $object = (array) $object;
1233 * Invoke a field hook.
1235 * For each operation, both this function and _content_field_invoke_default() are
1236 * called so that the default database handling can occur.
1238 function _content_field_invoke($op, &$node, $teaser = NULL
, $page = NULL
) {
1239 $type_name = is_string($node) ?
$node : (is_array($node) ?
$node['type'] : $node->type
);
1240 $type = content_types($type_name);
1241 $field_types = _content_field_types();
1244 foreach ($type['fields'] as
$field) {
1245 $items = isset($node->$field['field_name']) ?
$node->$field['field_name'] : array();
1247 // Make sure AHAH 'add more' button isn't sent to the fields for processing.
1248 unset($items[$field['field_name'] .
'_add_more']);
1250 $module = $field_types[$field['type']]['module'];
1251 $function = $module .
'_field';
1252 if (function_exists($function)) {
1253 $result = $function($op, $node, $field, $items, $teaser, $page);
1254 if (is_array($result)) {
1255 $return = array_merge($return, $result);
1257 else if (isset($result)) {
1258 $return[] = $result;
1261 // test for values in $items in case modules added items on insert
1262 if (isset($node->$field['field_name']) || count($items)) {
1263 $node->$field['field_name'] = $items;
1270 * Invoke content.module's version of a field hook.
1272 function _content_field_invoke_default($op, &$node, $teaser = NULL
, $page = NULL
) {
1273 $type_name = is_string($node) ?
$node : (is_array($node) ?
$node['type'] : $node->type
);
1274 $type = content_types($type_name);
1275 $field_types = _content_field_types();
1278 // The operations involving database queries are better off handled by table
1279 // rather than by field.
1280 if (in_array($op, array('load', 'insert', 'update', 'delete', 'delete revision'))) {
1281 return content_storage($op, $node);
1284 foreach ($type['fields'] as
$field) {
1285 $items = isset($node->$field['field_name']) ?
$node->$field['field_name'] : array();
1286 $result = content_field($op, $node, $field, $items, $teaser, $page);
1287 if (is_array($result)) {
1288 $return = array_merge($return, $result);
1290 else if (isset($result)) {
1291 $return[] = $result;
1293 if (isset($node->$field['field_name'])) {
1294 $node->$field['field_name'] = $items;
1302 * Return a list of all content types.
1304 * @param $content_type_name
1305 * If set, return information on just this type.
1307 * Do some type checking and set up empty arrays for missing
1308 * info to avoid foreach errors elsewhere in the code.
1310 function content_types($type_name = NULL
) {
1311 // handle type name with either an underscore or a dash
1312 $type_name = !empty($type_name) ?
str_replace('-', '_', $type_name) : NULL
;
1314 $info = _content_type_info();
1315 if (isset($info['content types'])) {
1316 if (!isset($type_name)) {
1317 return $info['content types'];
1319 if (isset($info['content types'][$type_name])) {
1320 return $info['content types'][$type_name];
1323 return array('tables' => array(), 'fields' => array(), 'extra' => array());
1327 * Return a list of all fields.
1329 * @param $field_name
1330 * If not empty, return information on just this field.
1331 * @param $content_type_name
1332 * If not empty, return information of the field within the context of this content
1335 * Be sure to check empty() instead of isset() on field_name and
1336 * content_type_name to avoid bad results when the value is set
1337 * but empty, as sometimes happens in the formatter.
1339 function content_fields($field_name = NULL
, $content_type_name = NULL
) {
1340 $info = _content_type_info();
1341 if (isset($info['fields'])) {
1342 if (empty($field_name)) {
1343 return $info['fields'];
1345 if (isset($info['fields'][$field_name])) {
1346 if (empty($content_type_name)) {
1347 return $info['fields'][$field_name];
1349 if (isset($info['content types'][$content_type_name]['fields'][$field_name])) {
1350 return $info['content types'][$content_type_name]['fields'][$field_name];
1357 * Return a list of field types.
1359 function _content_field_types() {
1360 $info = _content_type_info();
1361 return isset($info['field types']) ?
$info['field types'] : array();
1365 * Return a list of widget types.
1367 function _content_widget_types() {
1368 $info = _content_type_info();
1369 return isset($info['widget types']) ?
$info['widget types'] : array();
1373 * Return the formatter description corresponding to a formatter name,
1374 * defaulting to 'default' if none is found.
1376 function _content_get_formatter($formatter_name, $field_type) {
1377 $field_types = _content_field_types();
1378 $formatters = $field_types[$field_type]['formatters'];
1380 if (!isset($formatters[$formatter_name]) && $formatter_name != 'hidden') {
1381 // This might happen when the selected formatter has been renamed in the
1382 // module, or if the module has been disabled since then.
1383 $formatter_name = 'default';
1386 return isset($formatters[$formatter_name]) ?
$formatters[$formatter_name] : FALSE
;
1390 * Collate all information on content types, fields, and related structures.
1393 * If TRUE, clear the cache and fetch the information from the database again.
1395 function _content_type_info($reset = FALSE
) {
1399 if ($reset || !isset($info)) {
1400 // Make sure this function doesn't run until the tables have been created,
1401 // For instance: when first enabled and called from content_menu(),
1402 // or when uninstalled and some subsequent field module uninstall
1403 // attempts to refresh the data.
1405 // Don't try this before content module's update is run
1406 // to add module and active columns to the table.
1407 if (variable_get('content_schema_version', -1) < 6007) {
1411 if (!$reset && $cached = cache_get('content_type_info:'.
$language->language
, content_cache_tablename())) {
1412 $info = $cached->data
;
1416 'field types' => array(),
1417 'widget types' => array(),
1418 'fields' => array(),
1419 'content types' => array(),
1422 // Populate field types.
1423 foreach (module_list() as
$module) {
1424 $module_field_types = module_invoke($module, 'field_info');
1425 if ($module_field_types) {
1426 foreach ($module_field_types as
$name => $field_info) {
1427 // Truncate names to match the value that is stored in the database.
1428 $db_name = substr($name, 0, 32);
1429 $info['field types'][$db_name] = $field_info;
1430 $info['field types'][$db_name]['module'] = $module;
1431 $info['field types'][$db_name]['formatters'] = array();
1436 // Populate widget types and formatters for known field types.
1437 foreach (module_list() as
$module) {
1438 if ($module_widgets = module_invoke($module, 'widget_info')) {
1439 foreach ($module_widgets as
$name => $widget_info) {
1440 // Truncate names to match the value that is stored in the database.
1441 $db_name = substr($name, 0, 32);
1442 $info['widget types'][$db_name] = $widget_info;
1443 $info['widget types'][$db_name]['module'] = $module;
1444 // Replace field types with db_compatible version of known field types.
1445 $info['widget types'][$db_name]['field types'] = array();
1446 foreach ($widget_info['field types'] as
$field_type) {
1447 $field_type_db_name = substr($field_type, 0, 32);
1448 if (isset($info['field types'][$field_type_db_name])) {
1449 $info['widget types'][$db_name]['field types'][] = $field_type_db_name;
1455 if ($module_formatters = module_invoke($module, 'field_formatter_info')) {
1456 foreach ($module_formatters as
$name => $formatter_info) {
1457 foreach ($formatter_info['field types'] as
$field_type) {
1458 // Truncate names to match the value that is stored in the database.
1459 $db_name = substr($field_type, 0, 32);
1460 if (isset($info['field types'][$db_name])) {
1461 $info['field types'][$db_name]['formatters'][$name] = $formatter_info;
1462 $info['field types'][$db_name]['formatters'][$name]['module'] = $module;
1469 // Populate actual field instances.
1470 module_load_include('inc', 'content', 'includes/content.crud');
1471 foreach (node_get_types('types', NULL
, TRUE
) as
$type_name => $data) {
1472 $type = (array) $data;
1473 $type['url_str'] = str_replace('_', '-', $type['type']);
1474 $type['fields'] = array();
1475 $type['tables'] = array();
1476 if ($fields = content_field_instance_read(array('type_name' => $type_name))) {
1477 foreach ($fields as
$field) {
1478 $db_info = content_database_info($field);
1479 $type['tables'][$db_info['table']] = $db_info['table'];
1481 // Allow external modules to translate field strings.
1482 $field_strings = array(
1483 'widget_label' => $field['widget']['label'],
1484 'widget_description' => $field['widget']['description'],
1486 drupal_alter('content_field_strings', $field_strings, $field['type_name'], $field['field_name']);
1487 $field['widget']['label'] = $field_strings['widget_label'];
1488 $field['widget']['description'] = $field_strings['widget_description'];
1490 $type['fields'][$field['field_name']] = $field;
1491 // This means that content_fields($field_name) (no type name)
1492 // returns the last instance loaded.
1493 $info['fields'][$field['field_name']] = $field;
1495 // Make sure the per-type table is added, even if no field is actually
1497 $table = _content_tablename($type['type'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE
);
1498 $type['tables'][$table] = $table;
1501 // Gather information about non-CCK 'fields'.
1502 $extra = module_invoke_all('content_extra_fields', $type_name);
1503 drupal_alter('content_extra_fields', $extra, $type_name);
1504 // Add saved weights.
1505 foreach (variable_get('content_extra_weights_'.
$type_name, array()) as
$key => $value) {
1506 // Some stored entries might not exist anymore, for instance if uploads
1507 // have been disabled, or vocabularies removed...
1508 if (isset($extra[$key])) {
1509 $extra[$key]['weight'] = $value;
1512 $type['extra'] = $extra;
1514 $info['content types'][$type_name] = $type;
1517 cache_set('content_type_info:'.
$language->language
, $info, content_cache_tablename());
1524 * Implementation of hook_node_type()
1525 * React to change in node types
1527 function content_node_type($op, $info) {
1530 module_load_include('inc', 'content', 'includes/content.crud');
1531 content_type_create($info);
1534 module_load_include('inc', 'content', 'includes/content.crud');
1535 content_type_update($info);
1538 module_load_include('inc', 'content', 'includes/content.crud');
1539 content_type_delete($info);
1545 * Clear the cache of content_types; called in several places when content
1546 * information is changed.
1548 function content_clear_type_cache($rebuild_schema = FALSE
) {
1549 cache_clear_all('*', content_cache_tablename(), TRUE
);
1550 _content_type_info(TRUE
);
1552 // Refresh the schema to pick up new information.
1553 if ($rebuild_schema) {
1554 $schema = drupal_get_schema(NULL
, TRUE
);
1557 if (module_exists('views')) {
1558 // Needed because this can be called from .install files
1559 module_load_include('module', 'views');
1560 views_invalidate_cache();
1565 * Retrieve the database storage location(s) for a field.
1567 * TODO: add a word about why it's not included in the global _content_type_info array.
1570 * The field whose database information is requested.
1572 * An array with the keys:
1573 * "table": The name of the database table where the field data is stored.
1574 * "columns": An array of columns stored for this field. Each is a collection
1575 * of information returned from hook_field_settings('database columns'),
1576 * with the addition of a "column" attribute which holds the name of the
1577 * database column that stores the data.
1579 function content_database_info($field) {
1581 if ($field['db_storage'] == CONTENT_DB_STORAGE_PER_FIELD
) {
1582 $db_info['table'] = _content_tablename($field['field_name'], CONTENT_DB_STORAGE_PER_FIELD
);
1585 $db_info['table'] = _content_tablename($field['type_name'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE
);
1588 $db_info['columns'] = (array) $field['columns'];
1589 // Generate column names for this field from generic column names.
1590 foreach ($db_info['columns'] as
$column_name => $attributes) {
1591 $db_info['columns'][$column_name]['column'] = $field['field_name'] .
'_'.
$column_name;
1598 * Helper function for identifying the storage type for a field.
1600 function content_storage_type($field) {
1601 if ($field['multiple'] > 0) {
1602 return CONTENT_DB_STORAGE_PER_FIELD
;
1605 module_load_include('inc', 'content', 'includes/content.crud');
1606 $instances = content_field_instance_read(array('field_name' => $field['field_name']));
1607 if (count($instances) > 1) {
1608 return CONTENT_DB_STORAGE_PER_FIELD
;
1611 return CONTENT_DB_STORAGE_PER_CONTENT_TYPE
;
1615 * Manipulate a 2D array to reverse rows and columns.
1617 * The default data storage for fields is delta first, column names second.
1618 * This is sometimes inconvenient for field modules, so this function can be
1619 * used to present the data in an alternate format.
1622 * The array to be transposed. It must be at least two-dimensional, and
1623 * the subarrays must all have the same keys or behavior is undefined.
1625 * The transposed array.
1627 function content_transpose_array_rows_cols($array) {
1629 if (is_array($array)) {
1630 foreach ($array as
$key1 => $value1) {
1631 if (is_array($value1)) {
1632 foreach ($value1 as
$key2 => $value2) {
1633 if (!isset($result[$key2])) {
1634 $result[$key2] = array();
1636 $result[$key2][$key1] = $value2;
1645 * Helper function to flatten an array of allowed values.
1648 * A single or multidimensional array.
1650 * A flattened array.
1652 function content_array_flatten($array) {
1654 if (is_array($array)) {
1655 foreach ($array as
$key => $value) {
1656 if (is_array($value)) {
1657 $result += content_array_flatten($value);
1660 $result[$key] = $value;
1668 * Create an array of the allowed values for this field.
1670 * Used by number and text fields, expects to find either
1671 * PHP code that will return the correct value, or a string
1672 * with keys and labels separated with '|' and with each
1673 * new value on its own line.
1676 * The field whose allowed values are requested.
1678 * Optional. Use TRUE to return a flattened array (default).
1679 * FALSE can be used to support optgroups for select widgets
1680 * when allowed values list is generated using PHP code.
1682 function content_allowed_values($field, $flatten = TRUE
) {
1683 static
$allowed_values;
1685 $cid = $field['field_name'] .
':'.
($flatten ?
'1' : '0');
1686 if (isset($allowed_values[$cid])) {
1687 return $allowed_values[$cid];
1690 $allowed_values[$cid] = array();
1692 if (isset($field['allowed_values_php'])) {
1694 $result = eval($field['allowed_values_php']);
1695 if (is_array($result)) {
1697 $result = content_array_flatten($result);
1699 $allowed_values[$cid] = $result;
1704 if (empty($allowed_values[$cid]) && isset($field['allowed_values'])) {
1705 $list = explode("\n", $field['allowed_values']);
1706 $list = array_map('trim', $list);
1707 $list = array_filter($list, 'strlen');
1708 foreach ($list as
$opt) {
1709 // Sanitize the user input with a permissive filter.
1710 $opt = content_filter_xss($opt);
1711 if (strpos($opt, '|') !== FALSE
) {
1712 list($key, $value) = explode('|', $opt);
1713 $allowed_values[$cid][$key] = (isset($value) && $value !=='') ?
$value : $key;
1716 $allowed_values[$cid][$opt] = $opt;
1719 // Allow external modules to translate allowed values list.
1720 drupal_alter('content_allowed_values', $allowed_values[$cid], $field);
1722 return $allowed_values[$cid];
1726 * Filter out HTML from allowed values array while leaving entities unencoded.
1728 * @see content_allowed_values()
1729 * @see optionwidgets_select_process()
1730 * @see content_handler_filter_many_to_one::allowed_values()
1732 function content_allowed_values_filter_html(&$options) {
1733 foreach ($options as
$key => $opt) {
1734 if (is_array($opt)) {
1735 content_allowed_values_filter_html($options[$key]);
1738 $options[$key] = html_entity_decode(strip_tags($opt), ENT_QUOTES
);
1744 * Like filter_xss_admin(), but with a shorter list of allowed tags.
1746 * Used for items entered by administrators, like field descriptions,
1747 * allowed values, where some (mainly inline) mark-up may be desired
1748 * (so check_plain() is not acceptable).
1750 function content_filter_xss($string) {
1751 return filter_xss($string, _content_filter_xss_allowed_tags());
1755 * List of tags allowed by content_filter_xss().
1757 function _content_filter_xss_allowed_tags() {
1758 return array('a', 'b', 'big', 'code', 'del', 'em', 'i', 'ins', 'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
1762 * Human-readable list of allowed tags, for display in help texts.
1764 function _content_filter_xss_display_allowed_tags() {
1765 return '<'.
implode('> <', _content_filter_xss_allowed_tags()) .
'>';
1769 * Format a field item for display.
1771 * Used to display a field's values outside the context of the $node, as
1772 * when fields are displayed in Views, or to display a field in a template
1773 * using a different formatter than the one set up on the Display Fields tab
1774 * for the node's context.
1777 * Either a field array or the name of the field.
1779 * The field item(s) to be formatted (such as $node->field_foo[0],
1780 * or $node->field_foo if the formatter handles multiple values itself)
1781 * @param $formatter_name
1782 * The name of the formatter to use.
1784 * Optionally, the containing node object for context purposes and
1785 * field-instance options.
1788 * A string containing the contents of the field item(s) sanitized for display.
1789 * It will have been passed through the necessary check_plain() or check_markup()
1790 * functions as necessary.
1792 function content_format($field, $item, $formatter_name = 'default', $node = NULL
) {
1793 if (!is_array($field)) {
1794 $field = content_fields($field);
1797 if (content_access('view', $field, NULL
, $node) && $formatter = _content_get_formatter($formatter_name, $field['type'])) {
1798 $theme = $formatter['module'] .
'_formatter_'.
$formatter_name;
1802 '#field_name' => $field['field_name'],
1803 '#type_name' => isset($node->type
) ?
$node->type
:'',
1804 '#formatter' => $formatter_name,
1806 '#delta' => isset($item['#delta']) ?
$item['#delta'] : NULL
,
1809 if (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE
) {
1810 // Single value formatter.
1812 // hook_field('sanitize') expects an array of items, so we build one.
1813 $items = array($item);
1814 $function = $field['module'] .
'_field';
1815 if (function_exists($function)) {
1816 $function('sanitize', $node, $field, $items, FALSE
, FALSE
);
1819 $element['#item'] = $items[0];
1822 // Multiple values formatter.
1824 $function = $field['module'] .
'_field';
1825 if (function_exists($function)) {
1826 $function('sanitize', $node, $field, $items, FALSE
, FALSE
);
1829 foreach ($items as
$delta => $item) {
1830 $element[$delta] = array(
1832 '#weight' => $delta,
1837 return theme($theme, $element);
1842 * Registry of available node build modes.
1845 * Determines what information should be returned.
1847 * Depending on the value of the $selector parameter:
1848 * - NULL: a flat list of all available build modes.
1849 * The other two options are mainly used internally by CCK's UI:
1850 * - '_tabs': the list of tabs to be shown on the 'Display fields' screens.
1851 * - a string tab id: the build modes in this tab.
1853 function content_build_modes($selector = NULL
) {
1856 if (!isset($info)) {
1858 foreach (module_implements('content_build_modes') as
$module) {
1859 $function = $module .
'_content_build_modes';
1860 $data = array_merge($data, (array) $function());
1863 foreach ($data as
$tab) {
1864 // Use the + operator to preserve numeric indexes (core build modes).
1865 $flat += (array) $tab['build modes'];
1867 $info = array('tabs' => $data, 'build modes' => $flat);
1870 if ($selector === '_tabs') {
1871 return $info['tabs'];
1873 elseif (isset($selector) && isset($info['tabs'][$selector])) {
1874 return isset($info['tabs'][$selector]) ?
$info['tabs'][$selector]['build modes'] : array();
1877 return $info['build modes'];
1882 * Implementations of hook_content_build_modes
1883 * on behalf of core modules.
1886 * An array describing the build modes used by the module.
1887 * They are grouped by secondary tabs on CCK's 'Display fields' screens.
1891 * // The first level keys (tab1_url, tab2_url) will be used to generate
1892 * // the url of the tab: admin/content/node-type/[type_name]/display/[tab1_url]
1893 * // A module can add its render modes to a tab defined by another module.
1894 * // In this case, there's no need to provide a 'title' for this tab.
1895 * 'tab1_url' => array(
1896 * 'title' => t('The human-readable title of the tab'),
1897 * 'build modes' => array(
1898 * // The keys of the 'context' array are the values used in $node->build_mode.
1899 * 'mymodule_mode1' => array(
1900 * 'title' => t('The human-readable name of the build mode'),
1901 * // The 'views style' property determines if the render mode should be
1902 * // available as an option in Views' 'node' row style (not implemented yet).
1903 * 'views style' => TRUE,
1905 * 'mymodule_mode2' => array(
1906 * 'title' => t('Mode 2'),
1907 * 'views style' => TRUE,
1911 * 'tab2_url' => array(
1916 function node_content_build_modes() {
1919 'title' => t('Basic'),
1920 'build modes' => array(
1922 'title' => t('Teaser'),
1923 'views style' => TRUE
,
1926 'title' => t('Full node'),
1927 'views style' => TRUE
,
1932 'title' => t('RSS'),
1933 'build modes' => array(
1934 NODE_BUILD_RSS
=> array(
1935 'title' => t('RSS'),
1936 'views style' => FALSE
,
1942 function search_content_build_modes() {
1945 'title' => t('Search'),
1946 'build modes' => array(
1947 NODE_BUILD_SEARCH_INDEX
=> array(
1948 'title' => t('Search Index'),
1949 'views style' => FALSE
,
1951 NODE_BUILD_SEARCH_RESULT
=> array(
1952 'title' => t('Search Result'),
1953 'views style' => FALSE
,
1959 function book_content_build_modes() {
1962 'title' => t('Print'),
1963 'build modes' => array(
1964 NODE_BUILD_PRINT
=> array(
1965 'title' => t('Print'),
1966 'views style' => TRUE
,
1974 * Generate a table name for a field or a content type.
1977 * The name of the content type or content field
1979 * CONTENT_DB_STORAGE_PER_FIELD or CONTENT_DB_STORAGE_PER_CONTENT_TYPE
1981 * A string containing the generated name for the database table
1983 function _content_tablename($name, $storage, $version = NULL
) {
1984 if (is_null($version)) {
1985 $version = variable_get('content_schema_version', 0);
1988 if ($version < 1003) {
1995 $name = str_replace('-', '_', $name);
1996 switch ("$version-$storage") {
1997 case
'0-'. CONTENT_DB_STORAGE_PER_CONTENT_TYPE
:
1998 return "node_$name";
1999 case
'0-'. CONTENT_DB_STORAGE_PER_FIELD
:
2000 return "node_data_$name";
2001 case
'1003-'. CONTENT_DB_STORAGE_PER_CONTENT_TYPE
:
2002 return "content_type_$name";
2003 case
'1003-'. CONTENT_DB_STORAGE_PER_FIELD
:
2004 return "content_$name";
2009 * Generate table name for the content field table.
2011 * Needed because the table name changes depending on version.
2012 * Using 'content_node_field' instead of 'content_field'
2013 * to avoid conflicts with field tables that will be prefixed
2014 * with 'content_field'.
2016 function content_field_tablename($version = NULL
) {
2017 if (is_null($version)) {
2018 $version = variable_get('content_schema_version', 0);
2020 return $version < 6001 ?
'node_field' : 'content_node_field';
2024 * Generate table name for the content field instance table.
2026 * Needed because the table name changes depending on version.
2028 function content_instance_tablename($version = NULL
) {
2029 if (is_null($version)) {
2030 $version = variable_get('content_schema_version', 0);
2032 return $version < 6001 ?
'node_field_instance' : 'content_node_field_instance';
2036 * Generate table name for the content cache table.
2038 * Needed because the table name changes depending on version. Because of
2039 * a new database column, the content_cache table will be unusable until
2040 * update 6000 runs, so the cache table will be used instead.
2042 function content_cache_tablename() {
2043 if (variable_get('content_schema_version', -1) < 6000) {
2047 return 'cache_content';
2052 * A basic schema used by all field and type tables.
2054 * This will only add the columns relevant for the specified field.
2055 * Leave $field['columns'] empty to get only the base schema,
2056 * otherwise the function will return the whole thing.
2058 function content_table_schema($field = NULL
) {
2061 'vid' => array('type' => 'int', 'unsigned' => TRUE
, 'not null' => TRUE
, 'default' => 0),
2062 'nid' => array('type' => 'int', 'unsigned' => TRUE
, 'not null' => TRUE
, 'default' => 0)
2064 'primary key' => array('vid'),
2066 'nid' => array('nid'),
2070 // Add delta column if needed.
2071 if (!empty($field['multiple'])) {
2072 $schema['fields']['delta'] = array('type' => 'int', 'unsigned' => TRUE
, 'not null' => TRUE
, 'default' => 0);
2073 $schema['primary key'][] = 'delta';
2075 $schema['content fields'] = array();
2077 // Add field columns column if needed.
2078 // This function is called from install files where it is not safe
2079 // to use content_fields() or content_database_info(), so we
2080 // just used the column values stored in the $field.
2081 // We also need the schema to include fields from disabled modules
2082 // or there will be no way to delete those fields.
2084 if (!empty($field['columns'])) {
2085 foreach ($field['columns'] as
$column => $attributes) {
2086 $column_name = $field['field_name'] .
'_'.
$column;
2087 if (isset($attributes['index']) && $attributes['index']) {
2088 $schema['indexes'][$column_name] = array($column_name);
2089 unset($attributes['index']);
2091 unset($attributes['column']);
2092 unset($attributes['sortable']);
2093 $schema['fields'][$column_name] = $attributes;
2095 $schema['content fields'][] = $field['field_name'];
2101 * Checks if an index exists.
2103 * @todo: May we remove this funcion when implemented by Drupal core itself?
2104 * @link http://drupal.org/node/360854
2105 * @link http://dev.mysql.com/doc/refman/5.0/en/extended-show.html
2108 * Name of the table.
2110 * Name of the index.
2112 * TRUE if the table exists. Otherwise FALSE.
2114 function content_db_index_exists($table, $name) {
2116 if ($db_type == 'mysql' || $db_type == 'mysqli') {
2117 if (version_compare(db_version(), '5.0.3') < 0) {
2118 // Earlier versions of MySQL don't support a WHERE clause for SHOW.
2119 $result = db_query('SHOW INDEX FROM {'.
$table .
'}');
2120 while ($row = db_fetch_array($result)) {
2121 if ($row['Key_name'] == $name) {
2127 return (bool
)db_result(db_query("SHOW INDEX FROM {".
$table .
"} WHERE key_name = '$name'"));
2129 elseif ($db_type == 'pgsql') {
2130 // Note that the index names in Schema API for PostgreSQL are prefixed by
2131 // the table name and suffixed by '_idx'.
2132 return (bool
)db_result(db_query("SELECT COUNT(indexname) FROM pg_indexes WHERE indexname = '{".
$table .
"}_{$name}_idx'"));
2138 * Helper function for determining the behavior of a field or a widget
2139 * with respect to a given operation. (currently used for field 'view',
2140 * and widget 'default values' and 'multiple values')
2143 * 'field' or 'widget'
2145 * the name of the operation ('view', 'default value'...)
2147 * The field array, including widget info.
2149 * CONTENT_CALLBACK_NONE - do nothing for this operation
2150 * CONTENT_CALLBACK_CUSTOM - use the module's callback function.
2151 * CONTENT_CALLBACK_DEFAULT - use content module default behavior
2154 function content_callback($entity, $op, $field) {
2157 $info = module_invoke($field['module'], "field_info");
2158 return isset($info[$field['type']]['callbacks'][$op]) ?
$info[$field['type']]['callbacks'][$op] : CONTENT_CALLBACK_DEFAULT
;
2161 $info = module_invoke($field['widget']['module'], "widget_info");
2162 return isset($info[$field['widget']['type']]['callbacks'][$op]) ?
$info[$field['widget']['type']]['callbacks'][$op] : CONTENT_CALLBACK_DEFAULT
;
2167 * Helper function for determining the handling of a field, widget or
2168 * formatter with respect to a given operation.
2170 * Currently used for widgets and formatters 'multiple values'.
2173 * 'field', 'widget' or 'formatter'
2175 * the name of the operation ('default values'...)
2177 * - if $entity is 'field' or 'widget': the field array,
2178 * including widget info.
2179 * - if $entity is 'formater': the formatter array.
2181 * CONTENT_HANDLE_CORE - the content module handles this operation.
2182 * CONTENT_HANDLE_MODULE - the implementing module handles this operation.
2184 function content_handle($entity, $op, $object) {
2187 $info = module_invoke($object['module'], "field_info");
2188 return isset($info[$object['type']][$op]) ?
$info[$object['type']][$op] : CONTENT_HANDLE_CORE
;
2191 $info = module_invoke($object['widget']['module'], "widget_info");
2192 return isset($info[$object['widget']['type']][$op]) ?
$info[$object['widget']['type']][$op] : CONTENT_HANDLE_CORE
;
2195 // Much simpler, formatters arrays *are* the 'formatter_info' itself.
2196 // We let content_handle deal with them only for code consistency.
2197 return isset($object[$op]) ?
$object[$op] : CONTENT_HANDLE_CORE
;
2202 * Helper function to return the correct default value for a field.
2209 * The value of the field in the node.
2211 * The default value for that field.
2213 function content_default_value(&$form, &$form_state, $field, $delta) {
2214 $widget_types = _content_widget_types();
2215 $module = $widget_types[$field['widget']['type']]['module'];
2217 $default_value = array();
2218 if (!empty($field['widget']['default_value_php'])) {
2220 $result = eval($field['widget']['default_value_php']);
2222 if (is_array($result)) {
2223 $default_value = $result;
2226 elseif (!empty($field['widget']['default_value'])) {
2227 $default_value = $field['widget']['default_value'];
2229 return (array) $default_value;
2233 * Determine whether the user has access to a given field.
2236 * The operation to be performed. Possible values:
2240 * The field on which the operation is to be performed.
2242 * (optional) The account to check, if not given use currently logged in user.
2244 * (optional) The node on which the operation is to be performed.
2246 * TRUE if the operation is allowed;
2247 * FALSE if the operation is denied.
2249 function content_access($op, $field, $account = NULL
, $node = NULL
) {
2252 if (is_null($account)) {
2256 $access = module_invoke_all('field_access', $op, $field, $account, $node);
2257 foreach ($access as
$value) {
2258 if ($value === FALSE
) {
2266 * Hide specified fields from the $content variable in node templates.
2268 function content_field_wrapper_post_render($content, $element) {
2269 $field = content_fields($element['#field_name'], $element['#type_name']);
2270 if (theme('content_exclude', $content, $field, $element['#context'])) {
2278 * 'Theme' function for a field's addition to $content.
2280 * Adapts the all-inclusive $content variable in node templates to allow
2281 * some field content to be excluded. This is a theme function, so it can be
2282 * overridden in different themes to produce different results.
2284 * The html for individual fields and groups are available in the
2285 * $FIELD_NAME_rendered and $GROUP_NAME_rendered variables.
2287 * This allows more flexibility in node templates : you can use custom markup
2288 * around a few specific fields, and print the rest of the node with $content.
2291 * The themed content for this field or group.
2294 * The field or group array for this item.
2295 * $object['#type_name'] holds the content type.
2296 * $object['#field_name'] holds the field name (if a field).
2297 * $object['#group_name'] holds the group name (if a group).
2298 * $object['display_settings'] holds the display settings
2299 * for all contexts, in an array like:
2300 * $object['display_settings'] => array(
2302 * 'format' => 'default',
2305 * 'teaser' => array(
2306 * 'format' => 'default',
2312 * The context for which the node is being rendered.
2313 * Can be one of the following values :
2316 * - NODE_BUILD_SEARCH_INDEX
2317 * - NODE_BUILD_SEARCH_RESULT
2319 * - NODE_BUILD_PRINT
2320 * - ... any other custom build mode exposed by 3rd party modules using
2321 * hook_content_build_modes().
2324 * Whether or not content is to be added to $content in this context.
2325 * Uses the value of the 'Exclude' checkbox for this field
2326 * as set on the Manage fields screen.
2328 function theme_content_exclude($content, $object, $context) {
2329 // The field may be missing info for $contexts added by modules
2330 // enabled after the field was last edited.
2331 if (empty($object['display_settings'])
2332 || empty($object['display_settings'][$context])
2333 || !is_array($object['display_settings'][$context])
2334 || empty($object['display_settings'][$context]['exclude'])) {
2343 * Theme preprocess function for field.tpl.php.
2345 * The $variables array contains the following arguments:
2352 * @see field.tpl.php
2354 * TODO : this should live in theme/theme.inc, but then the preprocessor
2355 * doesn't get called when the theme overrides the template. Bug in theme layer ?
2357 function content_preprocess_content_field(&$variables) {
2358 $element = $variables['element'];
2359 $field = content_fields($element['#field_name'], $element['#node']->type
);
2361 $variables['node'] = $element['#node'];
2362 $variables['field'] = $field;
2363 $variables['items'] = array();
2365 if ($element['#single']) {
2366 // Single value formatter.
2367 foreach (element_children($element['items']) as
$delta) {
2368 $variables['items'][$delta] = $element['items'][$delta]['#item'];
2369 // Use isset() to avoid undefined index message on #children when field values are empty.
2370 $variables['items'][$delta]['view'] = isset($element['items'][$delta]['#children']) ?
$element['items'][$delta]['#children'] : '';
2374 // Multiple values formatter.
2375 // We display the 'all items' output as $items[0], as if it was the
2376 // output of a single valued field.
2377 // Raw values are still exposed for all items.
2378 foreach (element_children($element['items']) as
$delta) {
2379 $variables['items'][$delta] = $element['items'][$delta]['#item'];
2381 $variables['items'][0]['view'] = $element['items']['#children'];
2384 $variables['teaser'] = $element['#teaser'];
2385 $variables['page'] = $element['#page'];
2387 $field_empty = TRUE
;
2389 foreach ($variables['items'] as
$delta => $item) {
2390 if (!isset($item['view']) || (empty($item['view']) && (string)$item['view'] !== '0')) {
2391 $variables['items'][$delta]['empty'] = TRUE
;
2394 $field_empty = FALSE
;
2395 $variables['items'][$delta]['empty'] = FALSE
;
2400 'field_type' => $field['type'],
2401 'field_name' => $field['field_name'],
2402 'field_type_css' => strtr($field['type'], '_', '-'),
2403 'field_name_css' => strtr($field['field_name'], '_', '-'),
2404 'label' => check_plain(t($field['widget']['label'])),
2405 'label_display' => $element['#label_display'],
2406 'field_empty' => $field_empty,
2407 'template_files' => array(
2409 'content-field-'.
$element['#field_name'],
2410 'content-field-'.
$element['#node']->type
,
2411 'content-field-'.
$element['#field_name'] .
'-'.
$element['#node']->type
,
2414 $variables = array_merge($variables, $additions);
2418 * Theme preprocess function for node.
2420 * - Adds $FIELD_NAME_rendered variables
2421 * containing the themed output for the whole field.
2422 * - Adds the formatted values in the 'view' key of the items.
2424 function content_preprocess_node(&$vars) {
2425 $additions = _content_field_invoke_default('preprocess_node', $vars['node']);
2426 $vars = array_merge($vars, $additions);
2430 * Debugging using hook_content_fieldapi.
2432 * @TODO remove later
2437 function content_content_fieldapi($op, $field) {
2438 if (module_exists('devel')) {
2445 * Implementation of hook_content_extra_fields.
2447 * Informations for non-CCK 'node fields' defined in core.
2449 function content_content_extra_fields($type_name) {
2450 $type = node_get_types('type', $type_name);
2453 if ($type->has_title
) {
2454 $extra['title'] = array(
2455 'label' => $type->title_label
,
2456 'description' => t('Node module form.'),
2460 if ($type->has_body
) {
2461 $extra['body_field'] = array(
2462 'label' => $type->body_label
,
2463 'description' => t('Node module form.'),
2468 $extra['revision_information'] = array(
2469 'label' => t('Revision information'),
2470 'description' => t('Node module form.'),
2473 if (module_exists('comment')) {
2474 $extra['comment_settings'] = array(
2475 'label' => t('Comment settings'),
2476 'description' => t('Comment module form.'),
2480 if (module_exists('locale') && variable_get("language_content_type_$type_name", 0)) {
2481 $extra['language'] = array(
2482 'label' => t('Language'),
2483 'description' => t('Locale module form.'),
2487 if (module_exists('translation') && translation_supported_type($type_name)) {
2488 $extra['translation'] = array(
2489 'label' => t('Translation settings'),
2490 'description' => t('Translation module form.'),
2494 if (module_exists('menu')) {
2495 $extra['menu'] = array(
2496 'label' => t('Menu settings'),
2497 'description' => t('Menu module form.'),
2501 if (module_exists('taxonomy') && taxonomy_get_vocabularies($type_name)) {
2502 $extra['taxonomy'] = array(
2503 'label' => t('Taxonomy'),
2504 'description' => t('Taxonomy module form.'),
2508 if (module_exists('book')) {
2509 $extra['book'] = array(
2510 'label' => t('Book'),
2511 'description' => t('Book module form.'),
2515 if (module_exists('path')) {
2516 $extra['path'] = array(
2517 'label' => t('Path settings'),
2518 'description' => t('Path module form.'),
2522 if ($type_name == 'poll' && module_exists('poll')) {
2523 $extra['title'] = array(
2524 'label' => t('Poll title'),
2525 'description' => t('Poll module title.'),
2528 $extra['choice_wrapper'] = array(
2529 'label' => t('Poll choices'),
2530 'description' => t('Poll module choices.'),
2533 $extra['settings'] = array(
2534 'label' => t('Poll settings'),
2535 'description' => t('Poll module settings.'),
2539 if (module_exists('upload') && variable_get("upload_$type_name", TRUE
)) {
2540 $extra['attachments'] = array(
2541 'label' => t('File attachments'),
2542 'description' => t('Upload module form.'),
2552 * Retrieve the user-defined weight for non-CCK node 'fields'.
2554 * CCK's 'Manage fields' page lets users reorder node fields, including non-CCK
2555 * items (body, taxonomy, other hook_nodeapi-added elements by contrib modules...).
2556 * Contrib modules that want to have their 'fields' supported need to expose
2557 * them with hook_content_extra_fields, and use this function to retrieve the
2558 * user-defined weight.
2561 * The content type name.
2562 * @param $pseudo_field_name
2563 * The name of the 'field'.
2565 * The weight for the 'field', respecting the user settings stored
2566 * by content.module.
2568 function content_extra_field_weight($type_name, $pseudo_field_name) {
2569 $type = content_types($type_name);
2571 // If we don't have the requested item, this may be because the cached
2572 // information for 'extra' fields hasn't been refreshed yet.
2573 if (!isset($type['extra'][$pseudo_field_name])) {
2574 content_clear_type_cache();
2575 $type = content_types($type_name);
2578 if (isset($type['extra'][$pseudo_field_name])) {
2579 return $type['extra'][$pseudo_field_name]['weight'];
2584 * Find max delta value actually in use for a field.
2586 * Helper function to do things like tell when we should prevent a
2587 * change in multiple value settings that would result in data loss,
2588 * or know if content actually exists for a field.
2590 * @param $field_name
2591 * The field name to examine.
2593 * If provided, search only for existing data in that type,
2594 * otherwise search for all instances of field data in all types.
2596 * NULL if field is not in use, or the maximum delta value in use.
2599 * Go back to the field settings validation and use this function
2600 * to prevent (or confirm) changes in multiple values that
2601 * would destroy data.
2603 * Fields with only NULL data will show up as being in use.
2604 * Do we want to eliminate them from the results?
2606 function content_max_delta($field_name, $type_name = NULL
) {
2607 $fields = content_fields();
2608 $field = $fields[$field_name];
2610 // Non-multiple value fields don't use the delta column,
2611 // but could exist in multiple databases. If any value
2612 // exists in any examined table, the max delta will be zero.
2613 if (empty($field['multiple'])) {
2614 $content_types = content_types();
2615 foreach ($content_types as
$content_type) {
2616 if (empty($type_name) || $content_type['type'] == $type_name) {
2617 foreach ($content_type['fields'] as
$field) {
2618 $db_info = content_database_info($field);
2619 if (db_result(db_query("SELECT COUNT(*) FROM {".
$db_info['table'] .
"}")) >= 1) {
2626 // Multiple value fields always share the same table and use the delta.
2627 // If we want to find delta values for a particular type, we join
2628 // in the node table to limit the type.
2630 $db_info = content_database_info($field);
2631 if (!empty($type_name)) {
2632 $delta = db_result(db_query("SELECT MAX(delta) FROM {".
$db_info['table'] .
"} f LEFT JOIN {node} n ON f.vid = n.vid WHERE n.type = '%s'", $type_name));
2635 $delta = db_result(db_query("SELECT MAX(delta) FROM {".
$db_info['table'] .
"}"));
2641 // If we got this far, there is no data for this field.
2646 * Helper function to identify inactive fields.
2648 function content_inactive_fields($type_name = NULL
) {
2649 module_load_include('inc', 'content', 'includes/content.crud');
2650 if (!empty($type_name)) {
2651 $param = array('type_name' => $type_name);
2652 $inactive = array($type_name => array());
2656 $inactive = array();
2658 $all = content_field_instance_read($param, TRUE
);
2659 $active = array_keys(content_fields());
2660 foreach ($all as
$field) {
2661 if (!in_array($field['field_name'], $active)) {
2662 $inactive[$field['type_name']][$field['field_name']] = content_field_instance_expand($field);
2665 if (!empty($type_name)) {
2666 return $inactive[$type_name];