You are here

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. // Hook form alter just for reference, I really need this a lot.
  5. /**
  6.  * Implements hook_form_alter().
  7.  */
  8. function HOOK_form_alter(&$form, &$form_state, $form_id) {
  9.  
  10. }
  11.  
  12. // Use instead of hook_init: browse-tutorials.com/snippet/use-hookpagebuild-instead-hookinit-add-css-or-js-every-page
  13. /**
  14.  * Implements hook_page_build().
  15.  */
  16. function hook_page_build(&$page) {
  17.  
  18. }
  19.  
  20. // More views hooks: api.drupal.org/api/views/views.api.php/group/views_hooks/7
  21. /**
  22.  * Implements hook_views_query_alter().
  23.  */
  24. function HOOK_views_query_alter(&$view, &$query) {
  25.  
  26. }
  27.  
  28. // Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.
  29. search_excerpt($keys, $text)
  30.  
  31. // Performs HTTP requests. This is a flexible and powerful HTTP client implementation. Correctly handles GET, POST, PUT or any other HTTP requests.
  32. drupal_http_request($url, array $options = array())
  33.  
  34. // Static cache.
  35. function foo() {
  36. $cache = &drupal_static(__FUNCTION__);
  37. if (!isset($cache)) {
  38. // ...
  39. }
  40. return $cache;
  41. }
  42.  
  43. // "Permanent" cache.
  44. function foo() {
  45. $cache = &drupal_static(__FUNCTION__);
  46. if (!isset($cache)) {
  47. if ($cache = cache_get(__FUNCTION__ . '_data')) {
  48. $cache = $cache->data;
  49. }
  50. else {
  51. // ...
  52. cache_set(__FUNCTION__ . '_data', $cache, 'cache');
  53. }
  54. }
  55. return $cache;
  56. }
Programming Language: PHP