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..34ff98e --- /dev/null +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/field_type/DateTimeField.php @@ -0,0 +1,86 @@ + array('default_value_input'), + 'default_date' => array( + '#type' => 'select', + '#title' => t('Default date'), + '#description' => t('Set a default value for this date.'), + '#default_value' => isset($default_value['default_date']) ? $default_value['default_date'] : static::DEFAULT_VALUE_BLANK, + '#options' => array(static::DEFAULT_VALUE_BLANK => t('No default value'), static::DEFAULT_VALUE_NOW => t('The current date')), + ) + ); + + return $element; + } + + /** + * {@inheritdoc} + */ + public function defaultValuesFormSubmit(array $element, array &$form, array &$form_state) { + return $form_state['values']['default_value_input']; + } + + /** + * {@inheritdoc} + */ + public function getDefaultValue() { + $default_value = parent::getDefaultValue(); + + if ($default_value) { + $value = ''; + $date = ''; + if ($default_value['default_date'] == static::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('datetime_type') == 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. + $default_value = array( + array( + 'value' => $value, + 'date' => $date, + ) + ); + } + return $default_value; + } + +} 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..e38a575 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 @@ -24,16 +24,24 @@ * settings = { * "datetime_type" = "datetime" * }, - * instance_settings = { - * "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 { /** + * Defines the field type as date. + */ + const DATETIME_TYPE_DATE = 'date'; + + /** + * Defines the field type as datetime. + */ + const DATETIME_TYPE_DATETIME = 'datetime'; + + /** * Field definitions of the contained properties. * * @var array @@ -85,8 +93,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'), ), ); @@ -96,24 +104,6 @@ public function settingsForm(array $form, array &$form_state, $has_data) { /** * {@inheritdoc} */ - public function instanceSettingsForm(array $form, array &$form_state) { - $element = array(); - - $element['default_value'] = array( - '#type' => 'select', - '#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')), - '#weight' => 1, - ); - - return $element; - } - - /** - * {@inheritdoc} - */ public function prepareCache() { // The function generates a Date object for each field early so that it is // cached in the field cache. This avoids the need to generate the object 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 37d2c3b..42ba4bb 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,12 +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\Core\Entity\Field\FieldDefinitionInterface; use Drupal\Core\Entity\Field\FieldInterface; -use Drupal\field\FieldInstanceInterface; use Drupal\datetime\DateHelper; /** @@ -35,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, array &$form, array &$form_state) { $date_order = $this->getSetting('date_order'); $time_type = $this->getSetting('time_type'); @@ -76,7 +52,7 @@ public function formElement(FieldInterface $items, $delta, array $element, array // 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 6628dbb..fff5274 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,6 +6,7 @@ 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; @@ -37,12 +38,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. @@ -50,16 +45,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, array &$form, array &$form_state) { @@ -77,7 +62,7 @@ public function formElement(FieldInterface $items, $delta, array $element, array // 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); @@ -120,7 +105,7 @@ public function formElement(FieldInterface $items, $delta, array $element, array // 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 4fd86d9..a5b43ee 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php +++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php @@ -48,9 +48,11 @@ function setUp() { parent::setUp(); $web_user = $this->drupalCreateUser(array( + 'access content', 'view test entity', 'administer entity_test content', 'administer content types', + 'administer node fields', )); $this->drupalLogin($web_user); @@ -66,9 +68,6 @@ function setUp() { 'field_name' => $this->field->name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', - 'settings' => array( - 'default_value' => 'blank', - ), )); $this->instance->save(); @@ -292,40 +291,44 @@ function testDatelistWidget() { * Test default value functionality. */ function testDefaultValue() { + //Create a test content type. + $this->drupalCreateContentType(array('type' => 'date_content')); - // Change the field to a datetime field. - $this->field->settings['datetime_type'] = 'datetime'; + $this->field = entity_create('field_entity', array( + 'name' => drupal_strtolower($this->randomName()), + 'entity_type' => 'node', + 'type' => 'datetime', + 'settings' => array('datetime_type' => 'date'), + )); $this->field->save(); - $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 = entity_create('field_instance', array( + 'field_name' => $this->field->name, + 'entity_type' => 'node', + 'bundle' => 'date_content', + )); $this->instance->save(); - // Display creation form. - $date = new DrupalDateTime(); - $date_format = 'Y-m-d'; - $this->drupalGet('entity_test/add'); + // Set now as default_value. + $instance_edit = array( + 'default_value_input[default_date]' => 'now', + ); + $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $this->field->name, $instance_edit, t('Save settings')); - // See if current date is set. We cannot test for the precise time because - // it may be a few seconds between the time the comparison date is created - // and the form date, so we just test the date and that the time is not - // empty. - $this->assertFieldByName("{$field_name}[0][value][date]", $date->format($date_format), 'Date element found.'); - $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Time element found.'); + // Check that default value is selected in default value form. + $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $this->field->name); + $this->assertRaw('', 'The default value is selected in instance settings page'); - // Set the default value to 'blank'. - $this->instance->settings['default_value'] = 'blank'; - $this->instance->default_value_function = 'datetime_default_value'; - $this->instance->save(); + // Check if default_date has been stored successfully. + $config_entity = $this->container->get('config.factory')->get('field.instance.node.date_content.' . $this->field->name)->get(); + $this->assertEqual($config_entity['default_value']['default_date'], 'now', 'Default value has been stored succesfully'); - // Display creation form. - $this->drupalGet('entity_test/add'); + // Clean field_info cache in order to avoid stale cache values. + field_info_cache_clear(); - // See that no date is set. - $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); - $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.'); + // Create a new node to check that datetime field default value is today. + $new_node = entity_create('node', array('type' => 'date_content')); + $expected_date = new DrupalDateTime('now', DATETIME_STORAGE_TIMEZONE); + $this->assertEqual($new_node->get($this->field->name)->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT)); } /**