You are here

itoggle_field.module in iToggle 7

Same filename and directory in other branches
  1. 7.2 modules/field/itoggle_field.module

iToggle Field module.

File

modules/field/itoggle_field.module
View source
<?php

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

/**
 * Implements hook_field_widget_info().
 *
 */
function itoggle_field_widget_info() {
  return array(
    'itoggle_field_widget' => array(
      'label' => t('iToggle Widget'),
      'field types' => array(
        'list_boolean',
      ),
      'settings' => array(
        'display_label' => 1,
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_info().
 */
function itoggle_field_field_formatter_info() {
  return array(
    'itoggle_field_formatter' => array(
      'label' => t('iToggle Widget'),
      'description' => t('Displays the field as an iToggle Widget'),
      'field types' => array(
        'list_boolean',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function itoggle_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();

  // @TODO figure out if there is a better place to add the scripts
  itoggle_include_itoggle();
  itoggle_include_settings();
  drupal_add_js(drupal_get_path('module', 'itoggle') . '/js/itoggle.js');
  $info = itoggle_get_entity_info();
  $id = $info[$entity_type]['entity keys']['id'];
  $id = $entity->{$id};
  $checked = isset($items[0]['value']) && $items[0]['value'] == 1;
  if (!count($items)) {
    $items[] = 'itoggle';
  }
  foreach ($items as $delta => $item) {
    $elements[$delta] = array(
      '#markup' => theme('itoggle', array(
        'type' => $entity_type,
        'id' => $id,
        'property' => $field['field_name'],
        'checked' => $checked,
        'scope' => 'field',
      )),
    );
  }
  return $elements;
}

/**
 * Implements hook_field_widget_form().
 */
function itoggle_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $type = $instance['widget']['type'];
  $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  $required = $element['#required'];
  $has_value = isset($items[0]['value']);
  $keys = array_keys($field['settings']['allowed_values']);
  $off_value = array_shift($keys);
  $on_value = array_shift($keys);
  $element += array(
    '#type' => 'checkbox',
    '#default_value' => $has_value ? $has_value : $instance['default_value'][0]['value'],
    '#on_value' => $on_value,
    '#off_value' => $off_value,
    '#title' => $instance['label'],
    '#element_validate' => array(
      'itoggle_field_widget_validate',
    ),
    '#value_key' => 'value',
  );
  return $element;
}

/**
 * Form element validation handler for itoggle element.
 * 
 * @see itoggle_field_widget_form().
 */
function itoggle_field_widget_validate($element, &$form_state) {
  if ($element['#required'] && $element['#value'] == '_none') {
    form_error($element, t('!name field is required.', array(
      '!name' => $element['#title'],
    )));
  }
  $items = array(
    array(
      'value' => $element['#value'],
    ),
  );
  form_set_value($element, $items, $form_state);
}