/**
  * Given an entity reference definition, gets the original entity ID.
  *
  * @param array  $definition     The entity reference definition.
  * @param Entity $current_entity The current entity (optional).
  *
  * @return bool|Entity|null
  * @throws InvalidReferenceDefinitionException
  */
 public static function entityFromReferenceDefinition(array $definition, Entity $current_entity = null)
 {
     // Verify the reference definition.
     if (($message = HandlerBase::verifyReferenceDefinition($definition)) !== true) {
         throw new InvalidReferenceDefinitionException($message);
     }
     // Make sure the current entity ID and the reference definition ID are different.
     if ($current_entity && $definition['uuid'] == $current_entity->uuid()) {
         return null;
     }
     // Try to load the entity.
     $entity = Entity::loadByUUID($definition['uuid'], $definition['entity_type']);
     if (!$entity) {
         // Check to see if there is a relationship for the entity.
         if ($current_entity) {
             $count = db_select('publisher_pending_relationships', 'r')->condition('source_type', $current_entity->type())->condition('source_uuid', $current_entity->uuid())->condition('destination_type', $definition['entity_type'])->condition('destination_uuid', $definition['uuid'])->countQuery()->execute()->fetchField();
             if ($count > 0) {
                 return false;
             }
         }
         throw new InvalidReferenceDefinitionException(t('The @type @uuid does not exist.', array('@type' => $definition['entity_type'], '@uuid' => $definition['uuid'])));
     }
     return $entity;
 }