function node_add_body_field

Adds the default body field to a node type.

Parameters

$type: A node type object.

$label: (optional) The label for the body instance.

Return value

Body field instance.

6 calls to node_add_body_field()
forum_enable in drupal/core/modules/forum/forum.install
Implements hook_enable().
node_type_form_submit in drupal/core/modules/node/content_types.inc
Form submission handler for node_type_form().
standard_install in drupal/core/profiles/standard/standard.install
Implements hook_install().
TaxonomyTestBase::mockStandardInstall in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php
Provides a workaround for the inability to use the standard profile.
WebTestBase::drupalCreateContentType in drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
Creates a custom content type based on default settings.

... See full list

File

drupal/core/modules/node/node.module, line 530
The core module that allows content to be submitted to the site.

Code

function node_add_body_field($type, $label = 'Body') {

  // Add or remove the body field, as needed.
  $field = field_info_field('body');
  $instance = field_info_instance('node', 'body', $type->type);
  if (empty($field)) {
    $field = array(
      'field_name' => 'body',
      'type' => 'text_with_summary',
      'entity_types' => array(
        'node',
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => 'body',
      'entity_type' => 'node',
      'bundle' => $type->type,
      'label' => $label,
      'settings' => array(
        'display_summary' => TRUE,
      ),
    );
    $instance = field_create_instance($instance);

    // Assign widget settings for the 'default' form mode.
    entity_get_form_display('node', $type->type, 'default')
      ->setComponent($field['field_name'], array(
      'type' => 'text_textarea_with_summary',
    ))
      ->save();

    // Assign display settings for the 'default' and 'teaser' view modes.
    entity_get_display('node', $type->type, 'default')
      ->setComponent($field['field_name'], array(
      'label' => 'hidden',
      'type' => 'text_default',
    ))
      ->save();
    entity_get_display('node', $type->type, 'teaser')
      ->setComponent($field['field_name'], array(
      'label' => 'hidden',
      'type' => 'text_summary_or_trimmed',
    ))
      ->save();
  }
  return $instance;
}