summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcatch2014-03-30 10:56:53 (GMT)
committercatch2014-03-30 10:56:53 (GMT)
commitcf0549dbd2191b753e1d6701e41929dafe3ac294 (patch)
tree1babacf9f678f7bd5f4aea2002e67eb3a2f7c1db
parent35a33d68fa35509e4791c2c295fe2f991cd789b2 (diff)
Issue #2177799 by tstoeckler, jsbalsera, mauzeh: Allow IntegerItem's to be unsigned.
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php4
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php49
-rw-r--r--core/modules/aggregator/lib/Drupal/aggregator/Entity/Feed.php6
-rw-r--r--core/modules/aggregator/lib/Drupal/aggregator/Entity/Item.php3
-rw-r--r--core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php6
-rw-r--r--core/modules/comment/lib/Drupal/comment/Entity/Comment.php3
-rw-r--r--core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php2
-rw-r--r--core/modules/file/lib/Drupal/file/Entity/File.php6
-rw-r--r--core/modules/node/lib/Drupal/node/Entity/Node.php6
-rw-r--r--core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php3
-rw-r--r--core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php6
-rw-r--r--core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php3
-rw-r--r--core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMulRev.php3
-rw-r--r--core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestRev.php3
-rw-r--r--core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php6
-rw-r--r--core/modules/user/lib/Drupal/user/Entity/User.php3
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 a56b406..4ad8849 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 f22e060..760a225 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php
@@ -26,6 +26,27 @@ 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}
+ */
public static function propertyDefinitions(FieldDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('integer')
->setLabel(t('Integer value'));
@@ -36,12 +57,40 @@ class IntegerItem extends NumericItemBase {
/**
* {@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}
+ */
public static function schema(FieldDefinitionInterface $field_definition) {
return array(
'columns' => array(
'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 b284823..ff93c30 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 19b2bbf..f12ed40 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 68414d3..4ad34d2 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 69adba8..82a3dab 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 550f3d7..e681856 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 92e1e90..f6d9583 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 6dada8f..0e1d20f 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 eaf7079..9723a83 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 118abe9..1346c7f 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php
@@ -105,6 +105,12 @@ class EntityValidationTest extends EntityUnitTestBase {
// 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();
$this->assertEqual($violations->count(), 1, 'Validation failed.');
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 55a1611..fb1087f 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 870af93..1e6dc44 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 b6b660e..8be477a 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 4550acb..207f08b 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 3c13a94..fc33baf 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'))