Date.php in YAML Form 8
File
src/Plugin/YamlFormElement/Date.php
View source
<?php
namespace Drupal\yamlform\Plugin\YamlFormElement;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\YamlFormSubmissionInterface;
class Date extends DateBase {
public function getDefaultProperties() {
$date_format = '';
if (!defined('MAINTENANCE_MODE')) {
if ($date_format_entity = DateFormat::load('html_date')) {
$date_format = $date_format_entity
->getPattern();
}
}
return parent::getDefaultProperties() + [
'date_date_format' => $date_format,
'step' => '',
];
}
public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
parent::prepare($element, $yamlform_submission);
$element['#attributes']['type'] = 'date';
$element['#attached']['library'][] = 'yamlform/yamlform.element.date';
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$date_format = DateFormat::load('html_date')
->getPattern();
$form['date']['date_date_format'] = [
'#type' => 'yamlform_select_other',
'#title' => $this
->t('Date format'),
'#options' => [
$date_format => $this
->t('Year-Month-Date (@date)', [
'@date' => date($date_format),
]),
],
'#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>.'),
];
$form['date']['step'] = [
'#type' => 'number',
'#title' => $this
->t('Step'),
'#description' => $this
->t('Specifies the legal number intervals.'),
'#min' => 1,
'#size' => 4,
'#weight' => 10,
];
return $form;
}
}
Classes
Name |
Description |
Date |
Provides a 'date' element. |