You are here

itoggle.module in iToggle 7.3

Same filename and directory in other branches
  1. 7 itoggle.module
  2. 7.2 itoggle.module

iToggle module.

File

itoggle.module
View source
<?php

/**
 * @file
 * iToggle module.
 */

/**
 * Constant for iToggle library version.
 */
define('ITOGGLE_VERSION', '7.x-3.x-dev');

/**
 * Implements hook_library().
 */
function itoggle_library() {
  return array(
    'itoggle' => array(
      'title' => 'iToggle',
      'website' => 'http://drupal.org/project/itoggle',
      'version' => ITOGGLE_VERSION,
      'js' => array(
        drupal_get_path('module', 'itoggle') . '/misc/itoggle.js' => array(),
      ),
      'css' => array(
        drupal_get_path('module', 'itoggle') . '/misc/itoggle.css' => array(),
      ),
    ),
  );
}

/**
 * Implements hook_element_info().
 */
function itoggle_element_info() {
  return array(
    'itoggle' => array(
      '#input' => TRUE,
      '#base_type' => 'checkbox',
      // @TODO do we need this?
      '#process' => array(
        'itoggle_element_process',
        'ajax_process_form',
      ),
      '#theme' => 'itoggle',
      '#theme_wrappers' => array(
        'itoggle_wrapper',
      ),
      '#pre_render' => array(
        'form_pre_render_conditional_form_element',
      ),
      '#title_display' => 'before',
      // These are extra options specific to iToggle elements.
      '#itoggle_type' => 'yesno',
    ),
  );
}

/**
 * Process callback.
 *
 * @see itoggle_element_process()
 */
function itoggle_element_process($element, $form_state) {
  $element['#attached']['js'][] = drupal_get_path('module', 'itoggle') . '/misc/itoggle.js';
  $element['#attached']['js'][] = drupal_get_path('module', 'itoggle') . '/misc/itoggle-element.js';
  $element['#attached']['css'][] = drupal_get_path('module', 'itoggle') . '/misc/itoggle.css';
  if (isset($element['#options'])) {
    return form_process_checkboxes($element);
  }
  else {
    $element['#return_value'] = 1;
    return form_process_checkbox($element, $form_state);
  }
}

/**
 * Implements hook_theme().
 */
function itoggle_theme() {
  return array(
    'itoggle' => array(
      'render element' => 'element',
    ),
    'itoggle_wrapper' => array(
      'render element' => 'element',
    ),
  );
}
function theme_itoggle($variables) {

  //  $element = &$variables['element'];
  return theme_checkbox($variables);
}

/**
 * Theme callback.
 *
 * @see itoggle_theme()
 */
function theme_itoggle_wrapper($variables) {
  $element =& $variables['element'];
  $display_types = array(
    'yesno',
    'onoff',
    'onezero',
  );
  if (!in_array($element['#itoggle_type'], $display_types)) {
    $element['#itoggle_type'] = 'yesno';
  }

  // This function is invoked as theme wrapper, but the rendered form element
  // may not necessarily have been processed by form_builder().
  $element += array(
    '#title_display' => 'before',
  );
  $attributes['id'] = $element['#id'];

  // Add element's #type and #name as class to aid with JS/CSS selectors
  $attributes['class'] = array(
    'form-item',
    'itoggle-wrapper',
    "itoggle-display-{$element['#itoggle_type']}",
  );
  if (!empty($element['#type'])) {
    $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  }
  if (!empty($element['#name'])) {
    $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(
      ' ' => '-',
      '_' => '-',
      '[' => '-',
      ']' => '',
    ));
  }

  // Add a class for disabled elements to facilitate cross-browser styling.
  if (!empty($element['#attributes']['disabled'])) {
    $attributes['class'][] = 'form-disabled';
  }
  $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  $output .= $element['#children'];
  if (!empty($element['#description'])) {
    $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  }
  $output .= "</div>\n";
  return $output;
}

Functions

Namesort descending Description
itoggle_element_info Implements hook_element_info().
itoggle_element_process Process callback.
itoggle_library Implements hook_library().
itoggle_theme Implements hook_theme().
theme_itoggle
theme_itoggle_wrapper Theme callback.

Constants

Namesort descending Description
ITOGGLE_VERSION Constant for iToggle library version.