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;
}
doc_Drupal
2016-10-29 08:47:52
Comments
Leave a Comment

Please login to continue.