Drupal 7: How To Create a Views View from Within a Module (default view)

The whole idea in making a module is to make it, well ‘modular’, right? The person who is going to install the module should not have to create and configure a view. Chances are they would not get it just right anyway (you know, with all the exact settings the module depends upon). This is because the Views UI is so versatile that no two people are likely to set it up the exact same way. In many cases, if a module depends on data from a view, all the Views setting need to be just so in order for the module to work its magic.

Although this is very doable, I found it very difficult to locate documentation spelling out just how to do it. I’m not sure if it was not documented in a way that was easy to find or if it was the fact that I had a 3-day headache pounding away. Either way, I did finally come across a few links.

techcommons.stanford.edu

Drupal API

To give myself and others one more place to stumble upon what to do I have documented how to create a Views default view from within a module below. A default Views view is a view that weather enabled or not shows up on the /admin/structure/views page once a module is installed and enabled.

For the purpose of these notes we will call our module, ‘mymodule’. Everywhere you see ‘mymodule’ you need to replace with the name of your actual module name. This article assumes that you are familiar with module development basics and that you have already created your basic module files ( mymodule/mymodule.module, mymodule/mymodule.install & mymodule/mymodule.info ).

OK, let’s create a default view in a module!

Create a new file named mymodule.views_default.inc and save it into a directory named mymodule/includes/.

Inside the file you just created, paste the following function…

/**
 * Implementation of hook_views_default_views().
 */
function mymodule_views_default_views() {
 
  //PASTE EXPORTED VIEW CODE HERE
 
  $views[$view->name] = $view;
  return $views;
}

You can now create a View in the Views UI as you normally would.
Then export the view. Select the entire export text as presented in the textarea of the export result and past it in place of where you see “PASTE EXPORTED VIEW CODE HERE” inside the function you just made and save the file.

OK, next, inside the file your mymodule.module file, paste the following function (remember that everywhere you see ‘mymodule’ you need to replace with the name of your actual module name.).

/**
* Implementation of hook_views_api().
*/
function mymodule_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'mymodule') .'/includes'
  );
}

Now you can clear your cache and see the default View you just created in the list of default views at ‘admin/structure/views’

Please feel free to register and comment.

2 thoughts on “Drupal 7: How To Create a Views View from Within a Module (default view)

Leave a Reply

Your email address will not be published. Required fields are marked *