Last active
December 11, 2015 20:38
-
-
Save ruizfrontend/4656770 to your computer and use it in GitHub Desktop.
Drupal 7 common functions & hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// _-______defining menu elements_______________________________ | |
// (Including tpl theme) | |
/** | |
* Implements hook_menu(). | |
*/ | |
function MODULE_menu() { | |
$items['URL PATH'] = array( | |
'title' => 'TITLE', | |
'page callback' => 'CALLBACK FUNCTION', | |
'page arguments' => array(1), | |
'access callback' => 'ACCESS CALLBACK FUNCTION', | |
'access arguments' => array('PERMISSIONS STRING'), | |
'type' => MENU_CALLBACK, | |
); | |
} | |
function CALLBACK FUNCTION(PAGE ARGUMENTS) { | |
return theme('THEME FUNCTION', array('VARIABLE' => VALOR)); | |
} | |
/** | |
* Implentation of hook_theme(). | |
*/ | |
function MODULE_theme() { | |
return array( | |
'THEME FUNCTION' => array( | |
'template' => 'PATH TO .PHP.TPL FILE WITHOUT EXTENSION', | |
), | |
), | |
} | |
// _-______defining blocks_______________________________ | |
/** | |
* Implentation of hook_block_info(). | |
*/ | |
function MODULE_block_info() { | |
$blocks['BLOCK NAME'] = array( | |
'info' => t('DESCRIPTIVE BLOCK NAME'), | |
'status' => TRUE, | |
'cache' => DRUPAL_NO_CACHE, //change to DRUPAL_CACHE_GLOBAL in prod, DRUPAL_CACHE_PER_ROLE | |
); | |
return $blocks; | |
} | |
/** | |
* Implentation of hook_block_view(). | |
*/ | |
function MODULE_block_view($delta = '') { | |
switch ($delta) { | |
case 'BLOCK NAME': | |
$block['subject'] = t('BLOCKE'); | |
$block['content'] = BLOCKFUNCTION; | |
break; | |
} | |
return $block; | |
} | |
// _-______defining permissions_______________________________ | |
/** | |
* Implentation of hook_permission(). | |
*/ | |
function MODULE_permission() { | |
return array( | |
'PERMISSIONS STRING' => array( | |
'title' => t('PERMISSIONS STRING'), | |
'description' => t('DESCRIPTION.'), | |
), | |
); | |
} | |
// _-______Preprocess nodes_______________________________ | |
/** | |
* Implements hook_preprocess_node(). | |
*/ | |
function MODULE_preprocess_node(&$vars) { | |
if ($vars['node']->type == 'CONTENT TYPE') { | |
//code | |
} | |
} | |
// _-______Alter forms_______________________________ | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function MODULE_form_alter(&$form, &$form_state, $form_id) { | |
//print ($form_id); //use me to get the id of the your you want | |
switch ($form_id){ | |
case 'FORM ID': | |
//code | |
break; | |
} | |
} | |
// _-______Alter views_______________________________ | |
/** | |
* Implements hook_preprocess_views_view(). | |
*/ | |
function MODULE_preprocess_views_view(&$variables) { | |
//print_r($variables['name'].': '. $variables['display_id'].'<br/> '); | |
switch($variables['name']){ | |
case 'VIEW NAME': | |
if($variables['display_id'] == 'VIEW DISPLAY'){ | |
CODE | |
} | |
} | |
} | |
// _-______Include js and css_______________________________ | |
drupal_add_js(drupal_get_path('module', 'mymodule') .'/mymodule.js', array( | |
'type' => 'inline', | |
'external' => false, | |
'scope' => 'footer', | |
'weight' => 5 | |
)); | |
drupal_add_css(drupal_get_path('module', 'mymodule') .'/mymodule.css', array( | |
'type' => 'file', 'inline' or 'external', | |
'basename' => 'name', | |
'group' => CSS_SYSTEM, CSS_DEFAULT, CSS_THEME, | |
'every_page' => FALSE, | |
'weight' => 5, | |
'media' => 'all', 'print', 'screen', | |
'preprocess' => TRUE, | |
'browsers' = '!IE' | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some Drupal snippets I use too often