Commit c15c6c18 authored by catch's avatar catch

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

parent 64cd1211
......@@ -336,11 +336,33 @@ protected function buildBaseFieldDefinitions($entity_type_id) {
$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 @@ protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $
// 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) {
......
......@@ -148,6 +148,26 @@ public function setSetting($setting_name, $value) {
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 @@ public static function getReservedColumns() {
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;
}
}
......@@ -103,6 +103,14 @@ public function getSettings();
*/
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 @@ public function getSchema();
*/
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();
}
......@@ -476,6 +476,13 @@ public function getSchema() {
return $this->schema;
}
/**
* {@inheritdoc}
*/
public function hasCustomStorage() {
return FALSE;
}
/**
* {@inheritdoc}
*/
......@@ -579,6 +586,13 @@ public function setTranslatable($translatable) {
return $this;
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return 'field';
}
/**
* {@inheritdoc}
*/
......
......@@ -513,6 +513,13 @@ public function getSetting($setting_name) {
}
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return $this->field->getProvider();
}
/**
* {@inheritdoc}
*/
......@@ -780,4 +787,11 @@ public function getColumns() {
return $this->field->getColumns();
}
/**
* {@inheritdoc}
*/
public function hasCustomStorage() {
return $this->field->hasCustomStorage();
}
}
......@@ -552,6 +552,41 @@ public function testGetBaseFieldDefinitionsInvalidDefinition() {
$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 @@ protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $f
->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()));
}
......
......@@ -206,4 +206,26 @@ public function testFieldRequired() {
$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());
}
}
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