You are here

asaf_example.module in Asaf (ajax submit for any form) 8

Same filename and directory in other branches
  1. 7 modules/asaf_example/asaf_example.module

AJAX Examples module file with basic examples.

File

modules/asaf_example/asaf_example.module
View source
<?php

/**
 * @file
 * AJAX Examples module file with basic examples.
 */

/**
 * Implements hook_menu().
 *
 * Sets up calls to drupal_get_form() for all our example cases.
 *
 * @see menu_example.module for more details on hook_menu().
 */
function asaf_example_menu() {
  $items = array();
  $items['examples/asaf_example'] = array(
    'title' => 'ASAF Example',
    'page callback' => 'asaf_example_intro',
    'access callback' => TRUE,
    'expanded' => TRUE,
  );
  $items['examples/asaf_example/gui/simplest'] = array(
    'title' => 'Simplest (GUI)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_gui_simplest',
    ),
    'access callback' => TRUE,
    'file' => 'asaf_example.gui.simplest.inc',
  );
  $items['examples/asaf_example/gui/buttons'] = array(
    'title' => 'Buttons (GUI)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_gui_buttons',
    ),
    'access callback' => TRUE,
    'file' => 'asaf_example.gui.buttons.inc',
  );
  $items['examples/asaf_example/api/simplest'] = array(
    'title' => 'Simplest (API)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_api_simplest',
    ),
    'access callback' => TRUE,
    'file' => 'asaf_example.api.simplest.inc',
  );
  $items['examples/asaf_example/api/needed-files'] = array(
    'title' => 'Needed files (API)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_api_needed_files',
    ),
    'access callback' => TRUE,
    'weight' => 0,
    'file' => 'asaf_example.api.needed_files.inc',
  );
  $items['examples/asaf_example/api/depended-elements'] = array(
    'title' => 'Depended Elements (API)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_api_depended_elements',
    ),
    'access callback' => TRUE,
    'weight' => 0,
    'file' => 'asaf_example.api.depended_elements.inc',
  );
  $items['examples/asaf_example/api/areas'] = array(
    'title' => 'Renderable Areas (API)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_api_areas',
    ),
    'access callback' => TRUE,
    'weight' => 0,
    'file' => 'asaf_example.api.areas.inc',
  );

  /*
   * Some of the examples not so interesting but important for the autotests placed in tests/ folder
   */
  $items['examples/asaf_example/gui/needed-files'] = array(
    'title' => 'Needed files (GUI)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'asaf_example_gui_needed_files',
    ),
    'access callback' => TRUE,
    'weight' => 0,
    'file' => 'tests/asaf_example.gui.needed_files.inc',
  );
  return $items;
}
function asaf_example_theme() {
  return array(
    'asaf_examples' => array(
      'render element' => 'element',
    ),
    'asaf_examples_areas' => array(
      'render element' => 'element',
      'file' => 'asaf_example.api.areas.inc',
    ),
  );
}
function asaf_example_form_alter(&$form, &$form_state, $form_id) {
  if (substr($form_id, 0, 13) == 'asaf_example_') {
    $form['#attached']['css'][] = drupal_get_path('module', 'asaf_example') . '/css/asaf_example.css';
  }
}
function asaf_example_intro() {
  $page = array();
  $page['markup'] = array(
    '#markup' => t('The AJAX example module provides many examples of AJAX including forms, links, and AJAX commands.'),
  );
  $page['examples'] = array(
    '#theme' => 'asaf_examples',
    '#examples' => array(
      array(
        'name' => t('ASAF GUI'),
        'description' => t('Description for gui group'),
        'items' => array(
          l(t('Asaf admin page'), 'admin/config/system/asaf'),
          l(t('Simplest'), 'examples/asaf_example/gui/simplest'),
          l(t('Buttons'), 'examples/asaf_example/gui/buttons'),
        ),
      ),
      array(
        'name' => t('ASAF API'),
        'description' => t('Description for api group'),
        'items' => array(
          l(t('Simplest'), 'examples/asaf_example/api/simplest'),
          l(t('Needed files'), 'examples/asaf_example/api/needed-files'),
          l(t('Depended Elements'), 'examples/asaf_example/api/depended-elements'),
          l(t('Renderable Areas'), 'examples/asaf_example/api/areas'),
        ),
      ),
    ),
  );
  return $page;
}
function theme_asaf_examples($variables) {
  $output = '';
  $element = $variables['element'];
  if (isset($element['#examples'])) {
    foreach ($element['#examples'] as $group) {
      $output .= '<div class="examples-group">';
      if (isset($group['name'])) {
        $output .= '<h2>' . $group['name'] . '</h2>';
      }
      if (isset($group['description'])) {
        $output .= '<div class="description">' . $group['description'] . '</div>';
      }
      $output .= '<ol>';
      foreach ($group['items'] as $example) {
        $output .= '<li>' . $example . '</li>';
      }
      $output .= '</ol>';
      $output .= '</div>';
    }
  }
  return $output;
}