node_configure_fields

Versions
7
node_configure_fields($type)

Manage the field(s) for a node type.

Currently, the node module manages a single Field API field, 'body'. If $type->has_body is true, this function ensures the 'body' field exists and creates an instance of it for the bundle $type->type (e.g. 'page', 'story', ...). If $type->has_body is false, this function removes the instance (if it exists) for the 'body' field on $type->type.

Code

modules/node/node.module, line 554

<?php
function node_configure_fields($type) {
   // Add or remove the body field, as needed.
  $field = field_info_field('body');
  $instance = field_info_instance('node', 'body', $type->type);
  if ($type->has_body) {
    if (empty($field)) {
      $field = array(
        'field_name' => 'body',
        'type' => 'text_with_summary',
        'translatable' => TRUE,
      );
      $field = field_create_field($field);
    }
    if (empty($instance)) {
      $instance = array(
        'field_name' => 'body',
        'object_type' => 'node',
        'bundle' => $type->type,
        'label' => $type->body_label,
        'widget_type' => 'text_textarea_with_summary',
        'settings' => array('display_summary' => TRUE),

        // Define default formatters for the teaser and full view.
        'display' => array(
          'full' => array(
            'label' => 'hidden',
            'type' => 'text_default',
          ),
          'teaser' => array(
            'label' => 'hidden',
            'type' => 'text_summary_or_trimmed',
          ),
        ),
      );
      field_create_instance($instance);
    }
    else {
      $instance['label'] = $type->body_label;
      $instance['settings']['display_summary'] = TRUE;
      field_update_instance($instance);
    }
  }
  elseif (!empty($instance)) {
    field_delete_instance($instance);
  }

  if ($type->has_title) {
    // Add the title field if not present.
    $field = field_info_field('title');
    $instance = field_info_instance('node', 'title', $type->type);

    if (empty($field)) {
      $field = array(
        'field_name' => 'title',
        'type' => 'text',
      );
      $field = field_create_field($field);
    }
    if (empty($instance)) {
      $weight = -5;
      $instance = array(
        'field_name' => 'title',
        'object_type' => 'node',
        'bundle' => $type->type,
        'label' => $type->title_label,
        'widget_type' => 'text',
        'widget' => array(
          'weight' => $weight,
        ),
        'required' => TRUE,
        'locked' => TRUE,
        'display' => array(
          'full' => array(
            'label' => 'hidden',
            'type' => 'text_default',
            'weight' => $weight,
          ),
          'teaser' => array(
            'label' => 'hidden',
            'type' => 'text_default',
            'weight' => $weight,
          ),
        ),
      );
      field_create_instance($instance);
    }
    else {
      $instance['label'] = $type->title_label;
      field_update_instance($instance);
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.