I have 2 different node types. The first one describes a factory and the second one describes a measurement of the water inside the factory. The measurement content type has an Entity Reference field that points to the factory that the measurement is associated with.

What I want to do is print a list of all the factories in a table format and a specific column that will contain a field from the latest measurement that references to the specific factory.

For example:

Factory 1

field 1
field 2
field 3

Factory 2

field 1
field 2
field 3

Measurement 1

Refers to Factory 1
Integer Field
Date Created: 21/5/2013

Measurement 2

Refers to Factory 1
Integer Field
Date Created: 22/5/2013

Measurement 3

Refers to Factory 2
Integer Field
Date Created: 10/2/2012

Measurement 4

Refers to Factory 1
Integer Field
Date Created: 23/5/2013

What I want to print:
Factory 1 || field 1 || field 2 || field 3 || Integer Field (from Measurement 4)
Factory 2 || field 1 || field 2 || field 3 || Integer Field (from Measurement 3)

Notice that I'm only printing the Integer field from Measurement 4 since it is the latest measurement for the specific factory. I started writing some custom code and looked into EntityFieldQuery but I can't understand how to use it in conjunction with Entity Reference.


I've made an assumption about the way you want the table to be formatted, but something like the following should get you most of the way there:

// Set up the table.
$header = array('Factory Name', 'Field Value');
$rows = array();

// Get some nids for the factories you're interested in. Your real query will probably be more specific (checking status etc).
$args = array(':type' => 'factory');
$factory_nids = db_query('SELECT nid FROM {node} WHERE type = :type', $args)->fetchCol();

// Load the factory nodes.
$factory_nodes = node_load_multiple($factory_nids);

foreach ($factory_nodes as $factory_node) {
  // Add the factory name to the first column of this row.
  $row = array($factory_node->title);

  // Get the latest measurement related to this factory with an EntityFieldQuery.
  // fieldCondition() and fieldOrderBy()/propertyOrderBy() are the important bits here.
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'measurement')
    ->propertyCondition('status', 1)
    // This is the entity reference condition; entity ref fields use 'target_id' as the field column name.
    ->fieldCondition('field_factory_reference', 'target_id', $factory_node->nid)
    // If the date you're interested in is in a field, use something like this...
    ->fieldOrderBy('field_measurement_date', 'value', 'DESC')
    // ...or if you're interested in the node created date, this
    ->propertyOrderBy('created', 'DESC')
    ->range(0, 1);

  $results = $query->execute();

  // Make sure there are some results.
  if (!empty($results['node'])) {
    // EntityFieldQuerys always return an array of nids even if there's only one result, 
    // so it's easier to run through node_load_multiple and shift the element later
    $measurement_nodes = node_load_multiple(array_keys($results['node']));

    // Get the node
    $latest_measurement_node = array_shift($measurement_nodes);

    if ($field_items = field_get_items('node', $latest_measurement_node, 'field_some_field')) {
      // If we have an appropriate value, add it to the row.
      $row[] = $field_items[0]['value'];
    else {
      $row[] = 'No data found';
  else {
    $row[] = 'No data found';

  $rows[] = $row;

$rendered_table = theme('table', array('header' => $header, 'rows' => $rows));
|improve this answer|||||

How about using Views Field View module? Specifically:

Create a content view (i.e. latest_measurement_view):

  1. Add the entity reference relationship which points the measurement type to the factory type
  2. Add a contextual filter Content nid with the above relationship
  3. Add a filter for the measurement content type
  4. Add the integer field which belongs to the above type
  5. Sort the view by post date descending
  6. Set "Display a specific number of items" to 1 in the pager section

So this view gives you a single (integer) field that of the most recent measurement node for every corresponding factory node.

Now install and enable the Views Field View module and create another content view (lets say factories_view) to list your factories in a table and:

  1. Add a filter for the factory content type
  2. Add a field of content nid and hide it from display
  3. Add the fields you need from the factory type (title, fields 1, 2, 3 etc)
  4. Add a global view field. In the window that pops up choose the latest_measurement_view and the display you want from the "Views Settings" section and finally (and most importantly) in the "Contextual Filters field" use the [!nid] token (replacement patterns: [!nid] == Content: Nid (raw))

Just be careful to always have the global view field after the content nid field.

You should now have the desired view result showing a table with the factories and a column displaying the integer field of the latest measurement next to each respective factory.

|improve this answer|||||

Your Answer

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