I am building a module where I would like to use the fields from a user generated view. So let's say the user create a view with 3 fields: Class, Subject, & Nid. I run views_get_view_result() on the view, but that returns entire node objects. How do I get the simple values like this ex: {"1 grade", "English", 54} as an array perhaps?

Remember I don't know beforehand what fields have been selected by the user, so I have to get the raw data like that, or be able to see what fields have been selected and in what order.

Thanks !

Comments

dawehner’s picture

Status: Active » Fixed

Well views_get_view_result returns the raw database result, and yes there are entities involved there. That's "simply"
how d7 has to work.

You might want to try $view->style_plugin->get_field($row_index, 'field') to get the rendered field.

SlashCrew’s picture

Should anyone need it for future reference:

$view = views_get_view('assignment');
$view->init_display();
$view->pre_execute();
$view->execute();

$view_fields = $view->style_plugin->options['columns']; 
    foreach ($view->result as $index => $row) {
        $view->row_index = $index;

        foreach($view_fields as $field){    
            $field_output = $view->style_plugin->get_field_value($index, $field);
            error_log($field.": ".$field_output."\n", 3, "debug.log");
        }
    }

Instead of the error_log build an array or whatever you need. This code only work with single values, if you need to handle arrays then handle it in the second foreach loop. Thanks for the help Daniel!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.