You are here

encryptfapi_test.module in Encrypt Form API 7.2

Declares a form to test Form API encryption.

File

tests/encryptfapi_test.module
View source
<?php

/**
 * @file
 * Declares a form to test Form API encryption.
 */

/**
 * Implements hook_menu().
 */
function encryptfapi_menu() {
  $items = array();
  $items['encryptfapi_test'] = array(
    'title' => 'Encrypt FAPI Test',
    'description' => 'A test page for the Encrypt FAPI module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'encryptfapi_test_form',
    ),
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * The form.
 */
function encryptfapi_test_form() {
  $form = array();
  $form['#encrypted_fields'] = TRUE;
  $element_types = _encryptfapi_test_form_element_types();
  $test_types = _encryptfapi_test_test_types();

  // Set up three fields for each supported element type:
  // default, specified, and nested.
  foreach ($element_types as $name => $element_type) {
    foreach ($test_types as $test_type) {
      $field_id = "encryptfapi_test_{$name}_{$test_type}";
      if ($test_type == 'nested') {
        $form["{$field_id}_container"] = array(
          '#type' => 'container',
          '#tree' => TRUE,
        );
        $field =& $form["{$field_id}_container"][$field_id];
      }
      else {
        $field =& $form[$field_id];
      }
      $field = array(
        '#type' => $name,
        '#title' => ucwords($element_type['label']) . ' ' . ucwords($test_type),
        '#encrypt' => TRUE,
      );
      if ($test_type == 'specified') {
        $field['#encrypt'] = array(
          'config' => 'default',
          'options' => array(
            'base64' => TRUE,
          ),
        );
      }
      else {
        $field['#encrypt'] = TRUE;
      }
      if (isset($element_type['options']) && $element_type['options'] === TRUE) {
        $field['#options'] = _encryptfapi_test_form_element_options();
      }
      if (in_array($name, array(
        'checkbox',
        'radio',
      ))) {
        $field['#return_value'] = 1;
      }
      if (!isset($element_type['default_value']) || $element_type['default_value'] !== FALSE) {
        if ($test_type == 'nested') {
          $default_value = variable_get("{$field_id}_container", array());
          $default_value = isset($default_value[$field_id]) ? $default_value[$field_id] : '';
        }
        else {
          $default_value = variable_get($field_id, '');
        }
        $field['#default_value'] = $default_value;
      }
    }
  }
  return system_settings_form($form);
}
function _encryptfapi_test_form_element_types() {
  $element_types = array(
    'textfield' => array(
      'name' => 'textfield',
      'label' => t('text'),
    ),
    'textarea' => array(
      'name' => 'textarea',
      'label' => t('text area'),
    ),
    'checkbox' => array(
      'name' => 'checkbox',
      'label' => t('checkbox'),
    ),
    'checkboxes' => array(
      'name' => 'checkboxes',
      'label' => t('checkboxes'),
      'options' => TRUE,
    ),
    'radio' => array(
      'name' => 'radio',
      'label' => t('radio'),
    ),
    'radios' => array(
      'name' => 'radios',
      'label' => t('radios'),
      'options' => TRUE,
    ),
    'select' => array(
      'name' => 'select',
      'label' => t('select'),
      'options' => TRUE,
    ),
    'password' => array(
      'name' => 'password',
      'label' => t('password'),
      'default_value' => FALSE,
    ),
    'password_confirm' => array(
      'name' => 'password_confirm',
      'label' => t('password confirm'),
      'default_value' => FALSE,
    ),
  );
  return $element_types;
}
function _encryptfapi_test_form_element_options() {
  return drupal_map_assoc(array(
    'foo',
    'bar',
    'baz',
    'qux',
  ));
}
function _encryptfapi_test_test_types() {
  return array(
    'default',
    'specified',
    'nested',
  );
}