1 date_views_filter_handler_simple.inc date_views_filter_handler_simple::date_parts_form(&$form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id)

A form element to select date part values.

Parameters

string $prefix: A prefix for the date values, 'value', 'min', or 'max' .

string $source: An HTML selector for the operator for this element.

string $which: Which element to provide, 'all', 'value', or 'minmax' .

array $operator_values: An array of the allowed operators for this element.

string $identifier: Identifier of the exposed element.

string $relative_id: Form element ID to use for the relative date field.

Return value

The form date part element for this instance.:

File

core/modules/date/views/date_views_filter_handler_simple.inc, line 345
A standard Views filter for a single date field, using Date API form selectors and sql handling.

Class

date_views_filter_handler_simple
@file A standard Views filter for a single date field, using Date API form selectors and sql handling.

Code

function date_parts_form(&$form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id) {
  module_load_include('inc', 'date', 'date.elements');
  switch ($prefix) {
    case 'min':
      $label = t('Start date');
      $relative_label = t('Relative start date');
      break;
    case 'max':
      $label = t('End date');
      $relative_label = t('Relative end date');
      break;
    default:
      $label = '';
      $relative_label = t('Relative date');
      break;
  }

  $type = $this->options['form_type'];
  $format = $this->date_handler->views_formats($this->options['granularity'], 'display');
  $granularity = array_keys($this->date_handler->date_parts($this->options['granularity']));
  $relative_value = ($prefix == 'max' ? $this->options['default_to_date'] : $this->options['default_date']);

  if (!empty($form_state['exposed'])) {
    // UI when the date selector is exposed.
    $default_date = $this->date_default_value($prefix);
    $form[$prefix] = array(
      '#title' => check_plain($label),
      '#type' => $type,
      '#size' => 20,
      '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
      '#date_format' => date_limit_format($format, $granularity),
      '#date_label_position' => 'within',
      '#date_year_range' => $this->options['year_range'],
      '#process' => array($type . '_element_process'),
    );
    if ($which == 'all') {
      $form[$prefix]['#states']['visible'] = array();
      foreach ($operator_values as $operator) {
        $form[$prefix]['#states']['visible'][] = array(
          $source => array('value' => $operator),
        );
      }
    }
    if (!isset($form_state['input'][$identifier][$prefix])) {
      // Ensure these exist.
      foreach ($granularity as $key) {
        $form_state['input'][$identifier][$prefix][$key] = NULL;
      }
    }
  }
  else {
    // UI when the date selector is on the views configuration screen.
    $default_date = '';
    $form[$prefix . '_group'] = array(
      '#type' => 'fieldset',
      '#attributes' => array('class' => array('date-views-filter-fieldset')),
    );
    $form[$prefix . '_group'][$prefix . '_choose_input_type'] = array(
      '#title' => check_plain($label),
      '#type' => 'select',
      '#options' => array('date' => t('Select a date'), 'relative' => ('Enter a relative date')),
      '#attributes' => array('class' => array($prefix . '-choose-input-type')),
      '#default_value' => !empty($relative_value) ? 'relative' : 'date',
    );
    $form[$prefix . '_group'][$prefix] = array(
      '#title' => t('Select a date'),
      '#type' => $type,
      '#size' => 20,
      '#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
      '#date_format' => date_limit_format($format, $granularity),
      '#date_label_position' => 'within',
      '#date_year_range' => $this->options['year_range'],
      '#process' => array($type . '_element_process'),
      '#states' => array(
        'visible' => array(
          ":input.{$prefix}-choose-input-type" => array('value' => 'date'),
        ),
      ),
    );
    $form[$prefix . '_group'][$relative_id] = array(
      '#type' => 'textfield',
      '#title' => check_plain($relative_label),
      '#default_value' => $relative_value,
      '#description' => t('Relative dates are computed when the view is displayed. Examples: now, now +1 day, 12AM today, Monday next week. <a href="http://php.net/manual/en/datetime.formats.relative.php">More examples of relative date formats in the PHP documentation</a>.'),
      '#states' => array(
        'visible' => array(
          ":input.{$prefix}-choose-input-type" => array('value' => 'relative'),
        ),
      ),
    );
    if ($which == 'all') {
      $form[$prefix . '_group']['#states']['visible'] = array();
      foreach ($operator_values as $operator) {
        $form[$prefix . '_group']['#states']['visible'][] = array(
          $source => array('value' => $operator),
        );
      }
    }
  }
  return $form;
}