2 September 2013

List of useful hooks and functions in Drupal 7. You can find here something new or just bookmark it for your reference.

Source code viewer
  1. // Includes another module file, so you can use functions that are in that file.
  2. module_load_include($type, $module, $name = NULL);
  3.  
  4. // Use same window as dpm called krumo, to show arrays in message log.
  5. watchdog('debug', kprint_r($var, TRUE, NULL));
  6.  
  7. // Hook form alter just for reference, I really need this a lot.
  8. /**
  9.  * Implements hook_form_alter().
  10.  */
  11. function HOOK_form_alter(&$form, &$form_state, $form_id) {
  12.  
  13. }
  14.  
  15. // Use instead of hook_init: browse-tutorials.com/snippet/use-hookpagebuild-instead-hookinit-add-css-or-js-every-page
  16. /**
  17.  * Implements hook_page_build().
  18.  */
  19. function hook_page_build(&$page) {
  20.  
  21. }
  22.  
  23. // More views hooks: api.drupal.org/api/views/views.api.php/group/views_hooks/7
  24. /**
  25.  * Implements hook_views_query_alter().
  26.  */
  27. function HOOK_views_query_alter(&$view, &$query) {
  28.  
  29. }
  30.  
  31. // Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.
  32. search_excerpt($keys, $text)
  33.  
  34. // Performs HTTP requests. This is a flexible and powerful HTTP client implementation. Correctly handles GET, POST, PUT or any other HTTP requests.
  35. drupal_http_request($url, array $options = array())
  36.  
  37. // Static cache.
  38. function foo() {
  39. $cache = &drupal_static(__FUNCTION__);
  40. if (!isset($cache)) {
  41. // ...
  42. }
  43. return $cache;
  44. }
  45.  
  46. // "Permanent" cache.
  47. function foo() {
  48. $cache = &drupal_static(__FUNCTION__);
  49. if (!isset($cache)) {
  50. if ($cache = cache_get(__FUNCTION__ . '_data')) {
  51. $cache = $cache->data;
  52. }
  53. else {
  54. // ...
  55. cache_set(__FUNCTION__ . '_data', $cache, 'cache');
  56. }
  57. }
  58. return $cache;
  59. }
Programming Language: PHP