D7: Embed a Views 3's result into a page

Last updated on
16 February 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

- Create a View as usual ( No page or Block require )
- If you want to add a argument, add it using Contextual Filters.
- Create your page (for example in your custom module)

function MYMODULE_menu(){
  $items = array();

  $items['my_page'] = array(
    'title' => 'something',
    'type' => MENU_CALLBACK,
    'page callback' => 'my_page',
    'access arguments' => array('access content')
  );

  return $items;
}

function my_page() {

  $view = views_get_view('YOUR_VIEW_MACHIN_NAME');
  $args[] = 'argument A';
  $args[] = 'argument B';
  $display_id = 'default';
  $content = $view->execute_display($display_id, $args);

  /*
    To find your display_id go to the edit page of your view.
    There will be a list of displays at the left side of the control area.
    With out creating a view or block, "Master" will be at the top of that list.
    Hover your cursor over the name of the display you want to use. and look at the URL.
    The last word will be the display ID.
  */

  $build['content'] = array(
      'dummy' => array('#markup' => $content)
  );
  return $build;
}

Clear Cache at http://YOURSITE/admin/config/development/performance
visit http://YOURSITE/my_page

Tips
====
1) use views_get_view_result to get the result array instead of the html above

  $result_arr = views_get_view_result( 'YOUR_VIEW_MACHIN_NAME', $display_id, $arg1, $arg2);
  foreach($result_arr as $result){  ... }

https://api.drupal.org/api/views/views.module/function/views_get_view_re...

2) if you filter content using exposed filter

$view = views_get_view('get_records');
$view->exposed_input['field_record_ref_id_value'] = '1';
$view->exposed_input['field_record_user_id_value'] = '2';
$view->execute();
$results = $view->result;
...
$view->field['yourfield']->get_value($view->result[0]); //to get the actual data
//more info http://drupal.org/node/1330112

3) This same method can be applied together with AJAX callback to product a dynamic div.
See "Use the AJAX framework outside the context of a form"
at http://drupal.org/project/examples for more information

Help improve this page

Page status: No known problems

You can: