backdrop-contrib / og

The Organic Groups module provides users the ability to create, manage, and delete 'groups' on a site.
GNU General Public License v2.0
1 stars 8 forks source link

How to perform EntityFieldQuery for entities with more than one OG audience field #69

Closed argiepiano closed 2 years ago

argiepiano commented 2 years ago

This is here to document an alternative way to perform EntityFieldQuery for entities with more than one OG audience field.

Instead of:

    $query = new EntityFieldQuery();
    $result_will_fail = $query
      ->entityCondition('entity_type', 'node')
      ->propertyCondition('type', $node->type)
      ->fieldCondition('og_group1_ref', 'target_id', $group1->nid)
      ->fieldCondition('og_group2_ref', 'target_id', $group2->nid)
      ->execute();

Use:

    $query = new EntityFieldQuery();
    $result1 = $query
      ->entityCondition('entity_type', 'node')
      ->propertyCondition('type', $node->type)
      ->fieldCondition('og_group1_ref', 'target_id', $group1->nid)
      ->execute();

    $query = new EntityFieldQuery();
    $result2 = $query
      ->entityCondition('entity_type', 'node')
      ->propertyCondition('type', $node->type)
      ->fieldCondition('og_group2_ref', 'target_id', $group2->nid)
      ->execute();

    $result_ids = array_intersect(array_keys($result1['node']), array_keys($result2['node']));

Of course, be sure to check that $result1['node'] and $result2['node'] are set before the array_intersect.

laryn commented 2 years ago

@argiepiano Should this get moved into the Wiki for posterity? (I just opened up the Wiki to public edits)

argiepiano commented 2 years ago

Sounds like a good idea!

argiepiano commented 2 years ago

I created a page, but please check - it's not appearing correctly in the wiki index

argiepiano commented 2 years ago

Closing this - check the Wiki for these directions.