From e6a0d659f1250d0412a8fd358a0d2e0e8e9b51a4 Mon Sep 17 00:00:00 2001 From: "Frederic G. MARAND" Date: Sun, 28 Sep 2014 15:16:56 +0200 Subject: [PATCH] Issue #2318605 by fago: Updated default value implementations for uuid et al. --- .../lib/Drupal/Core/Field/Annotation/FieldType.php | 13 +++++++++ core/lib/Drupal/Core/Field/BaseFieldDefinition.php | 9 ++++++ core/lib/Drupal/Core/Field/FieldConfigBase.php | 9 ++++++ .../Field/Plugin/Field/FieldType/ChangedItem.php | 4 ++- .../Field/Plugin/Field/FieldType/CreatedItem.php | 12 ++------ .../Field/Plugin/Field/FieldType/LanguageItem.php | 14 ++------- .../Plugin/Field/FieldType/LanguageItemList.php | 29 +++++++++++++++++++ .../Field/Plugin/Field/FieldType/TimestampItem.php | 1 + .../Plugin/Field/FieldType/TimestampItemList.php | 29 +++++++++++++++++++ .../Core/Field/Plugin/Field/FieldType/UuidItem.php | 14 ++------- .../Field/Plugin/Field/FieldType/UuidItemList.php | 30 ++++++++++++++++++++ .../src/Tests/d6/MigrateAggregatorFeedTest.php | 2 +- core/modules/user/src/Tests/UserBlocksTest.php | 13 +++++++-- 13 files changed, 142 insertions(+), 37 deletions(-) create mode 100644 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItemList.php create mode 100644 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItemList.php create mode 100644 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItemList.php diff --git a/core/lib/Drupal/Core/Field/Annotation/FieldType.php b/core/lib/Drupal/Core/Field/Annotation/FieldType.php index d69f4a6..85ed6e9 100644 --- a/core/lib/Drupal/Core/Field/Annotation/FieldType.php +++ b/core/lib/Drupal/Core/Field/Annotation/FieldType.php @@ -87,4 +87,17 @@ class FieldType extends DataType { */ public $list_class; + /** + * The default value for fields of this type. + * + * This value can be overridden when creating individual field definitions + * using this type. + * + * Note that the provided value can be processed by the field type's item list + * class via \Drupal\Core\Field\FieldItemListInterface::processDefaultValue(). + * + * @var mixed + */ + public $default_value; + } diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index 613f88f..55ee9e7 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -387,6 +387,15 @@ public function getDefaultValue(ContentEntityInterface $entity) { else { $value = isset($this->definition['default_value']) ? $this->definition['default_value'] : NULL; } + // If no default value has been specified, add in the default defined by the + // field type. + if (!isset($value)) { + $definition = \Drupal::service('plugin.manager.field.field_type') + ->getDefinition($this->getType()); + if (isset($definition['default_value'])) { + $value = $definition['default_value']; + } + } // Allow the field type to process default values. $field_item_list_class = $this->getClass(); return $field_item_list_class::processDefaultValue($value, $entity, $this); diff --git a/core/lib/Drupal/Core/Field/FieldConfigBase.php b/core/lib/Drupal/Core/Field/FieldConfigBase.php index e0764cd..023389c 100644 --- a/core/lib/Drupal/Core/Field/FieldConfigBase.php +++ b/core/lib/Drupal/Core/Field/FieldConfigBase.php @@ -340,6 +340,15 @@ public function getDefaultValue(ContentEntityInterface $entity) { else { $value = $this->default_value; } + // If no default value has been specified, add in the default defined by the + // field type. + if (!isset($value)) { + $definition = \Drupal::service('plugin.manager.field.field_type') + ->getDefinition($this->getType()); + if (isset($definition['default_value'])) { + $value = $definition['default_value']; + } + } // Allow the field type to process default values. $field_item_list_class = $this->getClass(); return $field_item_list_class::processDefaultValue($value, $entity, $this); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php index b661b3e..8c3ff83 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php @@ -15,9 +15,11 @@ * label = @Translation("Last changed"), * description = @Translation("An entity field containing a UNIX timestamp of when the entity has been last updated."), * no_ui = TRUE, + * list_class = "\Drupal\Core\Field\Plugin\Field\FieldType\TimestampItemList", * constraints = { * "ComplexData" = {"value" = {"EntityChanged" = {}}} - * } + * }, + * default_value = "NOW" * ) */ class ChangedItem extends CreatedItem { diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php index 367a1bc..a485f42 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php @@ -15,19 +15,11 @@ * label = @Translation("Created"), * description = @Translation("An entity field containing a UNIX timestamp of when the entity has been created."), * no_ui = TRUE, + * list_class = "\Drupal\Core\Field\Plugin\Field\FieldType\TimestampItemList", * default_formatter = "timestamp", + * default_value = "NOW" * ) */ class CreatedItem extends TimestampItem { - /** - * {@inheritdoc} - */ - public function applyDefaultValue($notify = TRUE) { - parent::applyDefaultValue($notify); - // Created fields default to the current timestamp. - $this->setValue(array('value' => REQUEST_TIME), $notify); - return $this; - } - } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php index 961eb67..3e3fbdb 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php @@ -21,11 +21,13 @@ * label = @Translation("Language"), * description = @Translation("An entity field referencing a language."), * no_ui = TRUE, + * list_class = "\Drupal\Core\Field\Plugin\Field\FieldType\LanguageItemList", * constraints = { * "ComplexData" = { * "value" = {"Length" = {"max" = 12}} * } - * } + * }, + * default_value = "DEFAULT_LANGUAGE" * ) */ class LanguageItem extends FieldItemBase { @@ -90,16 +92,6 @@ public function setValue($values, $notify = TRUE) { /** * {@inheritdoc} */ - public function applyDefaultValue($notify = TRUE) { - // Default to the site's default language. When language module is enabled, - // this behavior is configurable, see language_field_info_alter(). - $this->setValue(array('value' => \Drupal::languageManager()->getDefaultLanguage()->getId()), $notify); - return $this; - } - - /** - * {@inheritdoc} - */ public function onChange($property_name) { // Make sure that the value and the language property stay in sync. if ($property_name == 'value') { diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItemList.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItemList.php new file mode 100644 index 0000000..14b1f6e --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItemList.php @@ -0,0 +1,29 @@ +getDefaultLanguage()->getId(); + } + return parent::processDefaultValue($default_value, $entity, $definition); + } + +} diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php index d355287..4d81b38 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php @@ -19,6 +19,7 @@ * label = @Translation("Timestamp"), * description = @Translation("An entity field containing a UNIX timestamp value."), * no_ui = TRUE, + * list_class = "\Drupal\Core\Field\Plugin\Field\FieldType\TimestampItemList", * default_formatter = "timestamp", * constraints = { * "ComplexData" = { diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItemList.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItemList.php new file mode 100644 index 0000000..9a4983e --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItemList.php @@ -0,0 +1,29 @@ +setValue(array('value' => $uuid->generate()), $notify); - return $this; - } - - /** - * {@inheritdoc} - */ public static function schema(FieldStorageDefinitionInterface $field_definition) { $schema = parent::schema($field_definition); $schema['unique keys']['value'] = array('value'); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItemList.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItemList.php new file mode 100644 index 0000000..ae4acb8 --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UuidItemList.php @@ -0,0 +1,30 @@ +generate(); + } + return parent::processDefaultValue($default_value, $entity, $definition); + } + +} diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php index 66bc81f..9c9740f 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php @@ -46,7 +46,7 @@ public function testAggregatorFeedImport() { $this->assertEqual($feed->url->value, 'http://knowyourmeme.com/newsfeed.rss'); $this->assertEqual($feed->refresh->value, 900); $this->assertEqual($feed->checked->value, 1387659487); - $this->assertEqual($feed->queued->value, 0); + $this->assertEqual($feed->queued->value, REQUEST_TIME); $this->assertEqual($feed->link->value, 'http://knowyourmeme.com'); $this->assertEqual($feed->description->value, 'New items added to the News Feed'); $this->assertEqual($feed->image->value, 'http://b.thumbs.redditmedia.com/harEHsUUZVajabtC.png'); diff --git a/core/modules/user/src/Tests/UserBlocksTest.php b/core/modules/user/src/Tests/UserBlocksTest.php index 48c0e1a..21e3ea7 100644 --- a/core/modules/user/src/Tests/UserBlocksTest.php +++ b/core/modules/user/src/Tests/UserBlocksTest.php @@ -8,6 +8,7 @@ namespace Drupal\user\Tests; use Drupal\simpletest\WebTestBase; +use Drupal\user\Entity\User; /** * Tests user blocks. @@ -76,7 +77,10 @@ function testUserLoginBlock() { function testWhosOnlineBlock() { $block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block'); - // Generate users. + // Get the install user. + $install_user = User::load(1); + + // Generate other users. $user1 = $this->drupalCreateUser(array()); $user2 = $this->drupalCreateUser(array()); $user3 = $this->drupalCreateUser(array()); @@ -86,18 +90,21 @@ function testWhosOnlineBlock() { $this->updateAccess($user2->id(), REQUEST_TIME + 1); // Insert an inactive user who should not be seen in the block, and ensure - // that the admin user used in setUp() does not appear. + // that the admin user used in setUp() does not appear, nor the install user. $inactive_time = REQUEST_TIME - (15 * 60) - 1; $this->updateAccess($user3->id(), $inactive_time); $this->updateAccess($this->adminUser->id(), $inactive_time); + $this->updateAccess($install_user->id(), $inactive_time); // Test block output. $content = entity_view($block, 'block'); - $this->drupalSetContent(render($content)); + $this->setRawContent(render($content)); $this->assertRaw(t('2 users'), 'Correct number of online users (2 users).'); $this->assertText($user1->getUsername(), 'Active user 1 found in online list.'); $this->assertText($user2->getUsername(), 'Active user 2 found in online list.'); $this->assertNoText($user3->getUsername(), 'Inactive user not found in online list.'); + $this->assertNoText($this->adminUser->getUsername(), 'Admin user not found in online list.'); + $this->assertNoText($install_user->getUsername(), 'Install user not found in online list.'); $this->assertTrue(strpos($this->drupalGetContent(), $user1->getUsername()) > strpos($this->drupalGetContent(), $user2->getUsername()), 'Online users are ordered correctly.'); } -- 1.7.9.5