Views can be stored in the database, which is typical of smaller sites and hobby sites. However, Views may also be stored directly in the code as "default" views, (which simply means they're available by default). Modules often come with views that are specific to the module data, but it's also possible -- and highly recommended -- that sites which have separate "development" and "production" sites export their views into default views in a site-specific module. This makes it very easy to transfer views from dev to production without making database changes.
<?phpIt should not contain a closing ?> tag, as the closing ?> tag is not required and anything AFTER the closing tag, such as a space or a linefeed, will be displayed directly to the browser and can potentially cause problems. The .module file will contain functions and drupal hooks. Hooks are specially named functions that Drupal will call in order to get your module's response at certain times while generating pages. The only function you will need for this exercise is the 'views_api' hook that tells Views that this module supports the Views API and what version:
function mymodule_views_api() { return array('api' => 2.0); }For other uses you may well add additional functions. Second, you need to create a mymodule.info file:
name = My module description = My site specific module. core = 6.xOnce you have these two files set up, you should be able to activate your new module at the Administer >> Modules page.
$views[$view->name] = $viewTo place this into your hook_views_default_views() you will need to place that after the view, and make sure the function returns $views at the end.
function mymodule_theme($existing) { return array( 'views_view__viewname__displayid' => array ( 'arguments' => array('view' => NULL), 'template' => 'views-view--viewname--displayid', 'base hook' => 'views_view', 'path' => drupal_get_path('module', 'mymodule'), ), ); }There are a small number of gotchas in doing this that you must be aware of.
display: array('view_array' => array(), 'view' => NULL), style: array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL), row: array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL), field: array('view' => NULL, 'field' => NULL, 'row' => NULL),Be sure to use the right arguments line or the theme system will not properly translate.
function mymodule_install() { db_query("UPDATE {system} SET weight = 11 WHERE name = 'mymodule'"); }If you use this method, the base hook should be set to the name of the original template being used. i.e, if this is a variate of views-view-list.tpl.php, this should be 'views_view_list'.
'preprocess functions' => array( 'template_preprocess', 'template_preprocess_views_view', 'mymodule_preprocess_views_view__viewname_displayid', ),The first one is the global 'template_preprocess' function which all templates utilize. It does some basic things such as setting up $zebra and a few other items. See api.drupal.org for specifics. The second one is the plugin specific preprocess. Like 'base hook' it should conform to the name used by the original template. i.e, if the original template was views-view-list.tpl.php then that preprocess function would be named template_preprocess_views_view_list. The third one is your module's preprocess function, if it needs one. In general, you probably will not need one, and you should only attempt to use one if you are reasonably familiar with the concept of preprocess functions and Drupal's theme system in general. See Drupal's theme documentation for more information.