DatetimeDefaultWidget.php 4.75 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * @file
 * Contains \Drupal\datetime\Plugin\field\widget\DateTimeDefaultWidget.
 */

namespace Drupal\datetime\Plugin\field\widget;

9
use Drupal\Component\Annotation\Plugin;
10
11
12
13
14
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\PluginSettingsBase;
15
use Drupal\field\Plugin\Core\Entity\FieldInstance;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Plugin implementation of the 'datetime_default' widget.
 *
 * @Plugin(
 *   id = "datetime_default",
 *   module = "datetime",
 *   label = @Translation("Date and time"),
 *   field_types = {
 *     "datetime"
 *   }
 * )
 */
class DateTimeDefaultWidget extends WidgetBase {

  /**
   * Constructs a DateTimeDefault Widget object.
   *
   * @param array $plugin_id
   *   The plugin_id for the widget.
37
38
   * @param array $plugin_definition
   *   The plugin implementation definition.
39
   * @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance
40
41
42
43
44
45
   *   The field instance to which the widget is associated.
   * @param array $settings
   *   The widget settings.
   * @param int $weight
   *   The widget weight.
   */
46
  public function __construct($plugin_id, array $plugin_definition, FieldInstance $instance, array $settings, $weight) {
47
48
    // Identify the function used to set the default value.
    $instance['default_value_function'] = $this->defaultValueFunction();
49
    parent::__construct($plugin_id, $plugin_definition, $instance, $settings, $weight);
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  }

  /**
   * Return the callback used to set a date default value.
   *
   * @return string
   *   The name of the callback to use when setting a default date value.
   */
  public function defaultValueFunction() {
    return 'datetime_default_value';
  }

  /**
   * Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
   *
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {

    $field = $this->field;
    $instance = $this->instance;
    $format_type = datetime_default_format_type();

    // We are nesting some sub-elements inside the parent, so we need a wrapper.
    // We also need to add another #title attribute at the top level for ease in
    // identifying this item in error messages. We do not want to display this
    // title because the actual title display is handled at a higher level by
    // the Field module.

    $element['#theme_wrappers'][] = 'datetime_wrapper';
    $element['#attributes']['class'][] = 'container-inline';
    $element['#element_validate'][] = 'datetime_datetime_widget_validate';

    // Identify the type of date and time elements to use.
    switch ($field['settings']['datetime_type']) {
      case 'date':
        $date_type = 'date';
        $time_type = 'none';
        $date_format = config('system.date')->get('formats.html_date.pattern.' . $format_type);
        $time_format = '';
        $element_format = $date_format;
        $storage_format = DATETIME_DATE_STORAGE_FORMAT;
        break;

      default:
        $date_type = 'date';
        $time_type = 'time';
        $date_format = config('system.date')->get('formats.html_date.pattern.' . $format_type);
        $time_format = config('system.date')->get('formats.html_time.pattern.' . $format_type);
        $element_format = $date_format . ' ' . $time_format;
        $storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
        break;
    }

    $element['value'] = array(
      '#type' => 'datetime',
      '#default_value' => NULL,
      '#date_increment' => 1,
      '#date_date_format'=>  $date_format,
      '#date_date_element' => $date_type,
      '#date_date_callbacks' => array(),
      '#date_time_format' => $time_format,
      '#date_time_element' => $time_type,
      '#date_time_callbacks' => array(),
      '#date_timezone' => drupal_get_user_timezone(),
      '#required' => $element['#required'],
    );

    // Set the storage and widget options so the validation can use them. The
    // validator will not have access to field or instance settings.
    $element['value']['#date_element_format'] = $element_format;
    $element['value']['#date_storage_format'] = $storage_format;

    if (!empty($items[$delta]['date'])) {
      $date = $items[$delta]['date'];
      // The date was created and verified during field_load(), so it is safe to
      // use without further inspection.
      $date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
      if ($field['settings']['datetime_type'] == 'date') {
        // A date without time will pick up the current time, use the default
        // time.
        datetime_date_default_time($date);
      }
      $element['value']['#default_value'] = $date;
    }

    return $element;
  }

}