Manipulating Views Programmatically

To create a custom presentation management dashboard for Showerma v2 I have learned how to clone, edit and delete views in Drupal 7 programmatically. Now I am going to share the results of my research.

There is plenty of articles about Views hooks on the net, but this one is not about the hooks.

The content is valid for Drupal 7. However, I suppose that the main principles and approaches are still valid and somehow applicable for Backdrop and even Drupal 8 because I don't think they have already changed the Views architecture significantly.

Let us get started from the simplest task.

Clone an existing view

Cloning views is easier than you could imagine.

// Load the view_machine_name view.
$view = views_get_view('view_machine_name');
// ---
// Clone the view.
$view->clone_view();
// ---
// Save the view.
$view->save();

Put this simple code into your module and replace the view_machine_name with the machine name of your source view.

Edit trivial settings of a view display

Any view usually has displays. If a view itself is the sort of a container for some data, then a view display is the data representation. The display can be either a block, a page or whatever else Drupal provides.

For your convenience, I will work with the Page display because it is the most popular one.

Okay, let us change the title and the path of the Page view display.

// Load the view_machine_name view.
$view = views_get_view('view_machine_name');
// Activate ‘page’ display.
$view->set_display('page');
// Initialize view handlers to be able to adjust them later.
$view->init_handlers();
// ---
// Set a new title. I don't use t() because I don't need it in this context.
$view->display_handler->set_option('title', 'New title');
// Change the path of the view.
$view->display_handler->set_option('path', 'your/path');
// ---
// Save the view.
$view->save();

After running this snippet, your view should become available at http//yoursite/your/path

Edit complex settings of a view display

Now let us try to change sorting criteria.

The typical sorting criterion for page display of any node view is the node publication date (Content: Post date). I am going to change the sort order for this criterion to Ascending.

// Load the view_machine_name view.
$view = views_get_view('view_machine_name');
// Activate ‘page’ display.
$view->set_display('page');
// Initialize view handlers to be able to adjust them later.
$view->init_handlers();
// ---
// Load sorting criteria.
$sorts = $view->display_handler->get_option('sorts');
// Change the sort order to ascending.
$sorts['created']['order'] = 'ASC';
// Replace existing sorting criteria with our changed criteria.
$view->display_handler->set_option('sorts', $sorts);
// ---
// Save the view.
$view->save();

Similar way using get_option() and set_option() methods you may change view footer, header, filters and some other settings. I did not have a goal to explain everything. My code examples are supposed to give you ideas how to proceed further.

Just remember to use right tools for debugging. I prefer xdebug and/or devel print functions, such as kpr(), dpm(), etc.

Delete a view

Finally, I would like to show you how to delete views programmatically.

// Load the view_machine_name view.
$view = views_get_view('view_machine_name');
// ---
// Delete the view.
$view->delete();

Please note you can only delete views, which have been created without Views hooks, for example through Views admin panel or using the clone method I mentioned above.

One more thing to add here... Sometimes after changing views, you may want to clear views cache. If that is the case, you may do something like this:

cache_clear_all('view_machine_name' . ':', 'cache_views_data', TRUE);

I hope this article will help to save some time. Check out the Showerma project if you want to see the code snippets above in action.