Skip to main contentSkip to footer

Drupal Select Node by EntityFieldQuery by translate language

Rupak Nepali
Share:
Drupal Select Node by EntityFieldQuery by translate language

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

 

 

Comments

Join the conversation and share your thoughts

Leave a Comment

Your comment will be reviewed before publishing.

Be the first to comment on this post!

Innovation

Let's Make Something Amazing Together

We always provide our best creative ideas at the highest level. Tell us about your project and we will make it work.

InnovateBringing innovative solutions to complex problems
AutomateStreamlining processes through automation
DominateLeading the market with exceptional results
Build Smarter and Launch FasterEfficient development for rapid deployment