Skip to content

Instantly share code, notes, and snippets.

@joshuapowell
Created November 25, 2011 23:08
Show Gist options
  • Save joshuapowell/1394630 to your computer and use it in GitHub Desktop.
Save joshuapowell/1394630 to your computer and use it in GitHub Desktop.
Drupal 7 & Views 3 capable dynamically loading views
<?php
/**
* @file
* Views object for the 'My Custom View' view
*
* This file would be saved inside of the previously created
* 'views' directory inside of the MYMODULE module directory.
*/
$view = new view;
$view->name = 'my_custom_view';
$view->description = 'A description of my custom view';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'My Custom View';
$view->core = 7;
$view->api_version = 3;
$view->disabled = TRUE;
...
name = "MY MODULE"
description = "Custom Views specific to a website"
package = "MY WEBSITE"
core = 7.x
dependencies[] = views
<?php
/**
* @file
* Custom views specific to a website
*/
/**
* Implementation of hook_views_api().
*/
function MYMODULE_views_api() {
return array(
'api' => 3
);
}
<?php
/**
* @file
* Load multiple views from a folder
*/
/**
* Implementation of hook_views_default_views().
*
* Create a directory named 'views' inside of your
* module directory (e.g., MYMODULE)
*
* Export each view and save file with an opening php
* delimiter at the beginning of the file, then paste
* in your exported Views object, and finally save your
* file with an .inc extension inside the newly created
* 'views' directory.
*/
function MYMODULE_views_default_views() {
$views = array();
$path = drupal_get_path('module', 'MYMODULE') . '/views';
$files = drupal_system_listing("/\.inc$/", $path, 'name', 0);
foreach($files as $file) {
include $file->uri;
$views[$view->name] = $view;
}
return $views;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment