You are here

function relation_rules_get_related_entities in Relation 8

Same name and namespace in other branches
  1. 8.2 relation.rules.inc \relation_rules_get_related_entities()
  2. 7 relation.rules.inc \relation_rules_get_related_entities()

Related entities getter callback.

File

./relation.rules.inc, line 176
Implements the Rules module API for Relation.

Code

function relation_rules_get_related_entities($entity, array $options, $name, $type, $info) {
  $source_entity = entity_metadata_wrapper($type, $entity);
  $source_entity_type = $source_entity
    ->type();
  $source_entity_id = $source_entity
    ->getIdentifier();
  $results = relation_query($source_entity_type, $source_entity_id)
    ->entityCondition('bundle', $info['relation_type'])
    ->range(0, 50)
    ->execute();
  $relation_ids = array_keys($results);
  $entities_ids = array();
  if (!$relation_ids) {
    return $entities_ids;
  }
  foreach (Relation::loadMultiple($relation_ids) as $relation) {
    foreach ($relation->endpoints[Language::LANGCODE_NOT_SPECIFIED] as $endpoint) {
      if ($endpoint['entity_type'] == $info['target_type']) {
        $entities_ids[] = $endpoint['entity_id'];
      }
    }
  }
  return $entities_ids;
}