function {module_name}_views_default_views() {
{...view code...}
$views[$view->name] = $view; // include this line after each view's code
return $views;
}
Tag Archive for views
Views default function
Views API module declaration
function {module_name}_views_api() {
return array('api' => 2.0);
}
hook_form_alter for views exposed filter form
/**
* Implementation of hook_form_FORM_ID_alter() for 'views exposed form'
* Change the first option label '<All>' to 'Filter by Identifier Name' for exposed filters
* The filter identifier needs set up with underscores for multiple word filter names
* e.g. 'document_type' becomes 'Document Type'
*/
function mymodule_form_views_exposed_form_alter (&$form, $form_state) {
foreach($form as $key => &$value) {
if(isset($value['#options']['All'])) {
$label = ucwords(strtolower(str_replace('_', ' ', $key)));
$value['#options']['All'] = t('Filter by !label', array('!label' => $label));
}
}
}
Items Per Page Get Parameter for Views
<?php
/**
* Implements hook_views_pre_execute().
*
* This allows us to adjust the number of rows to present, and to add the pager
* count display functionality.
*
* @param stdClass $view The view to alter.
*/
function YOURMODULEHERE_views_pre_execute(&$view) {
$show = $_GET['show'];
if( $show == 'all' ) $show = 0;
if( is_numeric($show) )
{
$view->pager['items_per_page'] = $show;
$view->handler->options['items_per_page'] = $show;
}
}
?>
Using views_embed_view
views_embed_view('view_name', 'block_1', $node->nid)
Create views programmatically
<?php
$view_args = array();
$display_id = 'page_1';
$view = views_get_view('view_name');
//avoid error if view is not created yet !
if ($view && !empty($view)) {
print $view->execute_display($display_id , $view_args);
}
?>
Using Views 2 and Drupal 6 to Create a Related Pages Block
$node = node_load(arg(1));
if($node){
foreach($node->taxonomy as $term) { $terms[] = $term->tid;}
return implode('+',$terms);
} else {return;}
Inserting a View into a Page in Drupal 6
/* You can simply paste this code into a page or template */
<?php
$view_args = array();
$display_id = 'page_1';
$view = views_get_view('[enter view name here]');
if (!empty($view)) {
print $view->execute_display($display_id , $view_args);
}
?>
Term Type Views – Taxonomy Argument
if ( arg(1) == 'term' && is_numeric(arg(2)) ) {
$tid = arg(2);
return $tid;
}
Show latest user’s tweets in a Drupal block
<?php $view_name = "tweets"; print views_embed_view($view_name, 'Block', $account->uid); ?>