public function ContentEntityBase::referencedEntities

public ContentEntityBase::referencedEntities()

Gets a list of entities referenced by this entity.

Return value

\Drupal\Core\Entity\EntityInterface[] An array of entities.

Overrides Entity::referencedEntities

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1056

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

public function referencedEntities() {
  $referenced_entities = array();

  // Gather a list of referenced entities.
  foreach ($this->getFields() as $field_items) {
    foreach ($field_items as $field_item) {
      // Loop over all properties of a field item.
      foreach ($field_item->getProperties(TRUE) as $property) {
        if ($property instanceof EntityReference && $entity = $property->getValue()) {
          $referenced_entities[] = $entity;
        }
      }
    }
  }

  return $referenced_entities;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEntityBase.php/function/ContentEntityBase::referencedEntities/8.1.x