function views_get_view

You are here

7 views.module views_get_view($name, $reset = FALSE)
5 views.module views_get_view($view_name)
6 views.module views_get_view($name, $reset = FALSE)

Get a view from the database or from default views.

This function is just a static wrapper around views::load(). This function isn't called 'views_load()' primarily because it might get a view from the default views which aren't technically loaded from the database.

Parameters

$name: The name of the view.

$reset: If TRUE, reset this entry in the load cache.

Return value

view A reference to the $view object. Use $reset if you're sure you want a fresh one.

18 calls to views_get_view()
ViewsAnalyzeTest::testAnalyzeBasic in tests/views_analyze.test
Tests that analyze works in general.
ViewsExposedFormTest::testRenameResetButton in tests/views_exposed_form.test
Tests, whether and how the reset button can be renamed.
ViewsGlossaryTestCase::testGlossaryView in tests/views_glossary.test
Tests the default glossary view.
ViewsUIWizardJumpMenuTestCase::testJumpMenus in tests/views_ui.test
Tests the jump menu style plugin.
ViewsViewTest::testDelete in tests/views_view.test

... See full list

1 string reference to 'views_get_view'
views_ui_add_form in includes/admin.inc
Form builder for the "add new view" page.

File

./views.module, line 1557
Primarily Drupal hooks and global API functions to manipulate views.

Code

function views_get_view($name, $reset = FALSE) {
  if ($reset) {
    $cache = &drupal_static('ctools_export_load_object');
    if (isset($cache['views_view'][$name])) {
      unset($cache['views_view'][$name]);
    }
  }

  ctools_include('export');
  $view = ctools_export_crud_load('views_view', $name);
  if ($view) {
    $view->update();
    return $view->clone_view();
  }
}

Comments

Since this method is considered to be "private" it comes with a manual. Please make sure to call

<?php
$view
->pre_execute()
?>

before calling

<?php
$view
->execute();
?>

Failing to do so will end up with notices being thrown which in turn will avoid Drupal caching the page. This might also have an effect on external caching services such as Varnish.

For reference, here is how this method can be used to modify results before outputting it to the page:

<?php
  $view
= views_get_view('view_name');
 
$view->set_display('view_block_name');
 
$view->set_arguments(array($tid));
 
// change the amount of items to show
 
$view->set_items_per_page(4);
 
$view->pre_execute();
 
$view->execute();
  print
$view->render();
?>

More useful methods can be found by looking at the API docs: http://api.drupal.org/api/views/includes!view.inc/class/view/7

When the requirement is to just add a view (block) programmatically then please have a look at: http://api.drupal.org/api/views/views.module/function/views_embed_view/7

I am using same code in my texonomy.term.tpl file but getting Error like
Call to a member function set_display() on a non-object

Please Help.

Your $view object has not been created. Try to check the name of the view and try again.