1

I have the following hook method:

hook_preprocess_views_view_unformatted(&$vars)

I can obtain the VIEW like so:

$view = $vars['view'];

When I dump this result I can see a complex structure with all the data I need...the VIEW can be iterated via:

foreach ($view->result as $index => $record) 

Each records data (node details) is navigable via the following path:

$record->_field_data['nid']['entity']->field_XXX

I am uneasy accessing these details with the second property in this chain as usually "underscore" represents private...maybe it is just the OO purist in mean telling me it's bad practice and I am defeating OCP. :)

Ultimately I need the ability to iterate my custom fields and this seems to be the only way I can achieve that -- is there not a API which would encapsulate this access for me?

I assumed the $view object itself would have an API to provide traversing methods or at least result iteration, but it doesn't appear that way.

  • Basically I need some confirmation from someone else that this is the "drupal way" and I will move forward :) thx – Alex.Barylski Jan 31 '14 at 14:28
6

The result array in the Views object contains the row information so yes, if you need to access the results, this is the way to do it.

For example, including:

function YOURMODULE_preprocess_views_view(&$variables) {

  $view=$variables['view'];
  drupal_set_message('<pre>' . print_r($view->result, TRUE) . '</pre>');

}

and then a quick View that just returns node objects would look like this:

enter image description here

and a View that returns fields (in this case just titles) would look like this:

enter image description here

and then when you actually include custom fields, you get that big _field_data array, eg:

enter image description here enter image description here

but you should also see it in a more top level area of the results as well:

enter image description here

Also, a quick look at views_get_view_result() might shed some more "this is how we do it" confirmation/information on Views results. Also What is the hook to preprocess a view and Changing values in preprocess view function talk about other options available to you in the Views API.

| improve this answer | |
  • Thanks for that detailed reply...much appreciated :) In the template views-view-fields.tpl.php there is a variable $fields['field_content']->content -- I assume this is constructed in the default preprocessor -- anyway I was expecting something similar in that big array instead of having to traverse the rather large VIEWS array – Alex.Barylski Jan 31 '14 at 17:26
  • You are talking about the rendered output of the field(s) and yes, that is constructed in a different stage of the View. If you could detail what you are trying to accomplish more precisely, some more detail to the answer can be provided for sure. – Jimajamma Jan 31 '14 at 17:31
  • I added another custom field called "align" and it's showing up under "_field_data" but not as a "field_field_align" property??? Any ideas? – Alex.Barylski Jan 31 '14 at 17:51
  • what kind of field is it and what is it showing up as? – Jimajamma Jan 31 '14 at 17:53
  • I think I figured out why...align is not included in the VIEW query output :) so unless I do that I have to navigate the _field_data property – Alex.Barylski Jan 31 '14 at 17:58

Your Answer

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

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