You are here

form_options_attributes_test.module in Form Options Attributes 7

Form Options Attributes test module

File

tests/form_options_attributes_test.module
View source
<?php

/**
 * @file
 * Form Options Attributes test module
 */

/**
 * Implements hook_menu().
 */
function form_options_attributes_test_menu() {
  $items = array();
  $items['form-options-attributes-test'] = array(
    'title' => 'Form Options Attributes Test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'form_options_attributes_test_form',
    ),
    'access callback' => TRUE,
  );
  $items['form-options-attributes-test-optgroup'] = array(
    'title' => 'Form Options Attributes Test with OptGroup',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'form_options_attributes_test_optgroup_form',
    ),
    'access callback' => TRUE,
  );
  return $items;
}
function form_options_attributes_test_form($form, &$form_state) {
  $states = array(
    'AL' => t('Alabama'),
    'AK' => t('Alaska'),
    'AZ' => t('Arizona'),
    'AR' => t('Arkansas'),
    // ..
    'WI' => t('Wisconsin'),
    'WY' => t('Wyoming'),
  );
  $states_attributes = array(
    'AL' => array(
      'class' => array(
        'southeast',
      ),
      'data-bbq-meat' => 'pork',
    ),
    'AK' => array(
      'class' => array(
        'non-contiguous',
      ),
      'data-bbq-meat' => 'salmon',
    ),
    'AZ' => array(
      'class' => array(
        'southwest',
      ),
      'data-bbq-meat' => 'rattlesnake',
    ),
    'AR' => array(
      'class' => array(
        'south',
      ),
      'data-bbq-meat' => 'beef',
    ),
    // ...
    'WI' => array(
      'class' => array(
        'midwest',
      ),
      'data-bbq-meat' => 'cheese',
    ),
    'WY' => array(
      'class' => array(
        'flyover',
      ),
      'data-bbq-meat' => 'bison',
    ),
  );
  $form['states'] = array(
    '#type' => 'select',
    '#title' => t('States'),
    '#options' => $states,
    '#options_attributes' => $states_attributes,
    '#attributes' => array(
      'class' => array(
        'states-bbq-selector',
      ),
    ),
  );
  $form['states_radio'] = array(
    '#type' => 'radios',
    '#title' => t('States'),
    '#options' => $states,
    '#options_attributes' => $states_attributes,
    '#attributes' => array(
      'class' => array(
        'states-bbq-selector-radios',
      ),
    ),
  );
  $form['states_checkboxes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('States'),
    '#options' => $states,
    '#options_attributes' => $states_attributes,
    '#attributes' => array(
      'class' => array(
        'states-bbq-selector-checkboxes',
      ),
    ),
  );
  return $form;
}
function form_options_attributes_test_optgroup_form($form, &$form_state) {
  $form['states'] = array(
    '#type' => 'select',
    '#title' => t('States and Provinces'),
    '#options' => array(
      t('United States') => array(
        'AL' => t('Alabama'),
        'AK' => t('Alaska'),
        'AZ' => t('Arizona'),
        'AR' => t('Arkansas'),
        // ..
        'WI' => t('Wisconsin'),
        'WY' => t('Wyoming'),
      ),
      t('Canada') => array(
        'AB' => t('Alberta'),
        'BC' => t('British Columbia'),
        // ..
        'NU' => t('Nunavut'),
        'YT' => t('Yukon'),
      ),
    ),
    '#options_attributes' => array(
      'AL' => array(
        'class' => array(
          'southeast',
        ),
        'data-bbq-meat' => 'pork',
      ),
      'AK' => array(
        'class' => array(
          'non-contiguous',
        ),
        'data-bbq-meat' => 'salmon',
      ),
      'AZ' => array(
        'class' => array(
          'southwest',
        ),
        'data-bbq-meat' => 'rattlesnake',
      ),
      'AR' => array(
        'class' => array(
          'south',
        ),
        'data-bbq-meat' => 'beef',
      ),
      // ...
      'WI' => array(
        'class' => array(
          'midwest',
        ),
        'data-bbq-meat' => 'cheese',
      ),
      'WY' => array(
        'class' => array(
          'flyover',
        ),
        'data-bbq-meat' => 'bison',
      ),
      'AB' => array(
        'class' => array(
          'rocky',
        ),
        'data-bbq-meat' => 'beaver',
      ),
      'BC' => array(
        'class' => array(
          'coastal',
        ),
        'data-bbq-meat' => 'otter',
      ),
      // ..
      'NU' => array(
        'class' => array(
          'arctic',
        ),
        'data-bbq-meat' => 'walrus',
      ),
      'YT' => array(
        'class' => array(
          'goldy',
        ),
        'data-bbq-meat' => 'moose',
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'states-bbq-selector',
      ),
    ),
  );
  return $form;
}