2a4ffcd297500eefac942150c0b200e35dd62166
[yaffs-website] / src / Plugin / Field / FieldWidget / DateTimeDefaultWidget.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Plugin implementation of the 'datetime_default' widget.
15  *
16  * @FieldWidget(
17  *   id = "datetime_default",
18  *   label = @Translation("Date and time"),
19  *   field_types = {
20  *     "datetime"
21  *   }
22  * )
23  */
24 class DateTimeDefaultWidget extends DateTimeWidgetBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * The date format storage.
28    *
29    * @var \Drupal\Core\Entity\EntityStorageInterface
30    */
31   protected $dateStorage;
32
33   /**
34    * {@inheritdoc}
35    */
36   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityStorageInterface $date_storage) {
37     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
38
39     $this->dateStorage = $date_storage;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
46     return new static(
47       $plugin_id,
48       $plugin_definition,
49       $configuration['field_definition'],
50       $configuration['settings'],
51       $configuration['third_party_settings'],
52       $container->get('entity.manager')->getStorage('date_format')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
60     $element = parent::formElement($items, $delta, $element, $form, $form_state);
61
62     // If the field is date-only, make sure the title is displayed. Otherwise,
63     // wrap everything in a fieldset, and the title will be shown in the legend.
64     if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
65       $element['value']['#title'] = $this->fieldDefinition->getLabel();
66       $element['value']['#description'] = $this->fieldDefinition->getDescription();
67     }
68     else {
69       $element['#theme_wrappers'][] = 'fieldset';
70     }
71
72     // Identify the type of date and time elements to use.
73     switch ($this->getFieldSetting('datetime_type')) {
74       case DateTimeItem::DATETIME_TYPE_DATE:
75         $date_type = 'date';
76         $time_type = 'none';
77         $date_format = $this->dateStorage->load('html_date')->getPattern();
78         $time_format = '';
79         break;
80
81       default:
82         $date_type = 'date';
83         $time_type = 'time';
84         $date_format = $this->dateStorage->load('html_date')->getPattern();
85         $time_format = $this->dateStorage->load('html_time')->getPattern();
86         break;
87     }
88
89     $element['value'] += [
90       '#date_date_format' => $date_format,
91       '#date_date_element' => $date_type,
92       '#date_date_callbacks' => [],
93       '#date_time_format' => $time_format,
94       '#date_time_element' => $time_type,
95       '#date_time_callbacks' => [],
96     ];
97
98     return $element;
99   }
100
101 }