You are here

function timefield_field_widget_form in Timefield 7

Implements hook_field_widget_form().

File

./timefield.module, line 735
Defines a Field API field for time

Code

function timefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $settings = $field['settings'];
  $instance_settings = $instance['settings'];
  $display_settings = $instance['display']['default']['settings'];

  // If the element is hidden, it might not have display settings, so we just
  // get the defaults @see #1862316
  if (empty($display_settings)) {
    $formatter_settings = timefield_field_formatter_info();
    $display_settings = $formatter_settings['timefield_default']['settings'];
  }
  $element += array(
    '#delta' => $delta,
  );
  $instance_class = str_replace('_', '-', $element['#field_name']) . "-" . $delta;
  $value = isset($items[$delta]['value']) ? timefield_integer_to_time($display_settings['display_format'], $items[$delta]['value']) : '';
  if ($settings['weekly_summary'] || $settings['weekly_summary_with_label']) {
    $days = isset($items[$delta]['mon']) ? _timefield_weekly_summary_days_map($items[$delta]) : array();
  }
  if ($settings['totime'] == 'required' || $settings['totime'] == 'optional') {
    $value2 = isset($items[$delta]['value2']) ? timefield_integer_to_time($display_settings['display_format'], $items[$delta]['value2']) : '';
  }
  switch ($instance['widget']['type']) {
    case 'timefield_standard':
      $element += array(
        '#title' => 'Time',
        '#type' => 'fieldset',
        '#delta' => $delta,
        '#element_validate' => array(
          'timefield_time_validate',
        ),
      );
      if (!$instance_settings['disable_plugin']) {
        $js_settings = _timefield_js_settings($instance_class, $instance_settings['input_format']);
        $context = array(
          'type' => 'field',
          'field' => $field,
          'instance' => $instance,
        );
        drupal_alter('timefield_js_settings', $js_settings[$instance_class], $context);
        $element['#attached'] = array(
          'library' => array(
            array(
              'timefield',
              'timepicker',
            ),
          ),
          'js' => array(
            drupal_get_path('module', 'timefield') . '/js/timefield.js',
            array(
              'data' => array(
                'timefield' => $js_settings,
              ),
              'type' => 'setting',
            ),
          ),
        );
      }
      if ($settings['weekly_summary_with_label']) {
        $element['label'] = array(
          '#title' => t('Label'),
          '#description' => t('Enter a label for the summary'),
          '#type' => 'textfield',
          '#default_value' => isset($items[$delta]['label']) ? $items[$delta]['label'] : '',
          '#size' => 40,
          '#maxlength' => 60,
        );
      }
      $element['value'] = array(
        '#title' => t('Time'),
        '#description' => t('Enter a time value, in any format'),
        '#type' => 'textfield',
        '#default_value' => _timefield_map_input_format_to_display_format($value, $instance_settings['input_format']),
        '#size' => 15,
        '#maxlength' => 15,
        '#attributes' => array(
          'class' => array(
            'edit-timefield-timepicker',
            $instance_class,
          ),
        ),
        '#required' => $instance['required'] && !$delta,
      );

      // Add second element if totime is required or optional.
      if ($settings['totime'] == 'required' || $settings['totime'] == 'optional') {
        $element['value2'] = array(
          '#title' => t('End Time'),
          '#description' => t('Enter a time value, in any format'),
          '#type' => 'textfield',
          '#default_value' => _timefield_map_input_format_to_display_format($value2, $instance_settings['input_format']),
          '#size' => 15,
          '#maxlength' => 15,
          '#attributes' => array(
            'class' => array(
              'edit-timefield-timepicker',
              $instance_class,
            ),
          ),
          '#required' => $settings['totime'] == 'required' && $instance['required'] && !$delta,
        );

        // Add the appearance of required-ness for required totime
        if ($settings['totime'] == 'required' && !$instance['required']) {
          $element['value2']['#states'] = array(
            // Only require this field if the value field has a value.
            'required' => array(
              ':input[name="' . $field['field_name'] . '[' . $langcode . '][' . $delta . '][value]"]' => array(
                'filled' => TRUE,
              ),
            ),
          );
        }
      }
      if ($settings['weekly_summary'] || $settings['weekly_summary_with_label']) {
        $element['days'] = array(
          '#title' => t('Days'),
          '#description' => t('Select the days this schedule applies to'),
          '#type' => 'checkboxes',
          '#options' => _timefield_weekly_summary_days(),
          '#default_value' => $days,
          '#attributes' => array(
            'class' => array(
              'edit-field-timefield-days',
            ),
          ),
        );
      }
      break;
  }
  return $element;
}