You are here

public function EntityReferenceIntegrityEntityHandler::hasDependents in Entity Reference Integrity 8

Check if an entity has dependent entties.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: An entity.

Return value

bool If the entity is referenced from elsewhere.

Overrides EntityReferenceDependencyManagerInterface::hasDependents

File

src/EntityReferenceIntegrityEntityHandler.php, line 52

Class

EntityReferenceIntegrityEntityHandler
Entity reference integrity entity handler.

Namespace

Drupal\entity_reference_integrity

Code

public function hasDependents(EntityInterface $entity) {
  $referencing_fields = $this->fieldMap
    ->getReferencingFields($entity
    ->getEntityTypeId());
  foreach ($referencing_fields as $source_entity_type_id => $source_fields) {

    // Select a count of all entities of type $source_entity_type_id which
    // have any one of their entity reference fields pointing to the given
    // entity.
    $number_of_referencing_entities = $this
      ->referentialEntityQuery($source_entity_type_id, $source_fields, $entity
      ->id())
      ->count()
      ->execute();

    // Stop immediately if we find any foreign entity which references our own
    // entity.
    if ($number_of_referencing_entities > 0) {
      return TRUE;
    }
  }
  return FALSE;
}