diff -u b/core/modules/field/src/Entity/FieldInstanceConfig.php b/core/modules/field/src/Entity/FieldInstanceConfig.php --- b/core/modules/field/src/Entity/FieldInstanceConfig.php +++ b/core/modules/field/src/Entity/FieldInstanceConfig.php @@ -58,10 +58,10 @@ * The field type. * * This property is denormalized from the field storage for optimization of - * the "entity and render cache hits" critical paths. It is lazy loaded from - * the FieldStorageConfig if not specified at __construct() time (see - * getType()), and saved in config records so that it is present on the next - * load (see toArray()). + * the "entity and render cache hits" critical paths. If not present in the + * $values passed to entity_create(), it is populated from the + * FieldStorageConfig in postCreate(), and saved in config records so that it + * is present on the next loads. * * @var string */ @@ -251,7 +251,9 @@ $field_storage = $values['field_storage']; $values['field_name'] = $field_storage->getName(); $values['entity_type'] = $field_storage->getTargetEntityTypeId(); - $this->fieldStorage = $field_storage; + // The internal property is fieldStorage, not field_storage. + unset($values['field_storage']); + $values['fieldStorage'] = $field_storage; } else { if (empty($values['field_name'])) { @@ -287,10 +289,6 @@ * {@inheritdoc} */ public function getType() { - // If not already set, read the field type from the field storage. - if (empty($this->field_type)) { - $this->field_type = $this->getFieldStorageDefinition()->getType(); - } return $this->field_type; } @@ -301,9 +299,12 @@ // Validate that we have a valid storage for this instance. This throws an // exception if the storage is invalid. $this->getFieldStorageDefinition(); - // Also, make sure the denormalized field_type property is populated and - // saved in the config records. - $this->getType(); + // If it was not present in the $values passed to entity_create(), (e.g. for + // programmatic creation), populate the denormalized field_type property + // from the field storage, so that it gets saved in the config record. + if (empty($this->field_type)) { + $this->field_type = $this->getFieldStorageDefinition()->getType(); + } // 'Label' defaults to the field name (mostly useful for field instances // created in tests). only in patch2: unchanged: --- a/core/modules/field/tests/src/FieldInstanceConfigEntityUnitTest.php +++ b/core/modules/field/tests/src/FieldInstanceConfigEntityUnitTest.php @@ -125,9 +125,13 @@ public function testCalculateDependencies() { ->method('getConfigDependencyName') ->will($this->returnValue('field.storage.test_entity_type.test_field')); - $values = array('field_name' => $this->fieldStorage->getName(), 'entity_type' => 'test_entity_type', 'bundle' => 'test_bundle'); - $entity = new FieldInstanceConfig($values, $this->entityTypeId); - $dependencies = $entity->calculateDependencies(); + $instance = new FieldInstanceConfig(array( + 'field_name' => $this->fieldStorage->getName(), + 'entity_type' => 'test_entity_type', + 'bundle' => 'test_bundle', + 'field_type' => 'test_field', + ), $this->entityTypeId); + $dependencies = $instance->calculateDependencies(); $this->assertContains('field.storage.test_entity_type.test_field', $dependencies['entity']); $this->assertContains('test.test_entity_type.id', $dependencies['entity']); } @@ -136,8 +140,12 @@ public function testCalculateDependencies() { * @covers ::toArray() */ public function testToArray() { - $values = array('field_name' => $this->fieldStorage->getName(), 'entity_type' => 'test_entity_type', 'bundle' => 'test_bundle'); - $instance = new FieldInstanceConfig($values, $this->entityTypeId); + $instance = new FieldInstanceConfig(array( + 'field_name' => $this->fieldStorage->getName(), + 'entity_type' => 'test_entity_type', + 'bundle' => 'test_bundle', + 'field_type' => 'test_field', + ), $this->entityTypeId); $expected = array( 'id' => 'test_entity_type.test_bundle.field_test', @@ -171,4 +179,5 @@ public function testToArray() { $export = $instance->toArray(); $this->assertEquals($expected, $export); } + }