I'm running into some issues with getting Node Gallery Items to work with Entity Field Queries. I'm unable to filter results based on the parent gallery being referenced by the node gallery item. Here's some sample code that explains the issue.

First, I'll write an EFQ to produce an array of node ids belonging to all node galleries owned by a certain Organic Group. This part is fine.

$my_group_id = '150';
$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle', 'node_gallery_gallery')
->fieldCondition('og_group_ref', 'target_id', $my_group_id);
 	
$galleries = $query->execute();
$gallery_ids = array_keys($galleries['node']);  // result: array(20, 50, 65);

Next, I should be able to then use my $gallery_ids to run another Entity Field Query to fetch a list of node ids for node gallery items that belong to the galleries represented in the $gallery_ids array that I just produced. This part doesn't work as expected.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle','node_gallery_item')
->fieldCondition('node_gallery_ref_1', 'target_id', $gallery_ids);

$items = $query->execute();

$items is an empty result set with 0 elements.

Ok, let's make sure that we're able to get any results at all for child nodes of my galleries. I'll change my EFQ to strip out the fieldCondition on 'node_gallery_ref_1'.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle','node_gallery_item');

$items = $query->execute(); // Returns all node gallery items in database

I get the results I expected, but not the filtered results I was originally trying to get.

It seems that node_gallery_ref_1 is not being recognized as a valid field name for a fieldCondition for this type of entity. Is this a bug or am I missing something?

Comments

hyperlinked’s picture

Title: Cannot Filter on Node Gallery Image entity fields using Entity Field Queries » Cannot Filter on Node Gallery Items entity fields using Entity Field Queries
hyperlinked’s picture

Issue summary: View changes
hyperlinked’s picture

Issue summary: View changes
hyperlinked’s picture

Issue summary: View changes
hyperlinked’s picture

Issue summary: View changes
zengenuity’s picture

Status: Active » Closed (won't fix)

The entity reference data is not stored in the normal table location, which is probably why that doesn't work. Instead, it's used to create a Node Gallery Relationship entity. You will probably need to EFQ on that.

<?php
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type','node_gallery_relationship')
    ->propertyCondition('ngid', $gallery_ids);
  $items = $query->execute();
  if (!empty($items['node_gallery_relationship'])) {
    $relationships = entity_load('node_gallery_relationship', array_keys($items['node_gallery_relationship']));
  }
?>

From the relationships you can get the nids.