6

Hopefully someone with Drupal 7 experience can point me in the right direction.

I'm creating a custom module to query some nodes and group and sort them by their field values.

I tried to use Views but it seems that it's not possible to get the results in the format I need so I've decide to query the database directly and build my own output.

What's the easiest way to query nodes in Drupal 7 as I notice there is a table for every field now which would seem to make any query that I have to write extremely complex?

Are there any methods to query node data and filter/group the results or can it only be done using raw SQL.

If the latter can you show me an example query that includes Drupal 7 Field values?

2 Answers 2

12

I second Jeremy's suggestion of trying harder with views. However, if you absolutely must write your query directly in code, you'll want to use EntityFieldQuery.

For example, to get a count of all nodes with a text field value of 'foo':

// Get field information
$field = field_info_field('field_name');
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'NODE TYPE')
  ->fieldCondition($field, 'value', 'foo')
  ->count();

$count = $query->execute();

If you don't want a count, but results instead, you would leave off the count part, and fetch results:

foreach ($query->execute() as $entity_type => $entities) {
  foreach ($entities as $entity_id => $entity) {
  ...
  }
}

The Commerce Guys also have a good blog post detailing a simple use case.

2
  • Great answer, I didn't know about EntityFieldQuery. Thanks. May 12, 2011 at 16:53
  • Excellent answer, that will save my a lot of headaches!
    – Camsoft
    May 12, 2011 at 19:32
1

I tried to use Views but it seems that it's not possible to get the results in the format I need so I've decide to query the database directly and build my own output.

My first suggestion would be to try harder with views.

What's the easiest way to query nodes in Drupal 7 as I notice there is a table for every field now which would seem to make any query that I have to write extremely complex?

Yes it would be, which is why I made the first suggestion.

One tip could be to use a view to do the query but to produce your own output by invoking the view programatically.

A second tip would be to use the query which is generated by views as a starting point.

Fields in drupal 7 are defined via the field api. So you would need to look into that to find out how the fields are stored and help you to define your query. It can be done but it is not straightforward.

1
  • 1
    I'm having crazy problems with Views and the Date field in Drupal 7. I think the Date module is broken when using Views 3 and Postgres and views errors with SQL error messages. Also I'm trying to do complex grouping of data and I know Views can only do simple grouping. I don't believe Views is the beginning and the end to all things Drupal. I would love to use Views for this but I'm sure it won't do what I'm trying to do and I'm quite experienced with Views. The question was more for pointers on how to do things like views without using Views module.
    – Camsoft
    May 12, 2011 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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