You are here

public function DateBase::form in Webform 8.5

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

3 calls to DateBase::form()
Date::form in src/Plugin/WebformElement/Date.php
Gets the actual configuration webform array to be built.
DateList::form in src/Plugin/WebformElement/DateList.php
Gets the actual configuration webform array to be built.
DateTime::form in src/Plugin/WebformElement/DateTime.php
Gets the actual configuration webform array to be built.
3 methods override DateBase::form()
Date::form in src/Plugin/WebformElement/Date.php
Gets the actual configuration webform array to be built.
DateList::form in src/Plugin/WebformElement/DateList.php
Gets the actual configuration webform array to be built.
DateTime::form in src/Plugin/WebformElement/DateTime.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/DateBase.php, line 224

Class

DateBase
Provides a base 'date' class.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // Append supported date input format to #default_value description.
  $form['default']['default_value']['#description'] .= '<br /><br />' . $this
    ->t('Accepts any date 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 today, +2 months, and Dec 9 2004 are all valid.');

  // Append token date format to #default_value description.
  $form['default']['default_value']['#description'] .= '<br /><br />' . $this
    ->t("You may use tokens. Tokens should use the 'html_date' or 'html_datetime' date format. (i.e. @date_format)", [
    '@date_format' => '[current-user:field_date_of_birth:date:html_date]',
  ]);

  // Allow custom date formats to be entered.
  $form['display']['format']['#type'] = 'webform_select_other';
  $form['display']['format']['#other__option_label'] = $this
    ->t('Custom date format…');
  $form['display']['format']['#other__description'] = $this
    ->t('A user-defined date format. See the <a href="http://php.net/manual/function.date.php">PHP manual</a> for available options.');
  $form['date'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Date settings'),
  ];

  // Date min/max validation.
  $form['date']['date_container'] = $this
    ->getFormInlineContainer() + [
    '#weight' => 10,
  ];
  $form['date']['date_container']['date_date_min'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Date minimum'),
    '#description' => $this
      ->t('Specifies the minimum date.') . ' ' . $this
      ->t('To limit the minimum date to the submission date use the <code>[webform_submission:created:html_date]</code> token.') . '<br /><br />' . $this
      ->t('Accepts any date 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 today, +2 months, and Dec 9 2004 are all valid.'),
  ];
  $form['date']['date_container']['date_date_max'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Date maximum'),
    '#description' => $this
      ->t('Specifies the maximum date.') . ' ' . $this
      ->t('To limit the maximum date to the submission date use the <code>[webform_submission:created:html_date]</code> token.') . '<br /><br />' . $this
      ->t('Accepts any date 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 today, +2 months, and Dec 9 2004 are all valid.'),
  ];

  // Date days of the week validation.
  $form['date']['date_days'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Date days of the week'),
    '#options' => DateHelper::weekDaysAbbr(TRUE),
    '#element_validate' => [
      [
        '\\Drupal\\webform\\Utility\\WebformElementHelper',
        'filterValues',
      ],
    ],
    '#description' => $this
      ->t('Specifies the day(s) of the week. Please note, the date picker will disable unchecked days of the week.'),
    '#options_display' => 'side_by_side',
    '#required' => TRUE,
    '#weight' => 20,
  ];

  // Date/time min/max validation.
  if ($this
    ->hasProperty('date_date_min') && $this
    ->hasProperty('date_time_min') && $this
    ->hasProperty('date_min')) {
    $form['validation']['date_min_max_message'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#access' => TRUE,
      '#message_message' => $this
        ->t("'Date/time' minimum or maximum should not be used with 'Date' or 'Time' specific minimum or maximum.") . '<br/>' . '<strong>' . $this
        ->t('This can cause unexpected validation errors.') . '</strong>',
      '#message_close' => TRUE,
      '#message_storage' => WebformMessageElement::STORAGE_SESSION,
      '#states' => [
        'visible' => [
          [
            ':input[name="properties[date_date_min]"]' => [
              'filled' => TRUE,
            ],
          ],
          [
            ':input[name="properties[date_date_max]"]' => [
              'filled' => TRUE,
            ],
          ],
          [
            ':input[name="properties[date_time_min]"]' => [
              'filled' => TRUE,
            ],
          ],
          [
            ':input[name="properties[date_time_max]"]' => [
              'filled' => TRUE,
            ],
          ],
        ],
      ],
    ];
  }
  $form['validation']['date_container'] = $this
    ->getFormInlineContainer();
  $form['validation']['date_container']['date_min'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Date/time minimum'),
    '#description' => $this
      ->t('Specifies the minimum date/time.') . ' ' . $this
      ->t('To limit the minimum date/time to the submission date/time use the <code>[webform_submission:created:html_datetime]</code> token.') . '<br /><br />' . $this
      ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date/Time Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 10:00 PM are all valid.'),
  ];
  $form['validation']['date_container']['date_max'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Date/time maximum'),
    '#description' => $this
      ->t('Specifies the maximum date/time.') . ' ' . $this
      ->t('To limit the maximum date/time to the submission date/time use the <code>[webform_submission:created:html_datetime]</code> token.') . '<br /><br />' . $this
      ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date/Time Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 10:00 PM are all valid.'),
  ];
  return $form;
}