You are here

function commerce_file_limit_duration_element_process in Commerce File 7

FAPI process callback for limit textfield element type.

1 string reference to 'commerce_file_limit_duration_element_process'
commerce_file_element_info in includes/commerce_file.elements.inc
Implements hook_element_info().

File

includes/commerce_file.elements.inc, line 187
Commerce File form elements

Code

function commerce_file_limit_duration_element_process($element, &$form_state) {

  // Put the child elements in a container div.
  $element['#prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : '';
  $element['#prefix'] = '<div class="commerce-file-limit-duration-element commerce-file-limit-element">' . $element['#prefix'];
  unset($element['#field_prefix']);

  // move description to the top
  if (isset($element['#description'])) {
    $element['description'] = array(
      '#markup' => '<div class="description">' . $element['#description'] . '</div>',
      '#weight' => -10,
    );
    unset($element['#description']);
  }

  // close container
  $element['#suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : '';
  $element['#suffix'] .= '</div>';
  unset($element['#field_suffix']);

  // attach css
  $element['#attached']['css'][] = drupal_get_path('module', 'commerce_file') . '/css/commerce_file.forms.css';

  // build limit mode and value
  $element['#mode_options'] = _commerce_file_limit_element_get_mode_options($element, '_commerce_file_limit_format_interval');
  $defaults = _commerce_file_limit_element_get_defaults($element);
  $element['mode'] = array(
    '#type' => 'radios',
    '#options' => $element['#mode_options'],
    '#default_value' => $defaults['mode'],
    '#attributes' => array(
      'class' => array(
        'commerce-file-limit-element-mode',
      ),
    ),
  );
  $default_value = NULL;
  $default_unit = 86400;
  if (isset($defaults['value'])) {

    // split into value and unit of greatest divisor
    list($default_value, $default_unit) = _commerce_file_limit_duration_element_explode_value($defaults['value']);
  }
  $element['value'] = array(
    '#type' => 'container',
    'value' => array(
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $default_value,
      '#element_validate' => array(
        '_commerce_file_element_validate_integer_positive_or_zero',
      ),
    ),
    'unit' => array(
      '#type' => 'select',
      '#default_value' => $default_unit,
      '#options' => _commerce_file_limit_duration_element_units(),
    ),
    'description' => array(
      '#markup' => '<div class="description">' . t('Enter an integer value or zero.<br/>Months are based on 30 days.') . '</div>',
    ),
    '#attributes' => array(
      'class' => array(
        'commerce-file-limit-element-value container-inline',
      ),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="' . $element['#name'] . '[mode]"]' => array(
          'value' => 'value',
        ),
      ),
    ),
  );
  return $element;
}