diff --git a/core/modules/block_content/block_content.module b/core/modules/block_content/block_content.module index 2b0e785..ac45e62 100644 --- a/core/modules/block_content/block_content.module +++ b/core/modules/block_content/block_content.module @@ -62,3 +62,45 @@ function block_content_entity_type_alter(array &$entity_types) { $entity_types['block_content']->set('translation', $translation); } } + +/** + * Adds the default body field to a custom block type. + * + * @param string $block_type_id + * Id of the block type. + * @param string $label + * (optional) The label for the body instance. Defaults to 'Body' + * + * @return array() + * Body field. + */ +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; +} diff --git a/core/modules/block_content/src/BlockContentTypeForm.php b/core/modules/block_content/src/BlockContentTypeForm.php index e9589d6..f4131f2 100644 --- a/core/modules/block_content/src/BlockContentTypeForm.php +++ b/core/modules/block_content/src/BlockContentTypeForm.php @@ -101,6 +101,7 @@ public function save(array $form, FormStateInterface $form_state) { $logger->notice('Custom block type %label has been updated.', array('%label' => $block_type->label(), 'link' => $edit_link)); } else { + block_content_add_body_field($block_type->id()); $this->addBodyField($block_type); drupal_set_message(t('Custom block type %label has been added.', array('%label' => $block_type->label()))); $logger->notice('Custom block type %label has been added.', array('%label' => $block_type->label(), 'link' => $edit_link)); diff --git a/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php b/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php index 8df5067..1ea2156 100644 --- a/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php @@ -33,28 +33,8 @@ protected function createEntity() { 'revision' => FALSE )); $block_content_type->save(); - $field = entity_create('field_config', array( - // The field storage is guaranteed to exist because it is supplied by the - // block_content module. - 'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'), - 'bundle' => $block_content_type->id(), - 'label' => 'Body', - 'settings' => array('display_summary' => FALSE), - )); - $field->save(); - // Assign widget settings for the 'default' form mode. - entity_get_form_display('block_content', $block_content_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_content_type->id(), 'default') - ->setComponent('body', array( - 'label' => 'hidden', - 'type' => 'text_default', - )) - ->save(); + block_content_add_body_field($block_content_type->id()); + // Create a "Llama" custom block. $block_content = entity_create('block_content', array( 'info' => 'Llama', diff --git a/core/modules/block_content/src/Tests/BlockContentTestBase.php b/core/modules/block_content/src/Tests/BlockContentTestBase.php index 013793c..457b86e 100644 --- a/core/modules/block_content/src/Tests/BlockContentTestBase.php +++ b/core/modules/block_content/src/Tests/BlockContentTestBase.php @@ -50,30 +50,8 @@ protected function setUp() { parent::setUp(); // Ensure the basic bundle exists. This is provided by the standard profile. $block_content_type = $this->createBlockContentType('basic'); - $field = entity_create('field_config', array( - // The field storage is guaranteed to exist because it is supplied by the - // block_content module. - 'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'), - 'bundle' => $block_content_type->id(), - 'label' => 'Body', - 'settings' => array('display_summary' => FALSE), - )); - $field->save(); - - // Assign widget settings for the 'default' form mode. - entity_get_form_display('block_content', $block_content_type->id(), 'default') - ->setComponent('body', array( - 'type' => 'text_textarea_with_summary', - )) - ->save(); + block_content_add_body_field($block_content_type->id()); - // Assign display settings for 'default' view mode. - entity_get_display('block_content', $block_content_type->id(), 'default') - ->setComponent('body', array( - 'label' => 'hidden', - 'type' => 'text_default', - )) - ->save(); $this->adminUser = $this->drupalCreateUser($this->permissions); } diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_type.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_type.yml index 8b3f607..ceaa1a4 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_type.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_type.yml @@ -13,4 +13,4 @@ process: id: 'constants/id' label: 'constants/label' destination: - plugin: entity:block_content_type \ No newline at end of file + plugin: entity:block_content_type