diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php index cf3ae1dedb..eb0cbe9661 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -218,7 +218,7 @@ protected function processStubRow(Row $row) { if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) { // Use the configured default value for this specific field, if any. if ($default_value = $field_definition->getDefaultValueLiteral()) { - $values[] = $default_value; + $values = $default_value; } else { // Otherwise, ask the field type to generate a sample value. diff --git a/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php b/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php index e2f02ae9d7..4f91d3f5fb 100644 --- a/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php +++ b/core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\migrate\Kernel; +use Drupal\entity_test\Entity\EntityTestMul; use Drupal\KernelTests\KernelTestBase; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\migrate\MigrateExecutable; @@ -44,6 +45,9 @@ class MigrateEntityContentBaseTest extends KernelTestBase { */ protected function setUp() { parent::setUp(); + + // Enable a required field with a default value. + \Drupal::state()->set('entity_test.required_default_field', TRUE); $this->installEntitySchema('entity_test_mul'); ConfigurableLanguage::createFromLangcode('en')->save(); @@ -265,4 +269,26 @@ public function testEmptyDestinations() { $this->assertNull($entity->version->value); } + /** + * Tests stub rows. + */ + public function testStubRows() { + // Create a destination. + $this->createDestination([]); + + // Import a stub row. + $row = new Row([], [], TRUE); + $row->setDestinationProperty('type', 'test'); + $ids = $this->destination->import($row); + $this->assertCount(1, $ids); + + // Make sure the entity was saved. + $entity = EntityTestMul::load(reset($ids)); + $this->assertInstanceOf(EntityTestMul::class, $entity); + // Make sure the default value was applied to the required field. + $field_name = 'required_default_field'; + $default_value = $entity->getFieldDefinition($field_name)->getDefaultValueLiteral(); + $this->assertSame($default_value, $entity->get($field_name)->getValue()); + } + } diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 025cb5fc2f..04279754e1 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -114,6 +114,12 @@ function entity_test_entity_base_field_info(EntityTypeInterface $entity_type) { ->setLabel('Internal field') ->setInternal(TRUE); } + if ($entity_type->id() === 'entity_test_mul' && \Drupal::state()->get('entity_test.required_default_field')) { + $fields['required_default_field'] = BaseFieldDefinition::create('string') + ->setLabel('Required field with default value') + ->setRequired(TRUE) + ->setDefaultValue('this is a default value'); + } if ($entity_type->id() == 'entity_test_mulrev' && \Drupal::state()->get('entity_test.field_test_item')) { $fields['field_test_item'] = BaseFieldDefinition::create('field_test') ->setLabel(t('Field test'))