You are here

function _partial_date_field_widget_form in Partial Date 7

Implements hook_field_widget_form().

1 call to _partial_date_field_widget_form()
partial_date_field_widget_form in ./partial_date.module
Implements hook_field_widget_form().

File

./partial_date.admin.inc, line 884
Less freq. functions for field administration.

Code

function _partial_date_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
  $current_langcode = LANGUAGE_NONE;
  $help_txt = _partial_date_widget_help_text($instance, $current_langcode);
  $settings = $instance['widget']['settings'];
  $has_range = strpos($field['type'], 'range');
  $inline_range_style = FALSE;
  if ($has_range && !empty($settings['theme_overrides']['range_inline'])) {
    $inline_range_style = ' style="' . _partial_date_inline_float_css(FALSE) . '"';
  }

  // Fix the title on multi-value fields.
  if (empty($base['#title'])) {
    $base['#title_display'] = 'invisible';
  }
  elseif ($field['cardinality'] == 1) {
    $base['#type'] = 'item';
  }
  if (isset($items[$delta])) {
    $value = $items[$delta];
  }
  else {
    $value = $delta == 0 && !empty($instance['default_value'][0]) ? $instance['default_value'][0] : NULL;
  }
  $estimate_options = partial_date_field_estimates($field);

  // General styles to nicely format the element inline without having to load
  // external style sheets.
  $css = variable_get('partial_date_component_field_inline_styles', _partial_date_inline_float_css());
  $css_txt = variable_get('partial_date_component_field_txt_inline_styles', 'float: left; clear: left;');

  // Correct the timezone based on the widget values.
  $tz_from = empty($value) || empty($value['from']) || empty($value['from']['timezone']) ? NULL : $value['from']['timezone'];
  $value['from']['timezone'] = partial_date_timezone_handling_correlation($tz_from, $settings['tz_handling']);
  if (!partial_date_timezone_option_is_selectable($settings['tz_handling'])) {
    unset($settings['granularity']['from']['timezone']);
    if ($has_range) {
      unset($settings['granularity']['to']['timezone']);
    }
  }
  $increments = empty($settings['increments']) ? array() : $settings['increments'];
  $base['from'] = array(
    '#type' => 'partial_datetime_element',
    '#title' => $has_range ? t('Start date') : t('Date'),
    '#title_display' => 'invisible',
    '#default_value' => $value['from'],
    '#granularity' => $settings['granularity']['from'],
    '#estimates' => $settings['estimates']['from'],
    '#estimate_options' => $estimate_options,
    '#component_styles' => $css,
    '#increments' => $increments,
  );
  if ($inline_range_style) {
    $base['from']['#attributes']['style'] = $inline_range_style;
    $base['from']['#theme_wrappers'] = array(
      'partial_date_inline_form_element',
    );
    $base['#theme'] = 'partial_date_range_inline_element';
  }
  if ($has_range) {
    $base['_separator'] = array(
      '#type' => 'markup',
      '#markup' => t('<div class="partial-date-separator"' . $inline_range_style . '>to</div>', array(), array(
        'context' => 'datetime',
      )),
    );

    // Correct the timezone based on the widget values.
    $tz_to = empty($value) || empty($value['to']) || empty($value['to']['timezone']) ? NULL : $value['to']['timezone'];
    $value['to']['timezone'] = partial_date_timezone_handling_correlation($tz_to, $settings['tz_handling']);
    $base['to'] = array(
      '#type' => 'partial_datetime_element',
      '#title' => $has_range ? t('Start date') : t('Date'),
      '#title_display' => 'invisible',
      '#default_value' => $value['to'],
      '#granularity' => $settings['granularity']['to'],
      '#estimates' => $settings['estimates']['to'],
      '#estimate_options' => $estimate_options,
      '#component_styles' => $css,
      '#increments' => $increments,
    );
    if ($inline_range_style) {
      $base['to']['#attributes']['style'] = $inline_range_style;
      $base['to']['#theme_wrappers'] = array(
        'partial_date_inline_form_element',
      );
    }
  }
  $base['#component_help'] = field_filter_xss($help_txt['components']);
  if (!empty($settings['theme_overrides']['check_approximate'])) {
    $base['check_approximate'] = array(
      '#type' => 'checkbox',
      '#title' => t('Approximation only', array(), array(
        'context' => 'datetime',
      )),
      '#default_value' => empty($value['check_approximate']) ? 0 : $value['check_approximate'],
    );
    if (!empty($help_txt['check_approximate'])) {
      $base['check_approximate']['#description'] = field_filter_xss($help_txt['check_approximate']);
    }
  }

  // Calculate these for any JScript states.
  $parents = array();
  if (!empty($base['#field_parents'])) {
    $parents = $base['#field_parents'];
  }
  elseif (!empty($base['#parents'])) {
    $parents = $base['#parents'];
  }

  // field_partial_dates[und][0][check_approximate]
  $parents[] = $field['field_name'];
  $parents[] = $langcode;
  foreach (array(
    'txt_short',
    'txt_long',
  ) as $key) {
    if (!empty($settings['theme_overrides'][$key])) {
      $description = NULL;
      if (!empty($help_txt[$key])) {
        $description = field_filter_xss($help_txt[$key]);
      }
      $base[$key] = array(
        '#type' => 'textfield',
        '#title' => $description,
        '#description' => $description,
        '#title_display' => 'invisible',
        '#default_value' => empty($value[$key]) ? '' : $value[$key],
        '#prefix' => '<div class="partial-date-' . $key . '"' . ($css_txt ? ' style="' . $css_txt . '"' : '') . '>',
        '#suffix' => '</div>',
        '#maxlength' => 255,
      );
    }
  }

  // TODO
  // Inject a couple of extra timezone options if this is the default widget.
  if (FALSE) {

    //    case '--user--':
    //    case '--site--':
  }
  $base['_remove'] = array(
    '#type' => 'checkbox',
    '#title' => t('Remove date', array(), array(
      'context' => 'datetime',
    )),
    '#default_value' => 0,
    '#access' => empty($settings['hide_remove']),
    '#prefix' => '<div class="partial-date-remove" ' . ($css_txt ? ' style="' . $css_txt . '"' : '') . '>',
    '#suffix' => '</div>',
  );
  if (!empty($help_txt['_remove'])) {
    $base['_remove']['#description'] = field_filter_xss($help_txt['_remove']);
  }
  $base['#prefix'] = '<div class="clearfix">';
  $base['#suffix'] = '</div>';
  return $base;
}