Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / src / Form / DateFormatFormBase.php
1 <?php
2
3 namespace Drupal\system\Form;
4
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;
12
13 /**
14  * Provides a base form for date formats.
15  */
16 abstract class DateFormatFormBase extends EntityForm {
17
18   /**
19    * The date formatter service.
20    *
21    * @var \Drupal\Core\Datetime\DateFormatterInterface
22    */
23   protected $dateFormatter;
24
25   /**
26    * The date format storage.
27    *
28    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
29    */
30   protected $dateFormatStorage;
31
32   /**
33    * Constructs a new date format form.
34    *
35    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
36    *   The date service.
37    * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $date_format_storage
38    *   The date format storage.
39    */
40   public function __construct(DateFormatterInterface $date_formatter, ConfigEntityStorageInterface $date_format_storage) {
41     $date = new DrupalDateTime();
42
43     $this->dateFormatter = $date_formatter;
44     $this->dateFormatStorage = $date_format_storage;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container) {
51     return new static(
52       $container->get('date.formatter'),
53       $container->get('entity.manager')->getStorage('date_format')
54     );
55   }
56
57   /**
58    * Checks for an existing date format.
59    *
60    * @param string|int $entity_id
61    *   The entity ID.
62    * @param array $element
63    *   The form element.
64    *
65    * @return bool
66    *   TRUE if this format already exists, FALSE otherwise.
67    */
68   public function exists($entity_id, array $element) {
69     return (bool) $this->dateFormatStorage
70       ->getQuery()
71       ->condition('id', $element['#field_prefix'] . $entity_id)
72       ->execute();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function form(array $form, FormStateInterface $form_state) {
79     $form['label'] = [
80       '#type' => 'textfield',
81       '#title' => 'Name',
82       '#maxlength' => 100,
83       '#description' => t('Name of the date format'),
84       '#default_value' => $this->entity->label(),
85     ];
86
87     $form['id'] = [
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(),
92       '#machine_name' => [
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".'),
96       ],
97     ];
98     $form['date_format_pattern'] = [
99       '#type' => 'textfield',
100       '#title' => t('Format string'),
101       '#maxlength' => 100,
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.'),
103       '#required' => TRUE,
104       '#attributes' => [
105         'data-drupal-date-formatter' => 'source',
106       ],
107       '#field_suffix' => ' <small class="js-hide" data-drupal-date-formatter="preview">' . $this->t('Displayed as %date_format', ['%date_format' => '']) . '</small>',
108     ];
109
110     $form['langcode'] = [
111       '#type' => 'language_select',
112       '#title' => t('Language'),
113       '#languages' => LanguageInterface::STATE_ALL,
114       '#default_value' => $this->entity->language()->getId(),
115     ];
116     $form['#attached']['drupalSettings']['dateFormats'] = $this->dateFormatter->getSampleDateFormats();
117     $form['#attached']['library'][] = 'system/drupal.system.date';
118     return parent::form($form, $form_state);
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function validateForm(array &$form, FormStateInterface $form_state) {
125     parent::validateForm($form, $form_state);
126
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.'));
133         continue;
134       }
135     }
136   }
137
138   /**
139    * {@inheritdoc}
140    */
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);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
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.'));
153     }
154     else {
155       drupal_set_message(t('Custom date format added.'));
156     }
157     $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
158   }
159
160 }