3
global $language;

$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'events')
->propertyCondition('language', $language->language)
->propertyCondition('tnid', $output)
->execute();

I tried using this code and it is showing me empty page, any help on using EntityFieldQuery?

6

$language variable is better to be in the function as:

 function getListNode(){
   global $language;
   $query = new EntityFieldQuery();
   $query->entityCondition('entity_type', 'node', '=')
         ->entityCondition('bundle', 'content_type', '=')
         ->propertyCondition('language', $language->language, '=');
   $nids = $query->execute();
   return entity_load('node', array_keys($nids['node']));
 }

it works for me good luck

|improve this answer|||||
3

Since you're not actually querying any field values, I'm not really sure why you need to use an EntityFieldQuery for this. It would be much easier (and far more efficient) to use a straight query to load the node id(s):

$nids = db_select('node', 'n')
  ->condition('type', 'events')
  ->condition('language', $language->language)
  ->condition('tnid', $output)
  ->execute()
  ->fetchCol();

$matching_nodes = node_load_multiple($nids);
|improve this answer|||||
  • what I am trying to achieve in a multilang site, I tried to set language fallback hack using views before I was using entity translations but it's bugy&it can't work well for revisioning of node+translated node. the case in the views.I'm now using the i18n,it work great for the revisioning node+translations. is there anyway I can hack the views to load eng content if the translations are not available. ex : en : me test 1, me test 2 bur : me test 1, me test 2 burmese as in bur the"me test 1" is not translated yet, it'll pull en content can you please help.I've been frustrated for months:( – Boby Jul 17 '12 at 17:37
  • @Boby is asking for how to search using EntityFieldQuery, using db_select doesn't answer his actual question – Paul Sheldrake Sep 4 '13 at 14:29
  • 4
    @PaulSheldrake An EntityFieldQuery is for querying Entities based on Field values. If you aren't querying over fields, the savvy developer will save the extra overhead and use a db_select(). FYI an EntityFieldQuery uses a SelectQuery (the type of object returned from db_select()) internally anyway. – Clive Sep 4 '13 at 14:35
0

Your code seems correct. Make sure you actually display the results

if (!empty($result['node'])) {
  $nids = array_keys($result['node']);
  $nodes = node_load_multiple($nids);
  $output = node_view_multiple($nodes);
  drupal_render($output);
}
|improve this answer|||||
-3

The propertyCondition method needs a 3 parameter for the matching to work

propertyCondition('language', $language->language, '=')
|improve this answer|||||
  • 1
    Sorry but that's just not correct - the third parameter to propertyCondition() has a default value of NULL anyway, which cascades down to an = in the SelectQuery::condition() method. Adding that third parameter makes zero difference to the query being performed – Clive Sep 4 '13 at 14:36
  • Yeah, the function defaults to either = or IN depending on the value param as an array or string. api.drupal.org/api/drupal/includes%21entity.inc/function/… – doublejosh Feb 4 '16 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.