8

I have developed a module in a dev environment. I built a view for it using the UI. I've exported the view, but I don't know where to put it so that an installation of the module in a fresh environment will also create the view.

How do I do this properly?

Bonus: how do I have the module uninstall the view on hook_uninstall?

10

Create the MODULENAME.views_default.inc file (replace MODULENAME with the name of your module), and copy the exported code in the implementation of hook_views_default_views() you write in that file.

For example, the Views module implements node_views_default_views() for the Node module. The relevant parts for the front page view are the following ones.

  $views = array();

  // Omissis

  $view = new view;
  $view->name = 'frontpage';
  $view->description = 'Emulates the default Drupal front page; you may set the default home page path to this view to make it your front page.';
  $view->tag = 'default';
  $view->base_table = 'node';
  $view->human_name = 'Front page';
  $view->core = 0;
  $view->api_version = '3.0';
  $view->disabled = TRUE; /* Edit this to true to make a default view disabled initially */

  // Omissis

  $views['frontpage'] = $view;

The hook then returns the content of $views.

If the view has not been overwritten by a user, it should not be shown in the view list, once the module implementing the hook is uninstalled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.