You are here

function authcache_process_duration_select in Authenticated User Page Caching (Authcache) 7.2

Expand duration element to popup menu and text-field for custom values.

Related topics

1 string reference to 'authcache_process_duration_select'
authcache_element_info in ./authcache.module
Implements hook_element_info().

File

./authcache.module, line 1212
Authenticated User Page Caching (and anonymous users, too!)

Code

function authcache_process_duration_select($element, &$form_state) {
  $durations = isset($element['#durations']) ? $element['#durations'] : array();
  $empty_option = isset($element['#empty_option']) ? $element['#empty_option'] : NULL;
  $empty_value = isset($element['#empty_value']) ? $element['#empty_value'] : NULL;
  $zero_duration = isset($element['#zero_duration']) ? $element['#zero_duration'] : t('Temporary');

  // Determine default values for select and custom textfield.
  $select_default_value = NULL;
  $custom_default_value = NULL;
  if (isset($element['#default_value'])) {
    $select_default_value = $element['#default_value'];
    $custom_default_value = $element['#default_value'];
    if (!in_array($select_default_value, $durations)) {
      $select_default_value = 'custom';
    }
    elseif ($custom_default_value == 0) {

      // We require a positive integer, therefore clear out default value for
      // custom ttl when it is set to 0.
      $custom_default_value = NULL;
    }
  }

  // Generate options.
  // Necessary until #1272900 lands
  // @ignore style_function_spacing
  $options = drupal_map_assoc($durations, function ($duration) use ($zero_duration) {
    return (int) $duration > 0 ? format_interval($duration) : $zero_duration;
  });
  $options['custom'] = t('Custom');

  // Add select for standard durations.
  $element['select'] = array(
    '#type' => 'select',
    '#id' => drupal_html_id($element['#id'] . '-select'),
    '#options' => $options,
    '#default_value' => $select_default_value,
    '#empty_option' => $empty_option,
    '#empty_value' => $empty_value,
  );

  // Add textfield for custom duration.
  $element['custom'] = array(
    '#type' => 'textfield',
    '#id' => drupal_html_id($element['#id'] . '-custom'),
    '#size' => '25',
    '#maxlength' => '30',
    '#default_value' => $custom_default_value,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
    '#states' => array(
      'visible' => array(
        '#' . $element['select']['#id'] => array(
          'value' => 'custom',
        ),
      ),
    ),
  );
  $element['#tree'] = TRUE;
  return $element;
}