Issue #2143069 by plach, fago: Add getProvider() and hasCustomStorage to FieldDefinitionInterface.

8.0.x
catch 2014-03-26 15:51:07 +01:00
parent 64cd12115e
commit c15c6c188a
7 changed files with 198 additions and 7 deletions

View File

@ -336,11 +336,33 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface
$entity_type = $this->getDefinition($entity_type_id);
$class = $entity_type->getClass();
// Retrieve base field definitions and assign them the entity type provider.
$base_field_definitions = $class::baseFieldDefinitions($entity_type);
$provider = $entity_type->getProvider();
foreach ($base_field_definitions as $definition) {
// @todo Remove this check one FieldDefinitionInterface exposes a proper
// provider setter. See https://drupal.org/node/2225961.
if ($definition instanceof FieldDefinition) {
$definition->setProvider($provider);
}
}
// Invoke hook.
$result = $this->moduleHandler->invokeAll('entity_base_field_info', array($entity_type));
$base_field_definitions = NestedArray::mergeDeep($base_field_definitions, $result);
// Retrieve base field definitions from modules.
foreach ($this->moduleHandler->getImplementations('entity_base_field_info') as $module) {
$module_definitions = $this->moduleHandler->invoke($module, 'entity_base_field_info', array($entity_type));
if (!empty($module_definitions)) {
// Ensure the provider key actually matches the name of the provider
// defining the field.
foreach ($module_definitions as $field_name => $definition) {
// @todo Remove this check one FieldDefinitionInterface exposes a
// proper provider setter. See https://drupal.org/node/2225961.
if ($definition instanceof FieldDefinition) {
$definition->setProvider($module);
}
$base_field_definitions[$field_name] = $definition;
}
}
}
// Automatically set the field name for non-configurable fields.
foreach ($base_field_definitions as $field_name => $base_field_definition) {
@ -413,10 +435,31 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface
// Allow the entity class to override the base fields.
$bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
$provider = $entity_type->getProvider();
foreach ($bundle_field_definitions as $definition) {
// @todo Remove this check one FieldDefinitionInterface exposes a proper
// provider setter. See https://drupal.org/node/2225961.
if ($definition instanceof FieldDefinition) {
$definition->setProvider($provider);
}
}
// Invoke 'per bundle' hook.
$result = $this->moduleHandler->invokeAll('entity_bundle_field_info', array($entity_type, $bundle, $base_field_definitions));
$bundle_field_definitions = NestedArray::mergeDeep($bundle_field_definitions, $result);
// Retrieve base field definitions from modules.
foreach ($this->moduleHandler->getImplementations('entity_bundle_field_info') as $module) {
$module_definitions = $this->moduleHandler->invoke($module, 'entity_bundle_field_info', array($entity_type, $bundle, $base_field_definitions));
if (!empty($module_definitions)) {
// Ensure the provider key actually matches the name of the provider
// defining the field.
foreach ($module_definitions as $field_name => $definition) {
// @todo Remove this check one FieldDefinitionInterface exposes a
// proper provider setter. See https://drupal.org/node/2225961.
if ($definition instanceof FieldDefinition) {
$definition->setProvider($module);
}
$bundle_field_definitions[$field_name] = $definition;
}
}
}
// Automatically set the field name for non-configurable fields.
foreach ($bundle_field_definitions as $field_name => $field_definition) {

View File

@ -148,6 +148,26 @@ class FieldDefinition extends ListDataDefinition implements FieldDefinitionInter
return $this;
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return $this->definition['provider'];
}
/**
* Sets the name of the provider of this field.
*
* @param string $provider
* The provider name to set.
*
* @return $this
*/
public function setProvider($provider) {
$this->definition['provider'] = $provider;
return $this;
}
/**
* {@inheritdoc}
*/
@ -467,4 +487,25 @@ class FieldDefinition extends ListDataDefinition implements FieldDefinitionInter
return array('deleted');
}
/**
* {@inheritdoc}
*/
public function hasCustomStorage() {
return !empty($this->definition['custom_storage']);
}
/**
* Sets the storage behavior for this field.
*
* @param bool $custom_storage
* Pass FALSE if the storage controller takes care of storing the field,
* TRUE otherwise.
*
* @return $this
*/
public function setCustomStorage($custom_storage) {
$this->definition['custom_storage'] = $custom_storage;
return $this;
}
}

