Commit 703ec1b3 authored by webchick's avatar webchick

Issue #2313875 by alexpott, yched: Preserve the 'field_type' within FieldInstanceConfig.

parent 6b5c18da
......@@ -95,10 +95,6 @@ public function __construct(array $values, $entity_type = 'base_field_override')
throw new FieldException(String::format('Attempt to create a base field bundle override of field @field_name without a bundle', array('@field_name' => $values['field_name'])));
}
// Discard the 'field_type' entry that is added in config records to ease
// schema generation. See self::toArray().
unset($values['field_type']);
parent::__construct($values, $entity_type);
}
......
......@@ -35,6 +35,19 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn
*/
public $field_name;
/**
* The field type.
*
* This property is denormalized from the field storage for optimization of
* the "entity and render cache hits" critical paths. If not present in the
* $values passed to create(), it is populated from the field storage in
* postCreate(), and saved in config records so that it is present on
* subsequent loads.
*
* @var string
*/
public $field_type;
/**
* The name of the entity type the instance is attached to.
*
......@@ -190,7 +203,7 @@ public function getName() {
* {@inheritdoc}
*/
public function getType() {
return $this->getFieldStorageDefinition()->getType();
return $this->field_type;
}
/**
......@@ -207,20 +220,6 @@ public function targetBundle() {
return $this->bundle;
}
/**
* {@inheritdoc}
*/
public function toArray() {
$properties = parent::toArray();
// Additionally, include the field type, that is needed to be able to
// generate the field-type-dependant parts of the config schema and to
// allow for mapping settings from storage by field type.
// @see \Drupal\field\FieldInstanceConfigStorage::mapFromStorageRecords().
$properties['field_type'] = $this->getType();
return $properties;
}
/**
* {@inheritdoc}
*/
......@@ -236,6 +235,19 @@ public function calculateDependencies() {
return $this->dependencies;
}
/**
* {@inheritdoc}
*/
public function postCreate(EntityStorageInterface $storage) {
parent::postCreate($storage);
// If it was not present in the $values passed to 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();
}
}
/**
* {@inheritdoc}
*/
......
......@@ -87,7 +87,9 @@ public function __construct(array $values, $entity_type = 'field_instance_config
$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'])) {
......@@ -102,11 +104,6 @@ public function __construct(array $values, $entity_type = 'field_instance_config
throw new FieldException(String::format('Attempt to create a field instance @field_name without a bundle.', array('@field_name' => $values['field_name'])));
}
// Discard the 'field_type' entry that is added in config records to ease
// schema generation and mapping settings from storage.
// @see \Drupal\Core\Field\FieldConfigBase::toArray().
unset($values['field_type']);
parent::__construct($values, $entity_type);
}
......@@ -114,6 +111,8 @@ public function __construct(array $values, $entity_type = 'field_instance_config
* {@inheritdoc}
*/
public function postCreate(EntityStorageInterface $storage) {
parent::postCreate($storage);
// Validate that we have a valid storage for this instance. This throws an
// exception if the storage is invalid.
$this->getFieldStorageDefinition();
......
......@@ -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,26 @@ public function testToArray() {
$export = $instance->toArray();
$this->assertEquals($expected, $export);
}
/**
* @covers ::getType
*/
public function testGetType() {
// Ensure that FieldInstanceConfig::getType() is not delegated to
// FieldStorage.
$this->entityManager->expects($this->never())
->method('getFieldStorageDefinitions');
$this->fieldStorage->expects($this->never())
->method('getType');
$instance = new FieldInstanceConfig(array(
'field_name' => $this->fieldStorage->getName(),
'entity_type' => 'test_entity_type',
'bundle' => 'test_bundle',
'field_type' => 'test_field',
), $this->entityTypeId);
$this->assertEquals('test_field', $instance->getType());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment