You are here

function relation_rules_load_related in Relation 8

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

Action callback loading all related entities.

File

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

Code

function relation_rules_load_related($source_entity, $relation_type, $entity_type_op = NULL) {
  $endpoints = array();
  $source_entity_type = $source_entity
    ->type();
  $source_entity_id = $source_entity
    ->getIdentifier();
  foreach ($source_entity
    ->getPropertyInfo() as $property_name => $property) {
    if (isset($property['relation_type']) && $property['relation_type'] == $relation_type && isset($property['target_type'])) {
      $related_entities = $source_entity->{$property_name}
        ->value();
      if (!empty($related_entities)) {
        foreach ($related_entities as $related_entity) {
          if (empty($entity_type_op) || $entity_type_op == $property['target_type']) {
            $endpoint_wrapper = entity_metadata_wrapper($property['target_type'], $related_entity);
            if ($endpoint_wrapper
              ->type() != $source_entity_type || $endpoint_wrapper
              ->getIdentifier() != $source_entity_id) {
              $endpoints[] = $related_entity;
            }
          }
        }
      }
    }
  }
  return array(
    'endpoints' => $endpoints,
  );
}