Issue #2134967 by plopesc, swentel, chx: FieldDefinitionInterface should include a getTargetEntityTypeId().

8.0.x
Nathaniel Catchpole 2014-02-13 12:01:34 +00:00
parent c916406304
commit 4d166e5e33
5 changed files with 56 additions and 1 deletions

View File

@ -309,8 +309,12 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface
$entity_type = $this->getDefinition($entity_type_id); $entity_type = $this->getDefinition($entity_type_id);
$class = $entity_type->getClass(); $class = $entity_type->getClass();
$base_definitions = $class::baseFieldDefinitions($entity_type_id);
foreach ($base_definitions as &$base_definition) {
$base_definition->setTargetEntityTypeId($entity_type_id);
}
$this->entityFieldInfo[$entity_type_id] = array( $this->entityFieldInfo[$entity_type_id] = array(
'definitions' => $class::baseFieldDefinitions($entity_type_id), 'definitions' => $base_definitions,
// Contains definitions of optional (per-bundle) fields. // Contains definitions of optional (per-bundle) fields.
'optional' => array(), 'optional' => array(),
// An array keyed by bundle name containing the optional fields added // An array keyed by bundle name containing the optional fields added

View File

@ -285,6 +285,27 @@ class FieldDefinition extends ListDefinition implements FieldDefinitionInterface
return $this->getSetting('default_value'); return $this->getSetting('default_value');
} }
/**
* {@inheritdoc}
*/
public function getTargetEntityTypeId() {
return isset($this->definition['entity_type']) ? $this->definition['entity_type'] : NULL;
}
/**
* Sets the ID of the type of the entity this field is attached to.
*
* @param string $entity_type_id
* The name of the target entity type to set.
*
* @return static
* The object itself for chaining.
*/
public function setTargetEntityTypeId($entity_type_id) {
$this->definition['entity_type'] = $entity_type_id;
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -255,6 +255,22 @@ interface FieldDefinitionInterface extends ListDefinitionInterface {
*/ */
public function getDefaultValue(EntityInterface $entity); public function getDefaultValue(EntityInterface $entity);
/**
* Returns the ID of the type of the entity this field is attached to.
*
* This method should not be confused with EntityInterface::entityType()
* (configurable fields are config entities, and thus implement both
* interfaces):
* - FieldDefinitionInterface::getTargetEntityTypeId() answers "as a field,
* which entity type are you attached to?".
* - EntityInterface::getEntityTypeId() answers "as a (config) entity, what
* is your own entity type".
*
* @return string
* The name of the entity type.
*/
public function getTargetEntityTypeId();
/** /**
* Returns the field schema. * Returns the field schema.
* *

View File

@ -641,6 +641,13 @@ class Field extends ConfigEntityBase implements FieldInterface {
return array('type' => 'hidden'); return array('type' => 'hidden');
} }
/**
* {@inheritdoc}
*/
public function getTargetEntityTypeId() {
return $this->entity_type;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -598,6 +598,13 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
return array('type' => 'hidden'); return array('type' => 'hidden');
} }
/**
* {@inheritdoc}
*/
public function getTargetEntityTypeId() {
return $this->entity_type;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */