You are here
Drupal Hooks and Functions I Can't Live Without
Hooks
hook_theme D6/7 This hook is awesome for creating custom templates, specially when creating custom modules in which you would like to provide a custom template for its visual presentation. Expand
<?php
/**
* Implementation of hook_theme().
*/
function arts_theme($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'arts');
// Each parent key on this array will define and register a theme.
return array(
'arts_gallery_item' => array(
'variables' => array('imagesrc' => NULL),
'template' => 'arts_gallery_item', // this is the name of the template without the suffix .tpl.php
'path' => $path . '/templates', // I usually create a template folder in custom modules to store the tpls.
),
);
}
?>
This preprocess is optional and is solely intended to modify or add variables in $vars. Any module can override these variables by simply defining their own hook_preprocess_arts_gallery_item.
<?php
/**
* Implementation of hook_preprocess_arts_gallery_item().
*/
function arts_proprocess_arts_gallery_item(&$vars) {
// Let's modify the imagesrc variable, e.g. logo.gif.
$vars['imagesrc'] = 'sites/all/files/images/arts.gif';
// In addition, I can add new variables.
$vars['path'] = drupal_get_path('theme', 'roldanarts');
}
?>
And to invoke the newly created theme, simply use
<?php
print theme('arts_gallery_item', 'logo.gif');
?>
hook_views_query_alter Love this hook for altering Views queries, specially in the case when I need to perform unsupported operations. For instance, here's how I used this hook to be able to add the OR operation for the WHERE Clause since Views 6.x-2.11 automatically performs and AND operation. Expand
Say for instance I wanted to have the following WHERE clause on a View called "featured_arts".
<?php
db_query('SELECT n.title FROM {node} n WHERE n.type = "%s" AND (n.resource_type = "%s" OR n.featured = "%s")', 'featured', 'community', 'frontpage');
?>
Through the Views UI, I wouldn't have been able to add that (OR) operation, but it can easily be achieved thru the hook, like this
<?php
/**
* Implementation of hook_views_query_alter().
*/
function hook_views_query_alter(&$view, &$query) {
switch($view->name) {
case 'featured_arts':
$view->query->add_where('featured_arts_filter', '(node.resource_type = "%s" OR node.featured = "%s")', 'community', 'frontpage');
break;
}
}
?>
Worth noting: The first argument in the add_where function, in this example "featured_arts_filter", refers to whether this additional WHERE clause should be treated as a new AND/OR section.
hook_init When building custom modules, this is the hook for adding JS and CSS files. Expand
<?php
/**
* Implementation of hook_init().
*/
function arts_init() {
$path = drupal_get_path('module', 'arts');
drupal_add_css($path . '/css/arts.css');
drupal_add_js($path . '/js/arts.js');
}
?>
Functions
db_rewrite_sql() When creating and executing custom queries, this function will add access control checks against the SQL query. Very helpful, for filtering content based on permissions granted to different roles.
* This blog gets modify constantly since I use it all the time for my own reference.
Overrides
themehook_links When theming menus this is perfect to override the default markup. For instance, here's the function signature if I wanted to override the System Main Menu. Expand
<?php
/**
* D7
* Override of theme_links().
*/
function roldanarts_links__system_main_menu($variables) {
// Copy the contents of theme_links() found @
// http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links/7
}
?>
Here's how the System Main Menu gets invoked
<?php
print theme('links__system_main_menu', array(
'links' => $main_menu,
'attributes' => array(
'id' => 'main-menu-links',
'class' => array('links', 'clearfix'),
),
'heading' => array(
'text' => t('Main menu'),
'level' => 'h2',
'class' => array('element-invisible'),
),
));
?>
lifestyle:
medium:
tags:
- Diego F. Roldán's blog
- Log in to post comments
- 7683 reads