function node_add_body_field

Same name in other branches
  1. 7.x modules/node/node.module \node_add_body_field()
  2. 9 core/modules/node/node.module \node_add_body_field()
  3. 8.9.x core/modules/node/node.module \node_add_body_field()
  4. 11.x core/modules/node/node.module \node_add_body_field()

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.

14 calls to node_add_body_field()
AddToAllBundlesConfigActionTest::testFailIfExists in core/modules/field/tests/src/Kernel/AddToAllBundlesConfigActionTest.php
Tests that the action can be set to fail if the field already exists.
ConfigImportRecreateTest::testRecreateEntity in core/tests/Drupal/KernelTests/Core/Config/ConfigImportRecreateTest.php
ContentTypeCreationTrait::createContentType in core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php
Creates a custom content type based on default settings.
EditorAdminTest::testDisableFormatWithEditor in core/modules/editor/tests/src/Functional/EditorAdminTest.php
Tests format disabling.
EditorFileUsageTest::setUp in core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php

... See full list

File

core/modules/node/node.module, line 272

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' => [
                'display_summary' => TRUE,
                'allowed_formats' => [],
            ],
        ]);
        $field->save();
        
        /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
        $display_repository = \Drupal::service('entity_display.repository');
        // Assign widget settings for the default form mode.
        $display_repository->getFormDisplay('node', $type->id())
            ->setComponent('body', [
            'type' => 'text_textarea_with_summary',
        ])
            ->save();
        // Assign display settings for the 'default' and 'teaser' view modes.
        $display_repository->getViewDisplay('node', $type->id())
            ->setComponent('body', [
            'label' => 'hidden',
            'type' => 'text_default',
        ])
            ->save();
        // The teaser view mode is created by the Standard profile and therefore
        // might not exist.
        $view_modes = \Drupal::service('entity_display.repository')->getViewModes('node');
        if (isset($view_modes['teaser'])) {
            $display_repository->getViewDisplay('node', $type->id(), 'teaser')
                ->setComponent('body', [
                'label' => 'hidden',
                'type' => 'text_summary_or_trimmed',
            ])
                ->save();
        }
    }
    return $field;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.