I've create a view which displays all nodes with content type location. This view renders the node using it's teaser display, which I've created a custom node--location--teaser.tpl.php for.

In this template, I'm using entity_metadata_wrapper as a cleaner way to theme values. My template begins with;

  $nw = entity_metadata_wrapper('node', $node);

  $name     = $nw->label();
  $phone    = $nw->field_phone->value();
  $fax      = $nw->field_fax->value();
  $address  = $nw->field_address_plaintext->value();

All of the stored variables used for themeing return values for the first node rendered by the view, but subsequent nodes only return a value for the $name variable.

If I remove dpm($nw->value()); from line 3, I receive the appropriate values for all view results.

What is happening here? Why does dpm() cause this to happen?


I've moved this logic to a node preprocess function based on feedback from @Clive:

function _preprocess_location(&$vars) {
  $nw = entity_metadata_wrapper('node', $vars['node']);

  $vars['nw']['name']       = $nw->label();
  $vars['nw']['image_url']  = file_create_url(image_style_path( 'location_thumbnail', $nw->field_image->value()['uri']));
  $vars['nw']['phone']      = $nw->field_phone->value();
  $vars['nw']['fax']        = $nw->field_fax->value();
  $vars['nw']['address']    = $nw->field_address_plaintext->value();

If I add a dpm() call anywhere in this function I encounter the same problem as described above.

  • I can't quite put my finger on what's causing this, but having that code in a template file isn't a good idea (templates are evald, something in that might be causing the issue) - try moving the logic out to a preprocess function and see if you get the same results – Clive Aug 26 '15 at 18:25
  • Thanks @Clive — I agree that logic should be separate from presentation. Typically I move this all over once I'm out of my messy 'design' mentality. Unfortunately, it didn't fix the issue (see update). – Aaron Silber Aug 27 '15 at 13:44
  • _preprocess_location() is not a node preprocess function. – kiamlaluno Apr 4 '16 at 20:14
  • Correct. A node preprocess function contains a switch statement which routes location nodes to this function. I like to break things up this way. Sorry for the confusion. – Aaron Silber Apr 6 '16 at 12:15

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.