summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ajax_example/ajax_example.module15
-rw-r--r--ajax_example/ajax_example_misc.inc48
2 files changed, 56 insertions, 7 deletions
diff --git a/ajax_example/ajax_example.module b/ajax_example/ajax_example.module
index 908c7b3..488cfdb 100644
--- a/ajax_example/ajax_example.module
+++ b/ajax_example/ajax_example.module
@@ -158,15 +158,24 @@ function ajax_example_menu() {
'weight' => 8,
);
- // Use the AJAX framework outside the context of a form.
- // See ajax_example_misc.inc.
+ // Use the AJAX framework outside the context of a form using the use-ajax
+ // class. See ajax_example_misc.inc.
$items['examples/ajax_example/ajax_link'] = array(
- 'title' => 'Ajax Link',
+ 'title' => 'Ajax Link ("use-ajax" class)',
'page callback' => 'ajax_example_render_link',
'access callback' => TRUE,
'file' => 'ajax_example_misc.inc',
'weight' => 9,
);
+ // Use the AJAX framework outside the context of a form using a renderable
+ // array of type link with the #ajax property. See ajax_example_misc.inc.
+ $items['examples/ajax_example/ajax_link_renderable'] = array(
+ 'title' => 'Ajax Link (Renderable Array)',
+ 'page callback' => 'ajax_example_render_link_ra',
+ 'access callback' => TRUE,
+ 'file' => 'ajax_example_misc.inc',
+ 'weight' => 9,
+ );
// A menu callback is required when using ajax outside of the Form API.
$items['ajax_link_callback'] = array(
'page callback' => 'ajax_link_response',
diff --git a/ajax_example/ajax_example_misc.inc b/ajax_example/ajax_example_misc.inc
index ad77b36..72f4880 100644
--- a/ajax_example/ajax_example_misc.inc
+++ b/ajax_example/ajax_example_misc.inc
@@ -9,13 +9,13 @@
*/
/**
- * Demonstrates a clickable AJAX-enabled link.
+ * Demonstrates a clickable AJAX-enabled link using the 'use-ajax' class.
*
* Because of the 'use-ajax' class applied here, the link submission is done
* without a page refresh.
*
- * When using the AJAX framework outside the context of a form, you have to
- * include ajax.js explicitly.
+ * When using the AJAX framework outside the context of a form or a renderable
+ * array of type 'link', you have to include ajax.js explicitly.
* @return unknown_type
*/
function ajax_example_render_link() {
@@ -34,11 +34,51 @@ URL whether JS was enabled or not, letting it do different things based on that.
// a page reload. Note the /nojs portion of the path - if javascript is
// enabled, this part will be stripped from the path before it is called.
$link = l(t('Click here'), 'ajax_link_callback/nojs/', array('attributes' => array('class' => array('use-ajax'))));
- $output .= "<div>$link</div><div id='myDiv'></div>";
+ $output .= "<div id='myDiv'></div><div>$link</div>";
return $output;
}
/**
+ * Demonstrates a clickable AJAX-enabled link using a renderable array with the
+ * #ajax property.
+ *
+ * A link that is constructed as a renderable array can have the #ajax property,
+ * which ensures that the link submission is done without a page refresh. The href
+ * of the link is used as the ajax callback, but it degrades gracefully without
+ * JavaScript because if the 'nojs' portion of the href is not stripped out by js,
+ * the callback will return content as required for a full page reload.
+ *
+ * The necessary JavaScript file, ajax.js, will be included on the page
+ * automatically.
+ * @return unknown_type
+ */
+function ajax_example_render_link_ra() {
+ $explanation = "
+The link below has been rendered as an element with the #ajax property, so if
+javascript is enabled, ajax.js will try to submit it via an AJAX call instead
+of a normal page load. The URL also contains the '/nojs/' magic string, which
+is stripped if javascript is enabled, allowing the server code to tell by the
+URL whether JS was enabled or not, letting it do different things based on that.";
+ $build['my_div'] = array(
+ '#markup' => $explanation .'<div id="myDiv"></div>',
+ );
+ $build['ajax_link'] = array(
+ '#type' => 'link',
+ '#title' => t('Click here'),
+ // Note the /nojs portion of the href - if javascript is enabled,
+ // this part will be stripped from the path before it is called.
+ '#href' => 'ajax_link_callback/nojs/',
+ '#id' => 'ajax_link',
+ '#ajax' => array(
+ 'wrapper' => 'myDiv',
+ 'method' => 'html',
+ ),
+ );
+
+ return $build;
+}
+
+/**
* Callback for link example.
*
* Takes different logic paths based on whether Javascript was enabled.