You are here

function block_content_add_body_field in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/block_content/block_content.module \block_content_add_body_field()

Adds the default body field to a custom block type.

Parameters

string $block_type_id: Id of the block type.

string $label: (optional) The label for the body instance. Defaults to 'Body'

Return value

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

4 calls to block_content_add_body_field()
BlockContentCacheTagsTest::createEntity in core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php
Creates the entity to be tested.
BlockContentTestBase::createBlockContentType in core/modules/block_content/src/Tests/BlockContentTestBase.php
Creates a custom block type (bundle).
BlockContentTestBase::createBlockContentType in core/modules/block_content/src/Tests/Views/BlockContentTestBase.php
Creates a custom block type (bundle).
BlockContentTypeForm::save in core/modules/block_content/src/BlockContentTypeForm.php
Form submission handler for the 'save' action.

File

core/modules/block_content/block_content.module, line 78
Allows the creation of custom blocks through the user interface.

Code

function block_content_add_body_field($block_type_id, $label = 'Body') {

  // Add or remove the body field, as needed.
  $field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
  if (empty($field)) {
    $field = entity_create('field_config', array(
      'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array(
        'display_summary' => FALSE,
      ),
    ));
    $field
      ->save();

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

    // Assign display settings for 'default' view mode.
    entity_get_display('block_content', $block_type_id, 'default')
      ->setComponent('body', array(
      'label' => 'hidden',
      'type' => 'text_default',
    ))
      ->save();
  }
  return $field;
}