class EntityReference
Same name in this branch
- 9 core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php \Drupal\field\Plugin\migrate\field\d7\EntityReference
- 9 core/modules/views/src/Plugin/views/row/EntityReference.php \Drupal\views\Plugin\views\row\EntityReference
- 9 core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference
- 9 core/modules/views/src/Plugin/views/style/EntityReference.php \Drupal\views\Plugin\views\style\EntityReference
Same name in other branches
- 8.9.x core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php \Drupal\field\Plugin\migrate\field\d7\EntityReference
- 8.9.x core/modules/views/src/Plugin/views/row/EntityReference.php \Drupal\views\Plugin\views\row\EntityReference
- 8.9.x core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference
- 8.9.x core/modules/views/src/Plugin/views/style/EntityReference.php \Drupal\views\Plugin\views\style\EntityReference
- 8.9.x core/modules/entity_reference/src/Plugin/views/row/EntityReference.php \Drupal\entity_reference\Plugin\views\row\EntityReference
- 8.9.x core/modules/entity_reference/src/Plugin/views/display/EntityReference.php \Drupal\entity_reference\Plugin\views\display\EntityReference
- 8.9.x core/modules/entity_reference/src/Plugin/views/style/EntityReference.php \Drupal\entity_reference\Plugin\views\style\EntityReference
- 8.9.x core/lib/Drupal/Core/Field/Plugin/migrate/field/d7/EntityReference.php \Drupal\Core\Field\Plugin\migrate\field\d7\EntityReference
- 8.9.x core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference
- 10 core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php \Drupal\field\Plugin\migrate\field\d7\EntityReference
- 10 core/modules/views/src/Plugin/views/row/EntityReference.php \Drupal\views\Plugin\views\row\EntityReference
- 10 core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference
- 10 core/modules/views/src/Plugin/views/style/EntityReference.php \Drupal\views\Plugin\views\style\EntityReference
- 10 core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference
- 11.x core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php \Drupal\field\Plugin\migrate\field\d7\EntityReference
- 11.x core/modules/views/src/Plugin/views/row/EntityReference.php \Drupal\views\Plugin\views\row\EntityReference
- 11.x core/modules/views/src/Plugin/views/display/EntityReference.php \Drupal\views\Plugin\views\display\EntityReference
- 11.x core/modules/views/src/Plugin/views/style/EntityReference.php \Drupal\views\Plugin\views\style\EntityReference
- 11.x core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference
- 11.x core/modules/views/src/Plugin/views/filter/EntityReference.php \Drupal\views\Plugin\views\filter\EntityReference
Defines an 'entity_reference' data type.
This serves as 'entity' property of entity reference field items and gets its value set from the parent, i.e. LanguageItem.
The plain value of this reference is the entity object, i.e. an instance of \Drupal\Core\Entity\EntityInterface. For setting the value the entity object or the entity ID may be passed.
Note that the definition of the referenced entity's type is required, whereas defining referenceable entity bundle(s) is optional. A reference defining the type and bundle of the referenced entity can be created as following:
$definition = \Drupal\Core\Entity\EntityDefinition::create($entity_type)->addConstraint('Bundle', $bundle);
\Drupal\Core\TypedData\DataReferenceDefinition::create('entity')->setTargetDefinition($definition);
Plugin annotation
@DataType(
id = "entity_reference",
label = @Translation("Entity reference"),
definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
)
Hierarchy
- class \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\TypedDataInterface, \Drupal\Component\Plugin\PluginInspectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\TypedData\TypedDataTrait
- class \Drupal\Core\TypedData\DataReferenceBase extends \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\DataReferenceInterface
- class \Drupal\Core\Entity\Plugin\DataType\EntityReference extends \Drupal\Core\TypedData\DataReferenceBase
- class \Drupal\Core\TypedData\DataReferenceBase extends \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\DataReferenceInterface
Expanded class hierarchy of EntityReference
1 file declares its use of EntityReference
- ContentEntityBase.php in core/
lib/ Drupal/ Core/ Entity/ ContentEntityBase.php
4 string references to 'EntityReference'
- FieldInstanceSettings::transform in core/
modules/ field/ src/ Plugin/ migrate/ process/ d7/ FieldInstanceSettings.php - Performs the associated process.
- MigrationProvidersExistTest::testFieldProvidersExist in core/
modules/ migrate/ tests/ src/ Kernel/ Plugin/ MigrationProvidersExistTest.php - Tests that modules exist for all field plugins.
- views.view.test_entity_reference.yml in core/
modules/ system/ tests/ modules/ entity_reference_test/ config/ install/ views.view.test_entity_reference.yml - core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml
- views.view.test_entity_reference_entity_test.yml in core/
modules/ system/ tests/ modules/ entity_reference_test/ config/ install/ views.view.test_entity_reference_entity_test.yml - core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference_entity_test.yml
File
-
core/
lib/ Drupal/ Core/ Entity/ Plugin/ DataType/ EntityReference.php, line 34
Namespace
Drupal\Core\Entity\Plugin\DataTypeView source
class EntityReference extends DataReferenceBase {
/**
* The entity ID.
*
* @var int|string
*/
protected $id;
/**
* Gets the definition of the referenced entity.
*
* @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface
* The reference target's definition.
*/
public function getTargetDefinition() {
return $this->definition
->getTargetDefinition();
}
/**
* Checks whether the target entity has not been saved yet.
*
* @return bool
* TRUE if the entity is new, FALSE otherwise.
*/
public function isTargetNew() {
// If only an ID is given, the reference cannot be a new entity.
return !isset($this->id) && isset($this->target) && $this->target
->getValue()
->isNew();
}
/**
* {@inheritdoc}
*/
public function getTarget() {
if (!isset($this->target) && isset($this->id)) {
// If we have a valid reference, return the entity's TypedData adapter.
$entity = \Drupal::entityTypeManager()->getStorage($this->getTargetDefinition()
->getEntityTypeId())
->load($this->id);
$this->target = isset($entity) ? $entity->getTypedData() : NULL;
}
return $this->target;
}
/**
* {@inheritdoc}
*/
public function getTargetIdentifier() {
if (isset($this->id)) {
return $this->id;
}
elseif ($entity = $this->getValue()) {
return $entity->id();
}
}
/**
* {@inheritdoc}
*/
public function setValue($value, $notify = TRUE) {
unset($this->target);
unset($this->id);
// Both the entity ID and the entity object may be passed as value. The
// reference may also be unset by passing NULL as value.
if (!isset($value)) {
$this->target = NULL;
}
elseif ($value instanceof EntityInterface) {
$this->target = $value->getTypedData();
}
elseif (!is_scalar($value) || $this->getTargetDefinition()
->getEntityTypeId() === NULL) {
throw new \InvalidArgumentException('Value is not a valid entity.');
}
else {
$this->id = $value;
}
// Notify the parent of any changes.
if ($notify && isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
/**
* {@inheritdoc}
*/
public function getString() {
if ($entity = $this->getValue()) {
return $entity->label();
}
return '';
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DataReferenceBase::$target | protected | property | The referenced data. | ||
DataReferenceBase::getValue | public | function | Gets the data value. | Overrides TypedData::getValue | |
DependencySerializationTrait::$_entityStorages | protected | property | |||
DependencySerializationTrait::$_serviceIds | protected | property | |||
DependencySerializationTrait::__sleep | public | function | 1 | ||
DependencySerializationTrait::__wakeup | public | function | 2 | ||
EntityReference::$id | protected | property | The entity ID. | ||
EntityReference::getString | public | function | Returns a string representation of the data. | Overrides DataReferenceBase::getString | |
EntityReference::getTarget | public | function | Gets the referenced data. | Overrides DataReferenceBase::getTarget | |
EntityReference::getTargetDefinition | public | function | Gets the definition of the referenced entity. | ||
EntityReference::getTargetIdentifier | public | function | Gets the identifier of the referenced data. | Overrides DataReferenceInterface::getTargetIdentifier | |
EntityReference::isTargetNew | public | function | Checks whether the target entity has not been saved yet. | ||
EntityReference::setValue | public | function | Sets the data value. | Overrides DataReferenceBase::setValue | |
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | ||
TypedData::$definition | protected | property | The data definition. | 1 | |
TypedData::$name | protected | property | The property name. | ||
TypedData::$parent | protected | property | The parent typed data object. | ||
TypedData::applyDefaultValue | public | function | Applies the default value. | Overrides TypedDataInterface::applyDefaultValue | 3 |
TypedData::createInstance | public static | function | Constructs a TypedData object given its definition and context. | Overrides TypedDataInterface::createInstance | |
TypedData::getConstraints | public | function | Gets a list of validation constraints. | Overrides TypedDataInterface::getConstraints | 9 |
TypedData::getDataDefinition | public | function | Gets the data definition. | Overrides TypedDataInterface::getDataDefinition | |
TypedData::getName | public | function | Returns the name of a property or item. | Overrides TypedDataInterface::getName | |
TypedData::getParent | public | function | Returns the parent data structure; i.e. either complex data or a list. | Overrides TypedDataInterface::getParent | |
TypedData::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | Overrides PluginInspectionInterface::getPluginDefinition | |
TypedData::getPluginId | public | function | Gets the plugin_id of the plugin instance. | Overrides PluginInspectionInterface::getPluginId | |
TypedData::getPropertyPath | public | function | Returns the property path of the data. | Overrides TypedDataInterface::getPropertyPath | |
TypedData::getRoot | public | function | Returns the root of the typed data tree. | Overrides TypedDataInterface::getRoot | |
TypedData::setContext | public | function | Sets the context of a property or item via a context aware parent. | Overrides TypedDataInterface::setContext | |
TypedData::validate | public | function | Validates the currently set data value. | Overrides TypedDataInterface::validate | |
TypedData::__construct | public | function | Constructs a TypedData object given its definition and context. | 3 | |
TypedDataTrait::$typedDataManager | protected | property | The typed data manager used for creating the data types. | ||
TypedDataTrait::getTypedDataManager | public | function | Gets the typed data manager. | 2 | |
TypedDataTrait::setTypedDataManager | public | function | Sets the typed data manager. | 2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.