View File

@ -103,6 +103,14 @@ interface FieldDefinitionInterface extends ListDataDefinitionInterface {
*/
public function getSetting($setting_name);
/**
* Returns the name of the provider of this field.
*
* @return string
* The provider name; e.g., the module name.
*/
public function getProvider();
/**
* Returns whether the field is translatable.
*
@ -336,4 +344,17 @@ interface FieldDefinitionInterface extends ListDataDefinitionInterface {
*/
public function getColumns();
/**
* Returns the storage behavior for this field.
*
* Indicates whether the entity type's storage controller should take care of
* storing the field values or whether it is handled separately; e.g. by the
* module providing the field.
*
* @return bool
* FALSE if the storage controller takes care of storing the field, TRUE
* otherwise.
*/
public function hasCustomStorage();
}

View File

@ -476,6 +476,13 @@ class FieldConfig extends ConfigEntityBase implements FieldConfigInterface {
return $this->schema;
}
/**
* {@inheritdoc}
*/
public function hasCustomStorage() {
return FALSE;
}
/**
* {@inheritdoc}
*/
@ -579,6 +586,13 @@ class FieldConfig extends ConfigEntityBase implements FieldConfigInterface {
return $this;
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return 'field';
}
/**
* {@inheritdoc}
*/

View File

@ -513,6 +513,13 @@ class FieldInstanceConfig extends ConfigEntityBase implements FieldInstanceConfi
}
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return $this->field->getProvider();
}
/**
* {@inheritdoc}
*/
@ -780,4 +787,11 @@ class FieldInstanceConfig extends ConfigEntityBase implements FieldInstanceConfi
return $this->field->getColumns();
}
/**
* {@inheritdoc}
*/
public function hasCustomStorage() {
return $this->field->hasCustomStorage();
}
}

View File

@ -552,6 +552,41 @@ class EntityManagerTest extends UnitTestCase {
$this->entityManager->getBaseFieldDefinitions('test_entity_type');
}
/**
* Tests that getFieldDefinitions() method sets the 'provider' definition key.
*
* @covers ::getFieldDefinitions()
*/
public function testGetFieldDefinitionsProvider() {
$this->setUpEntityWithFieldDefinition(TRUE);
$module = 'entity_manager_test_module';
// @todo Mock FieldDefinitionInterface once it exposes a proper provider
// setter. See https://drupal.org/node/2225961.
$field_definition = $this->getMockBuilder('Drupal\Core\Field\FieldDefinition')
->disableOriginalConstructor()
->getMock();
// We expect two calls as the field definition will be returned from both
// base and bundle entity field info hook implementations.
$field_definition
->expects($this->exactly(2))
->method('setProvider')
->with($this->matches($module));
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->will($this->returnValue(array($module)));
$this->moduleHandler->expects($this->any())
->method('invoke')
->with($this->matches($module))
->will($this->returnValue(array($field_definition)));
$this->entityManager->getFieldDefinitions('test_entity_type', 'test_bundle');
}
/**
* Prepares an entity that defines a field definition.
*
@ -587,11 +622,12 @@ class EntityManagerTest extends UnitTestCase {
->method('bundleFieldDefinitions')
->will($this->returnValue(array()));
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->moduleHandler->expects($this->any())
->method('alter');
if (!$custom_invoke_all) {
$this->moduleHandler->expects($this->any())
->method('invokeAll')
->method('getImplementations')
->will($this->returnValue(array()));
}

View File

@ -206,4 +206,26 @@ class FieldDefinitionTest extends UnitTestCase {
$this->assertFalse($definition->isRequired());
}
/**
* Tests provider.
*/
public function testFieldProvider() {
$definition = FieldDefinition::create($this->fieldType);
$provider = $this->randomName();
$definition->setProvider($provider);
$this->assertEquals($provider, $definition->getProvider());
}
/**
* Tests custom storage.
*/
public function testCustomStorage() {
$definition = FieldDefinition::create($this->fieldType);
$this->assertFalse($definition->hasCustomStorage());
$definition->setCustomStorage(TRUE);
$this->assertTrue($definition->hasCustomStorage());
$definition->setCustomStorage(FALSE);
$this->assertFalse($definition->hasCustomStorage());
}
}