js_example.module

Tracking 8.x-1.x branch
  1. drupal
    1. 7 contributions/examples/js_example/js_example.module
    2. 8 contributions/examples/js_example/js_example.module

Examples showing how to use some of the new JavaScript features in Drupal 7.

Functions & methods

NameDescription
js_example_accordionjs_example_accordion implementation.
js_example_js_weightsjs_example_weights implementation.
js_example_menuImplements hook_menu().
js_example_themeImplements hook_theme().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Examples showing how to use some of the new JavaScript features in Drupal 7.
  5. */
  6. /**
  7. * @defgroup js_example Example: JavaScript
  8. * @ingroup examples
  9. * @{
  10. * Examples using Drupal 7's built-in JavaScript.
  11. */
  12. /**
  13. * Implements hook_theme().
  14. */
  15. function js_example_theme() {
  16. return array(
  17. 'my_accordion' => array(
  18. 'template' => 'accordion',
  19. 'variables' => array('title' => NULL),
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_menu().
  25. */
  26. function js_example_menu() {
  27. $items = array();
  28. $items['js_example/weights'] = array(
  29. 'title' => 'JS Example: see weighting in action',
  30. 'page callback' => 'js_example_js_weights',
  31. 'access callback' => TRUE,
  32. );
  33. $items['js_example/accordion'] = array(
  34. 'title' => 'JS Example: jQuery UI accordion',
  35. 'page callback' => 'js_example_accordion',
  36. 'access callback' => TRUE,
  37. );
  38. return $items;
  39. }
  40. /**
  41. * js_example_weights implementation.
  42. */
  43. function js_example_js_weights() {
  44. // Add some css to show which line is output by which script
  45. drupal_add_css(drupal_get_path('module', 'js_example') . '/css/jsweights.css');
  46. //
  47. $weights = array(
  48. 'red' => 100,
  49. 'blue' => 23,
  50. 'green' => 3,
  51. 'brown' => 45,
  52. 'black' => 5,
  53. 'purple' => 60
  54. );
  55. drupal_add_js(array('js_weights' => $weights), array('type' => 'setting'));
  56. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/red.js', array('weight' => $weights['red']));
  57. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/blue.js', array('weight' => $weights['blue']));
  58. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/green.js', array('weight' => $weights['green']));
  59. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/brown.js', array('weight' => $weights['brown']));
  60. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/black.js', array('weight' => $weights['black']));
  61. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/purple.js', array('weight' => $weights['purple']));
  62. $output = '<div id="js-weights"></div>';
  63. return $output;
  64. }
  65. /**
  66. * js_example_accordion implementation.
  67. */
  68. function js_example_accordion() {
  69. $title = t('Click sections to expand or collapse:');
  70. $build['myelement'] = array(
  71. '#theme' => 'my_accordion',
  72. '#title' => $title,
  73. );
  74. $build['myelement']['#attached']['library'][] = array('system', 'ui.accordion');
  75. $build['myelement']['#attached']['js'][] = array('data' => '(function($){$(function() { $("#accordion").accordion(); })})(jQuery);', 'type' => 'inline');
  76. $output = drupal_render($build);
  77. return $output;
  78. }