Issue #2177799 by tstoeckler, jsbalsera, mauzeh: Allow IntegerItem's to be unsigned.
parent
35a33d68fa
commit
cf0549dbd2
|
@ -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')
|
||||
|
|
|
@ -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'),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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'))
|
||||
|
|
Loading…
Reference in New Issue