I'm challenging myself to not use Views and instead rely on EntityFieldQuery() or Dynamic Database Queries.

I have a node Event with a date field and two entity field references to artists that will be preforming at the event and the venue the artist is playing out.

On the venue page, the venue information is displayed and I'm trying to display the list of events grouped by event NID and some of the artist's information like artist name and embedded YouTube video.

I built the query using Views in about 2 minutes to pull all the information. It takes 600ms to load the page without caching. Using EntityFieldQuery without rendering the information it takes 550ms or 300ms over the time it takes to render the page without all the queries.

function helper_load_events_by_venue_id($nid) {
  // this venue reference target id is found on the eventy enitity.
  // We pull and load all the events

  $query = new EntityFieldQuery();

  $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'event')
      ->fieldCondition('field_venue_reference', 'target_id', $nid);

  $result = $query->execute();

  if (isset($result['node'])) {
    $event_items_nids = array_keys($result['node']);
    $event_items = entity_load('node', $event_items_nids);
  $events = array();

  foreach ($event_items as $event_nid => $event) {
    $event_node_wrapper = entity_metadata_wrapper('node', $event);
    $events[$event_nid] = array();
    $events[$event_nid]['date'] = $event_node_wrapper->field_event_date->value();
    foreach ($event_node_wrapper->field_artist_reference->value() as $artist) {
      $events[$event_nid][] = $artist;

  return $events;

What is the best way to create an unordered list of upcoming events to be listed on a venue page without using views. It gets complicated because I want to show the event day, the artists that are playing at that event, the formatted embedded YouTube video of the artist, and taxonomy terms associated with the artist. Am I going to have to create queries to the database with lots of joins passing the information through theme function or can I use abstractions like EntityFieldQuery() and field_view_field()?

  • You can reduce the number of round trips to the database by using my sandbox module. It can load field data while doing an EFQ. drupal.org/sandbox/mikeytown2/2209909 – mikeytown2 Mar 23 '15 at 15:31
  • Loading the $event_items isn't the problem with performance. It's having to iterate through all the artists at each event, and then loading and building each of those nodes. EFQExtra is a bit complicated and I can't figure how to make it work on this case. – Adam S Mar 23 '15 at 18:27
  • How many items are you running through node_build_content? – mikeytown2 Mar 23 '15 at 19:31
  • 14 example nodes are run through node_build_content(). Instead of building the whole node I'm using field_view_field() on field_video and field_genre. Runs in 70% the time. – Adam S Mar 23 '15 at 21:55

Your Answer

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

Browse other questions tagged or ask your own question.