diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index 0da06cb..fa4cd96 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -167,48 +167,6 @@ function datetime_datelist_widget_validate(&$element, &$form_state) { } /** - * Sets a default value for an empty date field. - * - * Callback for $instance['default_value_function'], as implemented by - * Drupal\datetime\Plugin\field\widget\DateTimeDatepicker. - * - * @param $entity_type - * - * @param $entity - * - * @param array $field - * - * @param array $instance - * - * @param $langcode - * - * - * @return array - * - */ -function datetime_default_value($entity, $field, $instance, $langcode) { - - $value = ''; - $date = ''; - if ($instance['settings']['default_value'] == 'now') { - // A default value should be in the format and timezone used for date - // storage. - $date = new DrupalDateTime('now', DATETIME_STORAGE_TIMEZONE); - $storage_format = $field['settings']['datetime_type'] == 'date' ? DATETIME_DATE_STORAGE_FORMAT: DATETIME_DATETIME_STORAGE_FORMAT; - $value = $date->format($storage_format); - } - - // We only provide a default value for the first item, as do all fields. - // Otherwise, there is no way to clear out unwanted values on multiple value - // fields. - $item = array(); - $item[0]['value'] = $value; - $item[0]['date'] = $date; - - return $item; -} - -/** * Sets a consistent time on a date without time. * * The default time for a date without time can be anything, so long as it is diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeField.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeField.php new file mode 100644 index 0000000..31036ce --- /dev/null +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeField.php @@ -0,0 +1,48 @@ +getFieldDefinition()->getFieldSetting('default_value') == DateTimeItem::DEFAULT_VALUE_NOW) { + + // A default value should be in the format and timezone used for date + // storage. + $date = new DrupalDateTime('now', DATETIME_STORAGE_TIMEZONE); + $storage_format = $this->getFieldDefinition()->getFieldSetting('default_value') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT: DATETIME_DATETIME_STORAGE_FORMAT; + $value = $date->format($storage_format); + + } + // We only provide a default value for the first item, as do all fields. + // Otherwise, there is no way to clear out unwanted values on multiple value + // fields. + $item = array(); + $item[0]['value'] = $value; + $item[0]['date'] = $date; + + return $item; + } + +} diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeItem.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeItem.php index 558ca47..223ec2b 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeItem.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeItem.php @@ -28,7 +28,8 @@ * "default_value" = "now" * }, * default_widget = "datetime_default", - * default_formatter = "datetime_default" + * default_formatter = "datetime_default", + * list_class = "\Drupal\datetime\Plugin\field\field_type\DateTimeField" * ) */ class DateTimeItem extends ConfigFieldItemBase implements PrepareCacheInterface { @@ -41,6 +42,26 @@ class DateTimeItem extends ConfigFieldItemBase implements PrepareCacheInterface static $propertyDefinitions; /** + * Defines the default value as now. + */ + const DEFAULT_VALUE_NOW = 'now'; + + /** + * Defines the default value as blank. + */ + const DEFAULT_VALUE_BLANK = 'blank'; + + /** + * Defines the field type as date. + */ + const DATETIME_TYPE_DATE = 'date'; + + /** + * Defines the field type as datetime. + */ + const DATETIME_TYPE_DATETIME = 'datetime'; + + /** * {@inheritdoc} */ public function getPropertyDefinitions() { @@ -85,8 +106,8 @@ public function settingsForm(array $form, array &$form_state, $has_data) { '#description' => t('Choose the type of date to create.'), '#default_value' => $this->getFieldSetting('datetime_type'), '#options' => array( - 'datetime' => t('Date and time'), - 'date' => t('Date only'), + static::DATETIME_TYPE_DATETIME => t('Date and time'), + static::DATETIME_TYPE_DATE => t('Date only'), ), ); @@ -104,7 +125,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { '#title' => t('Default date'), '#description' => t('Set a default value for this date.'), '#default_value' => $this->getFieldSetting('default_value'), - '#options' => array('blank' => t('No default value'), 'now' => t('The current date')), + '#options' => array(static::DEFAULT_VALUE_BLANK => t('No default value'), static::DEFAULT_VALUE_NOW => t('The current date')), '#weight' => 1, ); diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php index b208e36..224b178 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php @@ -6,16 +6,11 @@ namespace Drupal\datetime\Plugin\field\widget; +use Drupal\datetime\Plugin\field\field_type\DateTimeItem; use Drupal\field\Annotation\FieldWidget; 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\Core\Entity\Field\FieldDefinitionInterface; use Drupal\Core\Entity\Field\FieldInterface; -use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstanceInterface; -use Drupal\Core\Datetime\DrupalDateTime; use Drupal\datetime\DateHelper; /** @@ -39,29 +34,6 @@ class DateTimeDatelistWidget extends WidgetBase { /** * {@inheritdoc} */ - public function __construct($plugin_id, array $plugin_definition, FieldDefinitionInterface $field_definition, array $settings) { - // Identify the function used to set the default value. - // @todo Make this work for both configurable and nonconfigurable fields: - // https://drupal.org/node/1989468. - if ($field_definition instanceof FieldInstanceInterface) { - $field_definition->default_value_function = $this->defaultValueFunction(); - } - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings); - } - - /** - * Returns 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'; - } - - /** - * {@inheritdoc} - */ public function formElement(FieldInterface $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $date_order = $this->getSetting('date_order'); $time_type = $this->getSetting('time_type'); @@ -80,7 +52,7 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang // Identify the type of date and time elements to use. switch ($this->getFieldSetting('datetime_type')) { - case 'date': + case DateTimeItem::DATETIME_TYPE_DATE: $storage_format = DATETIME_DATE_STORAGE_FORMAT; $type_type = 'none'; break; diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php index da550c7..63fa0e4 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php @@ -6,16 +6,12 @@ namespace Drupal\datetime\Plugin\field\widget; +use Drupal\datetime\Plugin\field\field_type\DateTimeItem; use Drupal\field\Annotation\FieldWidget; 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\Core\Entity\Field\FieldDefinitionInterface; use Drupal\Core\Entity\Field\FieldInterface; -use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstanceInterface; -use Drupal\Core\Datetime\DrupalDateTime; /** * Plugin implementation of the 'datetime_default' widget. @@ -41,12 +37,6 @@ class DateTimeDefaultWidget extends WidgetBase { * {@inheritdoc} */ public function __construct($plugin_id, array $plugin_definition, FieldDefinitionInterface $field_definition, array $settings) { - // Identify the function used to set the default value. - // @todo Make this work for both configurable and nonconfigurable fields: - // https://drupal.org/node/1989468. - if ($field_definition instanceof FieldInstanceInterface) { - $field_definition->default_value_function = $this->defaultValueFunction(); - } parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings); // @todo Inject this once https://drupal.org/node/2035317 is in. @@ -54,16 +44,6 @@ public function __construct($plugin_id, array $plugin_definition, FieldDefinitio } /** - * 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'; - } - - /** * {@inheritdoc} */ public function formElement(FieldInterface $items, $delta, array $element, $langcode, array &$form, array &$form_state) { @@ -81,7 +61,7 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang // Identify the type of date and time elements to use. switch ($this->getFieldSetting('datetime_type')) { - case 'date': + case DateTimeItem::DATETIME_TYPE_DATE: $date_type = 'date'; $time_type = 'none'; $date_format = $this->dateStorage->load('html_date')->getPattern($format_type); @@ -124,7 +104,7 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang // 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 ($this->getFieldSetting('datetime_type') == 'date') { + if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) { // A date without time will pick up the current time, use the default // time. datetime_date_default_time($date); diff --git a/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php b/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php index 09d4380..3ec7b53 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php +++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php @@ -8,6 +8,7 @@ namespace Drupal\datetime\Tests; use Drupal\Core\Language\Language; +use Drupal\datetime\Plugin\field\field_type\DateTimeItem; use Drupal\simpletest\WebTestBase; use Drupal\Core\Datetime\DrupalDateTime; @@ -303,8 +304,7 @@ function testDefaultValue() { $field_name = $this->field->name; // Set the default value to 'now'. - $this->instance->settings['default_value'] = 'now'; - $this->instance->default_value_function = 'datetime_default_value'; + $this->instance->settings['default_value'] = DateTimeItem::DEFAULT_VALUE_NOW; $this->instance->save(); // Display creation form.