diff --git a/core/modules/block_content/block_content.module b/core/modules/block_content/block_content.module index dd36852..2b0e785 100644 --- a/core/modules/block_content/block_content.module +++ b/core/modules/block_content/block_content.module @@ -62,54 +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_storage = FieldStorageConfig::loadByName('block_content', 'body'); - $field = FieldConfig::loadByName('block_content', $block_type_id, 'body'); - if (empty($field_storage)) { - $field_storage = entity_create('field_storage_config', array( - 'field_name' => 'body', - 'entity_type' => 'block_content', - 'type' => 'text_with_summary', - )); - $field_storage->save(); - } - if (empty($field)) { - $field = entity_create('field_config', array( - 'field_storage' => $field_storage, - '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/config/install/field.storage.block_content.body.yml b/core/modules/block_content/config/install/field.storage.block_content.body.yml new file mode 100644 index 0000000..b6ede76 --- /dev/null +++ b/core/modules/block_content/config/install/field.storage.block_content.body.yml @@ -0,0 +1,17 @@ +langcode: en +status: true +dependencies: + module: + - block_content + - text +id: block_content.body +field_name: body +entity_type: block_content +type: text_with_summary +settings: { } +module: text +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: true 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 cc7ae4a..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,6 +27,34 @@ class BlockContentCacheTagsTest extends EntityCacheTagsTestBase { * {@inheritdoc} */ protected function createEntity() { + $block_content_type = entity_create('block_content_type', array( + 'id' => 'basic', + 'label' => 'basic', + '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(); // Create a "Llama" custom block. $block_content = entity_create('block_content', array( 'info' => 'Llama', diff --git a/core/modules/block_content/src/Tests/BlockContentCreationTest.php b/core/modules/block_content/src/Tests/BlockContentCreationTest.php index 0262dd4..a93b756 100644 --- a/core/modules/block_content/src/Tests/BlockContentCreationTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCreationTest.php @@ -59,7 +59,7 @@ public function testBlockContentCreation() { // Check that the Basic block has been created. $this->assertRaw(format_string('!block %name has been created.', array( - '!block' => 'Basic block', + '!block' => 'basic', '%name' => $edit['info[0][value]'] )), 'Basic block created.'); @@ -105,7 +105,7 @@ public function testDefaultBlockContentCreation() { // Check that the block has been created and that it is a basic block. $this->assertRaw(format_string('!block %name has been created.', array( - '!block' => 'Basic block', + '!block' => 'basic', '%name' => $edit['info[0][value]'], )), 'Basic block created.'); diff --git a/core/modules/block_content/src/Tests/BlockContentListTest.php b/core/modules/block_content/src/Tests/BlockContentListTest.php index 07a3299..c644286 100644 --- a/core/modules/block_content/src/Tests/BlockContentListTest.php +++ b/core/modules/block_content/src/Tests/BlockContentListTest.php @@ -15,7 +15,7 @@ * @group block_content * @see \Drupal\block\BlockContentListBuilder */ -class BlockContentListTest extends WebTestBase { +class BlockContentListTest extends BlockContentTestBase { /** * Modules to enable. diff --git a/core/modules/block_content/src/Tests/BlockContentTestBase.php b/core/modules/block_content/src/Tests/BlockContentTestBase.php index 5bb7122..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; /** @@ -47,6 +48,32 @@ */ 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(); + + // 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/BlockContentTranslationUITest.php b/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php index aec6f69..74fc537 100644 --- a/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php +++ b/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php @@ -41,6 +41,19 @@ protected function setUp() { } /** + * {@inheritdoc} + */ + protected function setupBundle() { + // Create the basic bundle since it is provided by standard. + $bundle = entity_create('block_content_type', array( + 'id' => $this->bundle, + 'label' => $this->bundle, + 'revision' => FALSE + )); + $bundle->save(); + } + + /** * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getTranslatorPermission(). */ public function getTranslatorPermissions() { 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'); diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php index 3d7c0c3..6482725 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationListUiTest.php @@ -8,6 +8,7 @@ namespace Drupal\config_translation\Tests; use Drupal\Component\Utility\Unicode; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\simpletest\WebTestBase; @@ -389,6 +390,23 @@ public function doFieldListTest() { )); $content_type->save(); + // Create a block content type. + $block_content_type = entity_create('block_content_type', array( + 'id' => 'basic', + 'label' => 'Basic', + '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(); + // Look at a few fields on a few entity types. $pages = array( array( diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_body_field.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_body_field.yml new file mode 100644 index 0000000..5f604d6 --- /dev/null +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_body_field.yml @@ -0,0 +1,25 @@ +id: d6_block_content_body_field +label: Drupal 6 block content body field configuration +migration_groups: + - Drupal 6 +source: + # We do an empty source and a proper destination to have an idmap for + # migration_dependencies. + plugin: empty + constants: + entity_type: block_content + bundle: basic + field_name: body + label: Body + display_summary: false +process: + entity_type: 'constants/entity_type' + bundle: 'constants/bundle' + field_name: 'constants/field_name' + label: 'constants/label' + 'settings/display_summary': 'constants/display_summary' +destination: + plugin: entity:field_config +migration_dependencies: + required: + - d6_block_content_type 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 new file mode 100644 index 0000000..6a439b4 --- /dev/null +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block_content_type.yml @@ -0,0 +1,20 @@ +id: d6_block_content_type +label: Drupal 6 block content type +migration_groups: + - Drupal 6 +source: + # We do an empty source and a proper destination to have an idmap for + # migration_dependencies. + plugin: empty + constants: + label: Basic + id: basic + revision: false + description: '' +process: + id: 'constants/id' + label: 'constants/label' + revision: 'constants/revision' + description: 'constants/description' +destination: + plugin: entity:block_content_type \ No newline at end of file diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_custom_block.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_custom_block.yml index 3f68aaa..cfb9748 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_custom_block.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_custom_block.yml @@ -20,3 +20,4 @@ destination: migration_dependencies: required: - d6_filter_format + - d6_block_content_body_field diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php index 9894bdd..8373030 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Language\Language; use Drupal\block_content\Entity\BlockContent; use Drupal\Core\Language\LanguageInterface; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\migrate\MigrateExecutable; use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase; @@ -27,6 +28,13 @@ class MigrateBlockContentTest extends MigrateDrupalTestBase { */ protected function setUp() { parent::setUp(); + $migration = entity_load('migration', 'd6_block_content_type'); + $executable = new MigrateExecutable($migration, $this); + $executable->import(); + $migration = entity_load('migration', 'd6_block_content_body_field'); + $executable = new MigrateExecutable($migration, $this); + $executable->import(); + $this->prepareMigrations(array( 'd6_filter_format' => array( array(array(2), array('full_html')) diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php index 4dd02c4..11e3b29 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php @@ -61,6 +61,8 @@ class MigrateDrupal6Test extends MigrateFullDrupalTestBase { 'd6_aggregator_feed', 'd6_aggregator_item', 'd6_block', + 'd6_block_content_body_field', + 'd6_block_content_type', 'd6_book_settings', 'd6_cck_field_values:*', 'd6_cck_field_revision:*', diff --git a/core/modules/block_content/config/install/block_content.type.basic.yml b/core/profiles/standard/config/install/block_content.type.basic.yml similarity index 100% rename from core/modules/block_content/config/install/block_content.type.basic.yml rename to core/profiles/standard/config/install/block_content.type.basic.yml