You are here

function relation_rules_load_related in Relation 7

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

Action callback loading all related entities.

1 string reference to 'relation_rules_load_related'
RelationRulesTestCase::testRelationInRulesAdmin in tests/relation.rules.test
Test that relation options are available in rules admin UIs.

File

./relation.rules.inc, line 254
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[] = $endpoint_wrapper;
            }
          }
        }
      }
    }
  }
  return array(
    'endpoints' => $endpoints,
  );
}