function block_content_add_body_field

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

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.

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 = FieldConfig::create([
      '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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!block_content!block_content.module/function/block_content_add_body_field/8.1.x