ajax_example_misc.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * AJAX Miscellaneous Topics.
  5. */
  6. /**
  7. * Demonstrates a clickable AJAX-enabled link using the 'use-ajax' class.
  8. *
  9. * Because of the 'use-ajax' class applied here, the link submission is done
  10. * without a page refresh.
  11. *
  12. * When using the AJAX framework outside the context of a form or a renderable
  13. * array of type 'link', you have to include ajax.js explicitly.
  14. *
  15. * @return array
  16. * Form API array.
  17. *
  18. * @ingroup ajax_example
  19. */
  20. function ajax_example_render_link() {
  21. // drupal_add_library is invoked automatically when a form element has the
  22. // '#ajax' property, but since we are not rendering a form here, we have to
  23. // do it ourselves.
  24. drupal_add_library('system', 'drupal.ajax');
  25. $explanation = t("
  26. The link below has the <i>use-ajax</i> class applied to it, so if
  27. javascript is enabled, ajax.js will try to submit it via an AJAX call instead
  28. of a normal page load. The URL also contains the '/nojs/' magic string, which
  29. is stripped if javascript is enabled, allowing the server code to tell by the
  30. URL whether JS was enabled or not, letting it do different things based on that.");
  31. $output = "<div>" . $explanation . "</div>";
  32. // The use-ajax class is special, so that the link will call without causing
  33. // a page reload. Note the /nojs portion of the path - if javascript is
  34. // enabled, this part will be stripped from the path before it is called.
  35. $link = l(t('Click here'), 'ajax_link_callback/nojs/', array('attributes' => array('class' => array('use-ajax'))));
  36. $output .= "<div id='myDiv'></div><div>$link</div>";
  37. return $output;
  38. }
  39. /**
  40. * AJAX-enabled link in a renderable array.
  41. *
  42. * Demonstrates a clickable AJAX-enabled link using a renderable array with the
  43. * #ajax property.
  44. *
  45. * A link that is constructed as a renderable array can have the #ajax property,
  46. * which ensures that the link submission is done without a page refresh. The
  47. * href of the link is used as the ajax callback, but it degrades gracefully
  48. * without JavaScript because if the 'nojs' portion of the href is not stripped
  49. * out by js, the callback will return content as required for a full page
  50. * reload.
  51. *
  52. * The necessary JavaScript file, ajax.js, will be included on the page
  53. * automatically.
  54. *
  55. * @return array
  56. * Form API array.
  57. */
  58. function ajax_example_render_link_ra() {
  59. $explanation = "
  60. The link below has been rendered as an element with the #ajax property, so if
  61. javascript is enabled, ajax.js will try to submit it via an AJAX call instead
  62. of a normal page load. The URL also contains the '/nojs/' magic string, which
  63. is stripped if javascript is enabled, allowing the server code to tell by the
  64. URL whether JS was enabled or not, letting it do different things based on that.";
  65. $build['my_div'] = array(
  66. '#markup' => $explanation . '<div id="myDiv"></div>',
  67. );
  68. $build['ajax_link'] = array(
  69. '#type' => 'link',
  70. '#title' => t('Click here'),
  71. // Note the /nojs portion of the href - if javascript is enabled,
  72. // this part will be stripped from the path before it is called.
  73. '#href' => 'ajax_link_callback/nojs/',
  74. '#id' => 'ajax_link',
  75. '#ajax' => array(
  76. 'wrapper' => 'myDiv',
  77. 'method' => 'html',
  78. ),
  79. );
  80. return $build;
  81. }
  82. /**
  83. * Callback for link example.
  84. *
  85. * Takes different logic paths based on whether Javascript was enabled.
  86. * If $type == 'ajax', it tells this function that ajax.js has rewritten
  87. * the URL and thus we are doing an AJAX and can return an array of commands.
  88. *
  89. * @param string $type
  90. * Either 'ajax' or 'nojs. Type is simply the normal URL argument to this URL.
  91. *
  92. * @return string|array
  93. * If $type == 'ajax', returns an array of AJAX Commands.
  94. * Otherwise, just returns the content, which will end up being a page.
  95. *
  96. * @ingroup ajax_example
  97. */
  98. function ajax_link_response($type = 'ajax') {
  99. if ($type == 'ajax') {
  100. $output = t("This is some content delivered via AJAX");
  101. $commands = array();
  102. // See ajax_example_advanced.inc for more details on the available commands
  103. // and how to use them.
  104. $commands[] = ajax_command_append('#myDiv', $output);
  105. $page = array('#type' => 'ajax', '#commands' => $commands);
  106. ajax_deliver($page);
  107. }
  108. else {
  109. $output = t("This is some content delivered via a page load.");
  110. return $output;
  111. }
  112. }