function custom_block_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 'Block body'

Return value

array() Body field instance.

1 call to custom_block_add_body_field()

File

drupal/core/modules/block/custom_block/custom_block.module, line 185
Allows the creaation of custom blocks through the user interface.

Code

function custom_block_add_body_field($block_type_id, $label = 'Block body') {

  // Add or remove the body field, as needed.
  $field = field_info_field('block_body');
  $instance = field_info_instance('custom_block', 'block_body', $block_type_id);
  if (empty($field)) {
    $field = array(
      'field_name' => 'block_body',
      'type' => 'text_with_summary',
      'entity_types' => array(
        'custom_block',
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => 'block_body',
      'entity_type' => 'custom_block',
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array(
        'display_summary' => FALSE,
      ),
    );
    $instance = field_create_instance($instance);

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

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