3 namespace Drupal\system\Form;
5 use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6 use Drupal\Core\Datetime\DateFormatterInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Drupal\Core\Datetime\DrupalDateTime;
11 use Drupal\Core\Entity\EntityForm;
14 * Provides a base form for date formats.
16 abstract class DateFormatFormBase extends EntityForm {
19 * The date formatter service.
21 * @var \Drupal\Core\Datetime\DateFormatterInterface
23 protected $dateFormatter;
26 * The date format storage.
28 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
30 protected $dateFormatStorage;
33 * Constructs a new date format form.
35 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
37 * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $date_format_storage
38 * The date format storage.
40 public function __construct(DateFormatterInterface $date_formatter, ConfigEntityStorageInterface $date_format_storage) {
41 $date = new DrupalDateTime();
43 $this->dateFormatter = $date_formatter;
44 $this->dateFormatStorage = $date_format_storage;
50 public static function create(ContainerInterface $container) {
52 $container->get('date.formatter'),
53 $container->get('entity.manager')->getStorage('date_format')
58 * Checks for an existing date format.
60 * @param string|int $entity_id
62 * @param array $element
66 * TRUE if this format already exists, FALSE otherwise.
68 public function exists($entity_id, array $element) {
69 return (bool) $this->dateFormatStorage
71 ->condition('id', $element['#field_prefix'] . $entity_id)
78 public function form(array $form, FormStateInterface $form_state) {
80 '#type' => 'textfield',
83 '#description' => t('Name of the date format'),
84 '#default_value' => $this->entity->label(),
88 '#type' => 'machine_name',
89 '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
90 '#disabled' => !$this->entity->isNew(),
91 '#default_value' => $this->entity->id(),
93 'exists' => [$this, 'exists'],
94 'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
95 'error' => $this->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'),
98 $form['date_format_pattern'] = [
99 '#type' => 'textfield',
100 '#title' => t('Format string'),
102 '#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.'),
105 'data-drupal-date-formatter' => 'source',
107 '#field_suffix' => ' <small class="js-hide" data-drupal-date-formatter="preview">' . $this->t('Displayed as %date_format', ['%date_format' => '']) . '</small>',
110 $form['langcode'] = [
111 '#type' => 'language_select',
112 '#title' => t('Language'),
113 '#languages' => LanguageInterface::STATE_ALL,
114 '#default_value' => $this->entity->language()->getId(),
116 $form['#attached']['drupalSettings']['dateFormats'] = $this->dateFormatter->getSampleDateFormats();
117 $form['#attached']['library'][] = 'system/drupal.system.date';
118 return parent::form($form, $form_state);
124 public function validateForm(array &$form, FormStateInterface $form_state) {
125 parent::validateForm($form, $form_state);
127 // The machine name field should already check to see if the requested
128 // machine name is available.
129 $pattern = trim($form_state->getValue('date_format_pattern'));
130 foreach ($this->dateFormatStorage->loadMultiple() as $format) {
131 if ($format->getPattern() == $pattern && ($format->id() == $this->entity->id())) {
132 drupal_set_message(t('The existing format/name combination has not been altered.'));
141 public function submitForm(array &$form, FormStateInterface $form_state) {
142 $form_state->setValue('pattern', trim($form_state->getValue('date_format_pattern')));
143 parent::submitForm($form, $form_state);
149 public function save(array $form, FormStateInterface $form_state) {
150 $status = $this->entity->save();
151 if ($status == SAVED_UPDATED) {
152 drupal_set_message(t('Custom date format updated.'));
155 drupal_set_message(t('Custom date format added.'));
157 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));