diff --git a/core/modules/block_content/block_content.module b/core/modules/block_content/block_content.module index a71b657..2b0e785 100644 --- a/core/modules/block_content/block_content.module +++ b/core/modules/block_content/block_content.module @@ -62,45 +62,3 @@ 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 8927162..e9589d6 100644 --- a/core/modules/block_content/src/BlockContentTypeForm.php +++ b/core/modules/block_content/src/BlockContentTypeForm.php @@ -10,6 +10,8 @@ use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; /** * Base form for category edit forms. @@ -99,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 { + $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)); } @@ -106,4 +109,36 @@ public function save(array $form, FormStateInterface $form_state) { $form_state->setRedirect('block_content.type_list'); } + /** + * Adds a body field to a block content type. + * + * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type + */ + protected function addBodyField(BlockContentTypeInterface $block_content_type) { + $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(); + } + } diff --git a/core/modules/block_content/src/Entity/BlockContentType.php b/core/modules/block_content/src/Entity/BlockContentType.php index 007ebfb..fa1aba1 100644 --- a/core/modules/block_content/src/Entity/BlockContentType.php +++ b/core/modules/block_content/src/Entity/BlockContentType.php @@ -70,15 +70,4 @@ class BlockContentType extends ConfigEntityBundleBase implements BlockContentTyp */ public $description; - /** - * {@inheritdoc} - */ - public function postSave(EntityStorageInterface $storage, $update = TRUE) { - parent::postSave($storage, $update); - - if (!$update && !$this->isSyncing()) { - block_content_add_body_field($this->id); - } - } - } diff --git a/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php b/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php index 313e426..8df5067 100644 --- a/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php @@ -8,6 +8,7 @@ namespace Drupal\block_content\Tests; use Drupal\Core\Entity\EntityInterface; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\system\Tests\Entity\EntityCacheTagsTestBase; /** @@ -26,12 +27,34 @@ class BlockContentCacheTagsTest extends EntityCacheTagsTestBase { * {@inheritdoc} */ protected function createEntity() { - $bundle = entity_create('block_content_type', array( + $block_content_type = entity_create('block_content_type', array( 'id' => 'basic', 'label' => 'basic', 'revision' => FALSE )); - $bundle->save(); + $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(); // 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 3738106..013793c 100644 --- a/core/modules/block_content/src/Tests/BlockContentTestBase.php +++ b/core/modules/block_content/src/Tests/BlockContentTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\block_content\Tests; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\simpletest\WebTestBase; /** @@ -48,7 +49,31 @@ protected function setUp() { parent::setUp(); // Ensure the basic bundle exists. This is provided by the standard profile. - $this->createBlockContentType('basic'); + $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(); + + // 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/block_content/src/Tests/BlockContentTypeTest.php b/core/modules/block_content/src/Tests/BlockContentTypeTest.php index 76dfdb7..244cf8d 100644 --- a/core/modules/block_content/src/Tests/BlockContentTypeTest.php +++ b/core/modules/block_content/src/Tests/BlockContentTypeTest.php @@ -56,6 +56,9 @@ public function testBlockContentTypeCreation() { $block_type = entity_load('block_content_type', 'foo'); $this->assertTrue($block_type, 'The new block type has been created.'); + $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo'); + $this->assertTrue(isset($field_definitions['body']), 'Body field was created when using the UI to create block content types.'); + // Check that the block type was created in site default language. $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); $this->assertEqual($block_type->language()->getId(), $default_langcode); @@ -69,8 +72,8 @@ public function testBlockContentTypeEditing() { // We need two block types to prevent /block/add redirecting. $this->createBlockContentType('other'); - $field_definition = \Drupal::entityManager()->getFieldDefinitions('block_content', 'other')['body']; - $this->assertEqual($field_definition->getLabel(), 'Body', 'Body field was found.'); + $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'other'); + $this->assertFalse(isset($field_definitions['body']), 'Body field was not created when using the API to create block content types.'); // Verify that title and body fields are displayed. $this->drupalGet('block/add/basic');