0

i have code in my template:

$query = new EntityFieldQuery();
    $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'article')
    ->propertyCondition('status', 1)
    ->fieldCondition('field_show_on', 'tid', $group['identifier'], '=') // taxonomy identifier
    ->fieldCondition('field_perks_categories', 'tid', $categories['tid'], '=')
    ->fieldCondition('field_perks_sub_categories', 'tid', $subcategory['tid'], '=')
    ->execute();

i have other table named "artc_order" which contents nid, weight to to order article manually i want to implement this: http://eureka.ykyuen.info/2012/05/16/drupal-7-order-entityfieldquery-by-random-using-hook_query_tag_alter/ to my template file, how to do that?

kelaskakap
  • 113
  • 2
  • 11
  • another link about my issue. however it was implemented on module. can i implement it on template? http://drupal.stackexchange.com/questions/45785/entityfieldquery-inner-join/54277#54277 – kelaskakap Dec 28 '12 at 03:46

1 Answers1

0

Maybe it's usefull to use db_select for your case instead of EntityFieldQuery(). You cannot add JOIN directly to EntityFieldQuery because it's not supported, maybe in Drupal 8.

Your query can be rewritted with db_select like this :

$query = db_select('node', 'n')
        ->fields('n', array('nid'))
        ->leftJoin('field_data_field_show_on', 'fso', 'fso.entity_id = n.nid') // You can use INNER if you need
        ->leftJoin('field_data_field_perks_categories', 'fpc', 'fpc.entity_id = n.nid') // Same thing here
        ->leftJoin('field_data_field_perks_sub_categories', 'fpsc', 'fpsc.entity_id = n.nid') // And here also
        ->leftJoin('order', 'o', 'o.nid = n.nid') // Join artc_order
        ->condition('n.type', 'article', 'LIKE')
        ->condition('fso.field_show_on_tid', $group['identifier'])
        ->condition('fso.field_perks_categories_tid', $categories['tid'])
        ->condition('fso.field_perks_sub_categories_tid', $subcategory['tid'])
        ->condition('n.status', 1)
        ->orderBy('o.weight')
        ->execute();

PS: i can't test this query, because i don't have your database, but it's almost that.

PS2: Using queries into a template.php file is not a clean method, i know that in some extreme cases, we need to do this, but i think that Drupal has lot of hooks that allow us to what we need in our custom modules instead of template.php directly (it's only my opinion, i can be wrong)

Nabil SADKI
  • 134
  • 3