Search code examples
drupal-7

Drupal - get the field of view


Hello!

I am trying to get the fields of views
There is a code:

$variables = module_invoke('views', 'block_view', 'news-block');   
print render($variables['content']);

to display unit software.

Whether it is possible to obtain the field at such a conclusion. Rather get views across the fields from variable $variables

Advance all grateful

Sorry for my English.


Solution

  • You can use views_get_view_result function:

    $view = views_get_view_result($name, $display_id); // returns an array conatining the result of the view
    print_r($view);
    

    If you prefer to get whole node objects, you can use node_load function with nids of each row:

    $view = views_get_view_result($name, $display_id); // returns an array conatining the result of the view
    $nodes = array(); // create an empty array to push each node objects
    foreach ($view as $row) {
        $node = node_load($row->nid); // get the node object by nid
        $nodes[] = $node; // push node to $nodes array
    }
    print_r($nodes);
    

    Also see: https://drupal.stackexchange.com/questions/32825/get-a-views-result-using-php-code-and-iterate-the-proper-way