8.3.x node.module node_add_body_field(NodeTypeInterface $type, $label = 'Body')
8.0.x node.module node_add_body_field(NodeTypeInterface $type, $label = 'Body')
8.1.x node.module node_add_body_field(NodeTypeInterface $type, $label = 'Body')
8.2.x node.module node_add_body_field(NodeTypeInterface $type, $label = 'Body')
8.4.x node.module node_add_body_field(NodeTypeInterface $type, $label = 'Body')
7.x node.module node_add_body_field($type, $label = 'Body')

Adds the default body field to a node type.

Parameters

\Drupal\node\NodeTypeInterface $type: A node type object.

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

Return value

\Drupal\field\Entity\FieldConfig A Body field object.

12 calls to node_add_body_field()
ConfigImportRecreateTest::testRecreateEntity in core/tests/Drupal/KernelTests/Core/Config/ConfigImportRecreateTest.php
ContentTypeCreationTrait::createContentType in core/modules/simpletest/src/ContentTypeCreationTrait.php
Creates a custom content type based on default settings.
EditorAdminTest::testDisableFormatWithEditor in core/modules/editor/src/Tests/EditorAdminTest.php
Tests format disabling.
EditorFileUsageTest::setUp in core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php
EditorImageDialogTest::setUp in core/modules/editor/tests/src/Kernel/EditorImageDialogTest.php
Sets up the test.

... See full list

File

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

Code

function node_add_body_field(NodeTypeInterface $type, $label = 'Body') {
  // Add or remove the body field, as needed.
  $field_storage = FieldStorageConfig::loadByName('node', 'body');
  $field = FieldConfig::loadByName('node', $type->id(), 'body');
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $type->id(),
      'label' => $label,
      'settings' => array('display_summary' => TRUE),
    ]);
    $field->save();

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

    // Assign display settings for the 'default' and 'teaser' view modes.
    entity_get_display('node', $type->id(), 'default')
      ->setComponent('body', array(
        'label' => 'hidden',
        'type' => 'text_default',
      ))
      ->save();

    // The teaser view mode is created by the Standard profile and therefore
    // might not exist.
    $view_modes = \Drupal::entityManager()->getViewModes('node');
    if (isset($view_modes['teaser'])) {
      entity_get_display('node', $type->id(), 'teaser')
        ->setComponent('body', array(
          'label' => 'hidden',
          'type' => 'text_summary_or_trimmed',
        ))
        ->save();
    }
  }

  return $field;
}