Drupal 7 – Select node by language using EntityFieldQuery
Drupal 7 – Select node by language using EntityFieldQuery

While working as drupal developer in multi language Drupal 7 site you may find EntityFieldQuery class which allows finding entities based on entity properties and keep on getting confused with the propertyCondition and entityCondition for language translation.

Below example of entity field query helps me to find node main language specific featured blog post and

  global $language;
  $query = new EntityFieldQuery;
  $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', $bundletype)
      ->propertyCondition('status', NODE_PUBLISHED) // in case you need it
      ->propertyCondition('language', $language->language)
      ->fieldCondition('field_featured_blog', 'value', '1', "=")
      ->propertyOrderBy('changed', 'DESC')
      ->range(0,1);
  $query->execute();

Below will find data as per the translated entity based language featured blog.

  global $language;
  $query = new EntityFieldQuery;
  $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', $bundletype)
      ->propertyCondition('status', NODE_PUBLISHED)
      ->entityCondition('language', $language->language)
      ->fieldCondition('field_featured_blog','value', '1', "=")
      ->propertyOrderBy('created', 'DESC');
  $query->execute();

Drupal EntityFieldQuery by language

When you do print_r($query), then you will get following:

EntityFieldQuery Object
(
    [altered] => 1
    [entityConditions] => Array
        (
            [entity_type] => Array
                (
                    [value] => node
                    [operator] => =
                )

            [bundle] => Array
                (
                    [value] =>
                    [operator] => 
                )

            [language] => Array
                (
                    [value] => en
                    [operator] => =
                )

        )

    [fieldConditions] => Array
        (
            [0] => Array
                (
                    [field] => Array
                        (
                            [translatable] => 1
                            [entity_types] => Array
                                (
                                )

                            [settings] => Array
                                (
                                    [allowed_values] => Array
                                        (
                                            [0] => 
                                            [1] => 
                                        )

                                    [allowed_values_function] => 
                                    [entity_translation_sync] => 
                                )

                            [storage] => Array
                                (
                                    [type] => field_sql_storage
                                    [settings] => Array
                                        (
                                        )

                                    [module] => field_sql_storage
                                    [active] => 1
                                    [details] => Array
                                        (
                                            [sql] => Array
                                                (
                                                    [FIELD_LOAD_CURRENT] => Array
                                                        (
                                                            [field_data_field_featured_blog] => Array
                                                                (
                                                                    [value] => field_featured_blog_value
                                                                )

                                                        )

                                                    [FIELD_LOAD_REVISION] => Array
                                                        (
                                                            [field_revision_field_featured_blog] => Array
                                                                (
                                                                    [value] => field_featured_blog_value
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [foreign keys] => Array
                                (
                                )

                            [indexes] => Array
                                (
                                    [value] => Array
                                        (
                                            [0] => value
                                        )

                                )

                            [id] => 260
                            [field_name] => field_featured_blog
                            [type] => list_boolean
                            [module] => list
                            [active] => 1
                            [locked] => 0
                            [cardinality] => 1
                            [deleted] => 0
                            [columns] => Array
                                (
                                    [value] => Array
                                        (
                                            [type] => int
                                            [not null] => 
                                        )

                                )

                            [bundles] => Array
                                (
                                    [node] => Array
                                        (
                                            [0] => 
                                            [1] => 
                                        )

                                )

                        )

                    [column] => value
                    [value] => 1
                    [operator] => =
                    [delta_group] => 
                    [language_group] => 
                )

        )

    [fieldMetaConditions] => Array
        (
        )

    [propertyConditions] => Array
        (
            [0] => Array
                (
                    [column] =>
                    [value] => 1
                    [operator] => 
                )

        )

    [order] => Array
        (
            [0] => Array
                (
                    [type] => property
                    [specifier] =>
                    [direction] => DESC
                )

        )

    [range] => Array
        (
        )

    [pager] => Array
        (
        )

    [deleted] => 
    [fields] => Array
        (
            [0] => Array
                (
                    [translatable] => 1
                    [entity_types] => Array
                        (
                        )

                    [settings] => Array
                        (
                            [allowed_values] => Array
                                (
                                    [0] => 
                                    [1] => 
                                )

                            [allowed_values_function] => 
                            [entity_translation_sync] => 
                        )

                    [storage] => Array
                        (
                            [type] => field_sql_storage
                            [settings] => Array
                                (
                                )

                            [module] => field_sql_storage
                            [active] => 1
                            [details] => Array
                                (
                                    [sql] => Array
                                        (
                                            [FIELD_LOAD_CURRENT] => Array
                                                (
                                                    [field_data_field_featured_blog] => Array
                                                        (
                                                            [value] => field_featured_blog_value
                                                        )

                                                )

                                            [FIELD_LOAD_REVISION] => Array
                                                (
                                                    [field_revision_field_featured_blog] => Array
                                                        (
                                                            [value] => field_featured_blog_value
                                                        )

                                                )

                                        )

                                )

                        )

                    [foreign keys] => Array
                        (
                        )

                    [indexes] => Array
                        (
                            [value] => Array
                                (
                                    [0] => value
                                )

                        )

                    [id] => 260
                    [field_name] => field_featured_blog
                    [type] => list_boolean
                    [module] => list
                    [active] => 1
                    [locked] => 0
                    [cardinality] => 1
                    [deleted] => 0
                    [columns] => Array
                        (
                            [value] => Array
                                (
                                    [type] => int
                                    [not null] => 
                                )

                        )

                    [bundles] => Array
                        (
                            [node] => Array
                                (
                                    [0] =>
                                    [1] =>
                                )

                        )

                )

        )

    [count] => 
    [age] => FIELD_LOAD_CURRENT
    [tags] => Array
        (
        )

    [metaData] => Array
        (
        )

    [orderedResults] => Array
        (
        )

    [executeCallback] => 
)

Full details of EntityFieldQuery at:
https://api.drupal.org/api/drupal/includes%21entity.inc/class/EntityFieldQuery/7.x

The function defaults to either = or IN depending on the value param as an array or string.
https://api.drupal.org/api/drupal/includes%21entity.inc/function/EntityFieldQuery%3A%3ApropertyCondition/7.x

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here