You are here

js_callback_examples.module in JS Callback Handler 7.2

Provides examples on how to use the JS module API.

File

js_callback_examples/js_callback_examples.module
View source
<?php

/**
 * @file
 * Provides examples on how to use the JS module API.
 */

/**
 * Implements hook_menu().
 */
function js_callback_examples_menu() {
  $items['js-callback-examples'] = array(
    'title' => 'JS Callback Examples',
    'description' => 'Provides examples on how to use the JS module API.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'js_callback_examples_form',
    ),
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  $items['js-callback-access-denied'] = array(
    'title' => 'JS Callback Examples (access denied)',
    'access callback' => FALSE,
    'type' => MENU_CALLBACK,
  );
  $items['js-callback-redirect'] = array(
    'title' => 'JS Callback Examples (redirect)',
    'page callback' => 'drupal_goto',
    'page arguments' => array(
      'js-callback-redirect-result',
    ),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['js-callback-redirect-result'] = array(
    'title' => 'JS Callback Examples (redirect result)',
    'description' => 'Provides examples on how to use the JS module API.',
    'page callback' => 'js_callback_examples_redirect_result',
    'page arguments' => array(),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function js_callback_examples_init() {
  global $_js;
  if (!$_js) {
    drupal_set_message(t('You have the JS Example module enabled. Please make sure you disable it when you are done. View <a href="!url">example page</a>.', array(
      '!url' => url('js-callback-examples'),
    )), 'status', FALSE);
  }
}

/**
 * Implements hook_js_info().
 */
function js_callback_examples_js_info() {
  $callbacks['get_uid'] = array();
  return $callbacks;
}

/**
 * Menu callback for "js-callback-examples".
 */
function js_callback_examples_form($form, $form_state) {
  $form['#attached']['library'][] = array(
    'js',
    'js',
  );
  $form['#attached']['js'][drupal_get_path('module', 'js_callback_examples') . '/js_callback_examples.js'] = array();
  $form['intro'] = array(
    '#markup' => t('<p>This example page will show you examples on how each of these callbacks work using the JS module API.</p>'),
  );
  $submit = array(
    '#type' => 'submit',
    '#value' => t('Send'),
    '#js_callback' => array(
      'js_callback_examples' => 'get_uid',
    ),
  );
  $submit_code = $submit;
  $description = t('Use <code>#js_callback</code> on elements (like this submit button) to automatically generate the necessary data attributes. It takes an associative array where the key is the module name and the value is the callback.');
  $description .= '<pre><code>' . htmlentities(var_export($submit_code, TRUE)) . '</code></pre>';
  $description .= '<pre><code>' . htmlentities(render($submit_code)) . '</code></pre>';
  $form['get_uid'] = array(
    '#type' => 'fieldset',
    '#title' => t('Get UID (using #js_callback and $.fn.jsCallback())'),
    '#description' => $description,
    '#attributes' => array(
      'data-js-type' => 'callback',
    ),
  );
  $form['get_uid']['first_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter your name:'),
  );
  $form['get_uid']['actions'] = array(
    '#type' => 'actions',
  );
  $form['get_uid']['actions']['submit'] = $submit;
  $form['get_uid']['results'] = array(
    '#type' => 'fieldset',
    '#title' => t('Results'),
    '#attributes' => array(
      'class' => array(
        'results',
      ),
    ),
    '#value' => '<pre><code></code></pre>',
    // Needs high weight so it appears after actions.
    '#weight' => 1000,
  );
  $items = array();
  $items[] = l(t('Access Denied'), 'js-callback-access-denied');
  $items[] = l(t('Redirect'), 'js-callback-redirect');
  $items[] = l(t('Admin'), 'admin');
  $items[] = l(t('Front'), '<front>');
  $form['using_js_get'] = array(
    '#type' => 'fieldset',
    '#title' => t('Using $.jsGet()'),
    '#description' => t('Click the links below to see the results.'),
    '#attributes' => array(
      'data-js-type' => 'get',
    ),
    'links' => array(
      '#theme' => 'item_list',
      '#items' => $items,
    ),
    'results' => array(
      '#type' => 'fieldset',
      '#title' => t('Results'),
      '#attributes' => array(
        'class' => array(
          'results',
        ),
      ),
      '#value' => '<pre><code></code></pre>',
    ),
  );
  return $form;
}

/**
 * Implements MODULE_js_callback_CALLBACK().
 */
function js_callback_examples_js_callback_get_uid($first_name) {
  $json = array();

  // Ensure that the $first_name variable was provided.
  if (empty($first_name)) {
    drupal_set_message(t('You must enter your first name.'), 'error');
    $json['content'] = '<p>I don\'t know who you are!</p>';
  }
  else {
    $json['content'] = "<p>Hi {$first_name}!</p>";
  }
  return $json;
}

/**
 * Menu callback for "js-callback-redirect-result".
 */
function js_callback_examples_redirect_result() {
  $build = array(
    '#markup' => t('You clicked a link where the destination was <a href="/js-callback-redirect" target="_blank">/js-callback-redirect</a>, but was redirect to <a href="/js-callback-redirect-result" target="_blank">/js-callback-redirect-result</a>.'),
  );
  return $build;
}

Functions

Namesort descending Description
js_callback_examples_form Menu callback for "js-callback-examples".
js_callback_examples_init Implements hook_init().
js_callback_examples_js_callback_get_uid Implements MODULE_js_callback_CALLBACK().
js_callback_examples_js_info Implements hook_js_info().
js_callback_examples_menu Implements hook_menu().
js_callback_examples_redirect_result Menu callback for "js-callback-redirect-result".