You are here

public function Date::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/Date.php \Drupal\webform\Plugin\WebformElement\Date::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 DateBase::form

File

src/Plugin/WebformElement/Date.php, line 127

Class

Date
Provides a 'date' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  if (!$this
    ->datePickerExists()) {
    return $form;
  }
  if ($this
    ->datePickerExists()) {
    $form['date']['datepicker'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use date picker'),
      '#description' => $this
        ->t('If checked, the HTML5 date element will be replaced with a <a href="https://jqueryui.com/datepicker/">jQuery UI datepicker</a>'),
      '#return_value' => TRUE,
    ];
    $form['date']['datepicker_button'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show date picker button'),
      '#description' => $this
        ->t('If checked, date picker will include a calendar button'),
      '#return_value' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="properties[datepicker]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $date_format = DateFormat::load('html_date')
      ->getPattern();
    $form['date']['date_date_format'] = [
      '#type' => 'webform_select_other',
      '#title' => $this
        ->t('Date format'),
      '#options' => [
        $date_format => $this
          ->t('HTML date - @format (@date)', [
          '@format' => $date_format,
          '@date' => static::formatDate($date_format),
        ]),
        'l, F j, Y' => $this
          ->t('Long date - @format (@date)', [
          '@format' => 'l, F j, Y',
          '@date' => static::formatDate('l, F j, Y'),
        ]),
        'D, m/d/Y' => $this
          ->t('Medium date - @format (@date)', [
          '@format' => 'D, m/d/Y',
          '@date' => static::formatDate('D, m/d/Y'),
        ]),
        'm/d/Y' => $this
          ->t('Short date - @format (@date)', [
          '@format' => 'm/d/Y',
          '@date' => static::formatDate('m/d/Y'),
        ]),
      ],
      '#description' => $this
        ->t("Date format is only applicable for browsers that do not have support for the HTML5 date element. Browsers that support the HTML5 date element will display the date using the user's preferred format."),
      '#other__option_label' => $this
        ->t('Custom…'),
      '#other__placeholder' => $this
        ->t('Custom date format…'),
      '#other__description' => $this
        ->t('Enter date format using <a href="http://php.net/manual/en/function.date.php">Date Input Format</a>.'),
      '#states' => [
        'visible' => [
          ':input[name="properties[datepicker]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Show placeholder for the datepicker only.
    $form['form']['placeholder']['#states'] = [
      'visible' => [
        ':input[name="properties[datepicker]"]' => [
          'checked' => TRUE,
        ],
      ],
    ];
  }
  $form['date']['date_container']['step'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Step'),
    '#description' => $this
      ->t('Specifies the legal number intervals.'),
    '#min' => 1,
    '#size' => 4,
    '#states' => [
      'invisible' => [
        ':input[name="properties[datepicker]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  return $form;
}