Last active
March 16, 2018 13:15
-
-
Save jpmc/51386198342f26baa73a4059e3ed9ba4 to your computer and use it in GitHub Desktop.
A uniform method for getting Entity Reference(s) from Entity fields of varying cardinalities.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity | |
* Entity that has fields. | |
* @param String $field_name | |
* Field name for Entity Reference field. | |
* | |
* @return \Drupal\Core\Entity\EntityInterface[] | |
* Return an array of one or more referenced entities. | |
* | |
* @link https://drupal.stackexchange.com/a/186317/61513 | |
* For singular entity references, Drupal's `EntityReferenceItem` class lacks | |
* a means of directly getting the Entity object that is referenced in the | |
* field. | |
* Note: Multivalued Reference fields use `EntityReferenceFieldItemList`, which | |
* implements a `referencedEntities()` function which returns an array of | |
* referenced entities just fine. | |
* @since Drupal 8.0.x | |
*/ | |
function _get_entity_ref(\Drupal\Core\Entity\FieldableEntityInterface $entity, String $field_name) { | |
if ($entity->hasField($field_name)) { | |
// Get entity field type & value if the entity has the field. | |
$field_type = $entity->getFieldDefinition($field_name)->getType(); | |
$field = $entity->get($field_name); | |
if ($field) { | |
// If it is a singular entity ref, return it's ref'd Entity in an array. | |
if ($field_type === 'entity_reference') { | |
/** @var Drupal\Core\Field\EntityReferenceFieldItemList $field */ | |
// Get first/only reference item from field. | |
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $ref_item */ | |
$ref_item = $field->first(); | |
// Get entity reference from the reference item. | |
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $entity_ref */ | |
$entity_ref = $ref_item->get('entity'); | |
// Get the Entity wrapper. | |
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $entity_adaptor */ | |
$entity_adaptor = $entity_ref->getTarget(); | |
// Return the Entity value from the wrapper. | |
return [$entity_adaptor->getValue()]; | |
} | |
// If it is a multi-valued Entity ref, get all ref'd Entities in an array. | |
elseif ($field_type === 'entity_reference_revisions') { | |
/** @var Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $field */ | |
return $field->referencedEntities(); | |
} | |
// TODO: Throw error or logging here for "else" cases where it is not an Entity Reference field. | |
} | |
} | |
// Returns an empty array to ensure a consistent return type. | |
// TODO: Consider throwing or logging an exception if the field was missing. | |
return []; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment