From cf0549dbd2191b753e1d6701e41929dafe3ac294 Mon Sep 17 00:00:00 2001 From: catch Date: Sun, 30 Mar 2014 12:56:53 +0200 Subject: [PATCH] Issue #2177799 by tstoeckler, jsbalsera, mauzeh: Allow IntegerItem's to be unsigned. --- .../Field/FieldType/EntityReferenceItem.php | 4 +- .../Plugin/Field/FieldType/IntegerItem.php | 49 +++++++++++++++++++ .../lib/Drupal/aggregator/Entity/Feed.php | 6 ++- .../lib/Drupal/aggregator/Entity/Item.php | 3 +- .../custom_block/Entity/CustomBlock.php | 6 ++- .../lib/Drupal/comment/Entity/Comment.php | 3 +- .../ConfigurableEntityReferenceItem.php | 2 +- .../file/lib/Drupal/file/Entity/File.php | 6 ++- .../node/lib/Drupal/node/Entity/Node.php | 6 ++- .../lib/Drupal/shortcut/Entity/Shortcut.php | 3 +- .../Tests/Entity/EntityValidationTest.php | 6 +++ .../Drupal/entity_test/Entity/EntityTest.php | 3 +- .../entity_test/Entity/EntityTestMulRev.php | 3 +- .../entity_test/Entity/EntityTestRev.php | 3 +- .../lib/Drupal/taxonomy/Entity/Term.php | 6 ++- .../user/lib/Drupal/user/Entity/User.php | 3 +- 16 files changed, 91 insertions(+), 21 deletions(-) diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index a56b406f766..4ad88496f5a 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -63,9 +63,7 @@ class EntityReferenceItem extends FieldItemBase { // https://drupal.org/node/2107249 $target_id_definition = DataDefinition::create('integer') ->setLabel(t('Entity ID')) - ->setConstraints(array( - 'Range' => array('min' => 0), - )); + ->setSetting('unsigned', TRUE); } else { $target_id_definition = DataDefinition::create('string') diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php index f22e06040e1..760a225adbc 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php @@ -23,6 +23,27 @@ use Drupal\Core\TypedData\DataDefinition; */ class IntegerItem extends NumericItemBase { + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return array( + 'unsigned' => FALSE, + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public static function defaultInstanceSettings() { + return array( + 'min' => '', + 'max' => '', + 'prefix' => '', + 'suffix' => '', + ) + parent::defaultInstanceSettings(); + } + /** * {@inheritdoc} */ @@ -33,6 +54,32 @@ class IntegerItem extends NumericItemBase { return $properties; } + /** + * {@inheritdoc} + */ + public function getConstraints() { + $constraints = parent::getConstraints(); + + // If this is an unsigned integer, add a validation constraint for the + // integer to be positive. + if ($this->getSetting('unsigned')) { + $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager(); + $constraints[] = $constraint_manager->create('ComplexData', array( + 'value' => array( + 'Range' => array( + 'min' => 0, + 'minMessage' => t('%name: The integer must be larger or equal to %min.', array( + '%name' => $this->getFieldDefinition()->getLabel(), + '%min' => 0, + )), + ), + ), + )); + } + + return $constraints; + } + /** * {@inheritdoc} */ @@ -42,6 +89,8 @@ class IntegerItem extends NumericItemBase { 'value' => array( 'type' => 'int', 'not null' => FALSE, + // Expose the 'unsigned' setting in the field item schema. + 'unsigned' => $field_definition->getSetting('unsigned'), ), ), ); diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php index b284823c8d2..ff93c30d72f 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php @@ -134,7 +134,8 @@ class Feed extends ContentEntityBase implements FeedInterface { $fields['fid'] = FieldDefinition::create('integer') ->setLabel(t('Feed ID')) ->setDescription(t('The ID of the aggregator feed.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) @@ -155,7 +156,8 @@ class Feed extends ContentEntityBase implements FeedInterface { $fields['refresh'] = FieldDefinition::create('integer') ->setLabel(t('Refresh')) - ->setDescription(t('How often to check for new feed items, in seconds.')); + ->setDescription(t('How often to check for new feed items, in seconds.')) + ->setSetting('unsigned', TRUE); $fields['checked'] = FieldDefinition::create('timestamp') ->setLabel(t('Checked')) diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php index 19b2bbf7e04..f12ed40839e 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php @@ -54,7 +54,8 @@ class Item extends ContentEntityBase implements ItemInterface { $fields['iid'] = FieldDefinition::create('integer') ->setLabel(t('Aggregator item ID')) ->setDescription(t('The ID of the feed item.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['fid'] = FieldDefinition::create('entity_reference') ->setLabel(t('Aggregator feed ID')) diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php index 68414d371ad..4ad34d277ad 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php @@ -158,7 +158,8 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface { $fields['id'] = FieldDefinition::create('integer') ->setLabel(t('Custom block ID')) ->setDescription(t('The custom block ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) @@ -168,7 +169,8 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface { $fields['revision_id'] = FieldDefinition::create('integer') ->setLabel(t('Revision ID')) ->setDescription(t('The revision ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['langcode'] = FieldDefinition::create('language') ->setLabel(t('Language code')) diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index 69adba843e7..82a3dabd9ab 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -210,7 +210,8 @@ class Comment extends ContentEntityBase implements CommentInterface { $fields['cid'] = FieldDefinition::create('integer') ->setLabel(t('Comment ID')) ->setDescription(t('The comment ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php index 550f3d7c6bb..e6818561f9e 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php @@ -110,7 +110,7 @@ class ConfigurableEntityReferenceItem extends EntityReferenceItem implements All if ($target_type_info->hasKey('revision') && $target_type_info->getRevisionTable()) { $properties['revision_id'] = DataDefinition::create('integer') ->setLabel(t('Revision ID')) - ->setConstraints(array('Range' => array('min' => 0))); + ->setSetting('unsigned', TRUE); } return $properties; diff --git a/core/modules/file/lib/Drupal/file/Entity/File.php b/core/modules/file/lib/Drupal/file/Entity/File.php index 92e1e905bcc..f6d9583c1f2 100644 --- a/core/modules/file/lib/Drupal/file/Entity/File.php +++ b/core/modules/file/lib/Drupal/file/Entity/File.php @@ -233,7 +233,8 @@ class File extends ContentEntityBase implements FileInterface { $fields['fid'] = FieldDefinition::create('integer') ->setLabel(t('File ID')) ->setDescription(t('The file ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) @@ -263,7 +264,8 @@ class File extends ContentEntityBase implements FileInterface { $fields['filesize'] = FieldDefinition::create('integer') ->setLabel(t('File size')) - ->setDescription(t('The size of the file in bytes.')); + ->setDescription(t('The size of the file in bytes.')) + ->setSetting('unsigned', TRUE); $fields['status'] = FieldDefinition::create('integer') ->setLabel(t('Status')) diff --git a/core/modules/node/lib/Drupal/node/Entity/Node.php b/core/modules/node/lib/Drupal/node/Entity/Node.php index 6dada8f79f9..0e1d20fbedd 100644 --- a/core/modules/node/lib/Drupal/node/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Entity/Node.php @@ -335,7 +335,8 @@ class Node extends ContentEntityBase implements NodeInterface { $fields['nid'] = FieldDefinition::create('integer') ->setLabel(t('Node ID')) ->setDescription(t('The node ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) @@ -345,7 +346,8 @@ class Node extends ContentEntityBase implements NodeInterface { $fields['vid'] = FieldDefinition::create('integer') ->setLabel(t('Revision ID')) ->setDescription(t('The node revision ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['type'] = FieldDefinition::create('entity_reference') ->setLabel(t('Type')) diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php index eaf707902d2..9723a835e2d 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php @@ -136,7 +136,8 @@ class Shortcut extends ContentEntityBase implements ShortcutInterface { $fields['id'] = FieldDefinition::create('integer') ->setLabel(t('ID')) ->setDescription(t('The ID of the shortcut.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php index 118abe9db42..1346c7f716a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php @@ -104,6 +104,12 @@ class EntityValidationTest extends EntityUnitTestBase { $this->assertEqual($violations->count(), 0, 'Validation passes.'); // Test triggering a fail for each of the constraints specified. + $test_entity = clone $entity; + $test_entity->id->value = -1; + $violations = $test_entity->validate(); + $this->assertEqual($violations->count(), 1, 'Validation failed.'); + $this->assertEqual($violations[0]->getMessage(), t('%name: The integer must be larger or equal to %min.', array('%name' => 'ID', '%min' => 0))); + $test_entity = clone $entity; $test_entity->uuid->value = $this->randomString(129); $violations = $test_entity->validate(); diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php index 55a16113eec..fb1087ff61d 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php @@ -65,7 +65,8 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface { $fields['id'] = FieldDefinition::create('integer') ->setLabel(t('ID')) ->setDescription(t('The ID of the test entity.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMulRev.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMulRev.php index 870af93d6fe..1e6dc44a8a4 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMulRev.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMulRev.php @@ -53,7 +53,8 @@ class EntityTestMulRev extends EntityTestRev { $fields['revision_id'] = FieldDefinition::create('integer') ->setLabel(t('Revision ID')) ->setDescription(t('The version id of the test entity.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['default_langcode'] = FieldDefinition::create('boolean') ->setLabel(t('Default language')) diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestRev.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestRev.php index b6b660ea24c..8be477a5076 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestRev.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestRev.php @@ -58,7 +58,8 @@ class EntityTestRev extends EntityTest { $fields['revision_id'] = FieldDefinition::create('integer') ->setLabel(t('Revision ID')) ->setDescription(t('The version id of the test entity.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['langcode']->setRevisionable(TRUE); $fields['name']->setRevisionable(TRUE); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php index 4550acb5309..207f08b824c 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php @@ -111,7 +111,8 @@ class Term extends ContentEntityBase implements TermInterface { $fields['tid'] = FieldDefinition::create('integer') ->setLabel(t('Term ID')) ->setDescription(t('The term ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) @@ -150,7 +151,8 @@ class Term extends ContentEntityBase implements TermInterface { ->setDescription(t('The parents of this term.')) // Save new terms with no parents by default. ->setSetting('default_value', 0) - ->setConstraints(array('TermParent' => array())); + ->setSetting('unsigned', TRUE) + ->addConstraint('TermParent', array()); $fields['changed'] = FieldDefinition::create('changed') ->setLabel(t('Changed')) diff --git a/core/modules/user/lib/Drupal/user/Entity/User.php b/core/modules/user/lib/Drupal/user/Entity/User.php index 3c13a947236..fc33baf48a5 100644 --- a/core/modules/user/lib/Drupal/user/Entity/User.php +++ b/core/modules/user/lib/Drupal/user/Entity/User.php @@ -448,7 +448,8 @@ class User extends ContentEntityBase implements UserInterface { $fields['uid'] = FieldDefinition::create('integer') ->setLabel(t('User ID')) ->setDescription(t('The user ID.')) - ->setReadOnly(TRUE); + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID'))