You are here

public function WebformTime::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformTime.php \Drupal\webform\Plugin\WebformElement\WebformTime::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformElementBase::form

File

src/Plugin/WebformElement/WebformTime.php, line 78

Class

WebformTime
Provides a 'webform_time' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Append supported time input format to #default_value description.
  $form['default']['default_value']['#description'] .= '<br />' . $this
    ->t('Accepts any time in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as now, +2 hours, and 4:30 PM are all valid.');

  // Time.
  $form['time'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Time settings'),
  ];
  $form['time']['timepicker'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use time picker'),
    '#description' => $this
      ->t('If checked, HTML5 time element will be replaced with <a href="http://jonthornton.github.io/jquery-timepicker/">jQuery UI timepicker</a>'),
    '#return_value' => TRUE,
  ];
  $form['time']['time_format'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Time format'),
    '#options' => [
      'H:i' => $this
        ->t('24 hour - @format (@time)', [
        '@format' => 'H:i',
        '@time' => static::formatTime('H:i'),
      ]),
      'H:i:s' => $this
        ->t('24 hour with seconds - @format (@time)', [
        '@format' => 'H:i:s',
        '@time' => static::formatTime('H:i:s'),
      ]),
      'g:i A' => $this
        ->t('12 hour - @format (@time)', [
        '@format' => 'g:i A',
        '@time' => static::formatTime('g:i A'),
      ]),
      'g:i:s A' => $this
        ->t('12 hour with seconds - @format (@time)', [
        '@format' => 'g:i:s A',
        '@time' => static::formatTime('g:i:s A'),
      ]),
    ],
    '#other__option_label' => $this
      ->t('Custom…'),
    '#other__placeholder' => $this
      ->t('Custom time format…'),
    '#other__description' => $this
      ->t('Enter time format using <a href="http://php.net/manual/en/function.date.php">Time Input Format</a>.'),
  ];
  $form['time']['time_container'] = $this
    ->getFormInlineContainer();
  $form['time']['time_container']['min'] = [
    '#type' => 'webform_time',
    '#title' => $this
      ->t('Minimum'),
    '#description' => $this
      ->t('Specifies the minimum time.'),
  ];
  $form['time']['time_container']['max'] = [
    '#type' => 'webform_time',
    '#title' => $this
      ->t('Maximum'),
    '#description' => $this
      ->t('Specifies the maximum time.'),
  ];
  $form['time']['step'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Step'),
    '#description' => $this
      ->t('Specifies the minute intervals.'),
    '#options' => [
      60 => $this
        ->t('1 minute'),
      300 => $this
        ->t('5 minutes'),
      600 => $this
        ->t('10 minutes'),
      900 => $this
        ->t('15 minutes'),
      1200 => $this
        ->t('20 minutes'),
      1800 => $this
        ->t('30 minutes'),
    ],
    '#other__type' => 'number',
    '#other__description' => $this
      ->t('Enter interval in seconds.'),
  ];

  // Show placeholder for the timepicker only.
  $form['form']['placeholder']['#states'] = [
    'visible' => [
      ':input[name="properties[timepicker]"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  return $form;
}