diff --git a/core/lib/Drupal/Core/Field/FieldDefinition.php b/core/lib/Drupal/Core/Field/FieldDefinition.php index 67a9cc3..9e88c57 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinition.php +++ b/core/lib/Drupal/Core/Field/FieldDefinition.php @@ -339,7 +339,7 @@ public function offsetSet($offset, $value) { public function getSchema() { if (!isset($this->schema)) { // Get the schema from the field item class. - $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->type); + $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getFieldType()); $class = $definition['class']; $schema = $class::schema($this); // Fill in default values for optional entries. @@ -374,4 +374,13 @@ public function getColumns() { return $schema['columns']; } + /** + * A list of columns that can not be used as field type columns. + * + * @return array + */ + public static function getReservedColumns() { + return array('deleted'); + } + } diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index 857c056..4645936 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -39,16 +39,6 @@ public function __construct($definition, $name = NULL, TypedDataInterface $paren /** * {@inheritdoc} */ - public static function schema(FieldDefinitionInterface $field_definition) { - // @todo Remove when all field types will declare their schema. - return array( - 'columns' => array(), - ); - } - - /** - * {@inheritdoc} - */ public function getEntity() { return $this->getParent()->getEntity(); } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php index fe7ba96..caf1d24 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/BooleanItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -43,4 +44,21 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + ), + ), + ); + } + } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DateItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DateItem.php index 59984c8..8c5a82c 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DateItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DateItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -43,4 +44,20 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ), + ); + } + } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php index ce0817c..df510bd 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -22,6 +23,18 @@ class EmailItem extends FieldItemBase { /** + * Defines the max length for an email address + * + * The maximum length of an e-mail address is 254 characters. RFC 3696 + * specifies a total length of 320 characters, but mentions that + * addresses longer than 256 characters are not normally useful. Erratum + * 1690 was then released which corrected this value to 254 characters. + * @see http://tools.ietf.org/html/rfc3696#section-3 + * @see http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690 + */ + const EMAIL_MAX_LENGTH = 254; + + /** * Definitions of the contained properties. * * @see EmailItem::getPropertyDefinitions() @@ -44,6 +57,39 @@ public function getPropertyDefinitions() { return static::$propertyDefinitions; } + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'varchar', + 'length' => static::EMAIL_MAX_LENGTH, + 'not null' => FALSE, + ), + ), + ); + } + + /** + * {@inheritdoc} + */ + public function getConstraints() { + $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); + $constraints = parent::getConstraints(); + + $constraints[] = $constraint_manager->create('ComplexData', array( + 'value' => array( + 'Length' => array( + 'max' => static::EMAIL_MAX_LENGTH, + 'maxMessage' => t('%name: the e-mail address can not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getFieldLabel(), '@max' => static::EMAIL_MAX_LENGTH)), + ) + ), + )); + + return $constraints; + } /** * {@inheritdoc} 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 61f624f..1617504 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -88,6 +89,43 @@ public function getPropertyDefinitions() { /** * {@inheritdoc} */ + public static function schema(FieldDefinitionInterface $field_definition) { + $target_type = $field_definition->getFieldSetting('target_type'); + $target_type_info = \Drupal::entityManager()->getDefinition($target_type); + + if (is_subclass_of($target_type_info['class'], '\Drupal\Core\Entity\ContentEntityInterface')) { + $columns = array( + 'target_id' => array( + 'description' => 'The ID of the target entity.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + ); + } + else { + $columns = array( + 'target_id' => array( + 'description' => 'The ID of the target entity.', + 'type' => 'varchar', + 'length' => '255', + ), + ); + } + + $schema = array( + 'columns' => $columns, + 'indexes' => array( + 'target_id' => array('target_id'), + ), + ); + + return $schema; + } + + /** + * {@inheritdoc} + */ public function __get($name) { $name = ($name == 'value') ? 'target_id' : $name; return parent::__get($name); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php index ccfd3c5..27a6001 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/FloatItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -43,4 +44,19 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'float', + 'not null' => FALSE, + ), + ), + ); + } + } 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 82f088a..1e084dc 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/IntegerItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; @@ -42,4 +43,20 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ), + ); + } + } 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 3ed6127..89ce29d 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Language\Language; @@ -60,6 +61,22 @@ public function getPropertyDefinitions() { /** * {@inheritdoc} */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ), + ), + ); + } + + /** + * {@inheritdoc} + */ public function setValue($values, $notify = TRUE) { // Treat the values as property value of the language property, if no array // is given as this handles language codes and objects. diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php index 9c63b33..43dfaf2 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; @@ -17,6 +18,9 @@ * id = "string", * label = @Translation("String"), * description = @Translation("An entity field containing a string value."), + * settings = { + * "max_length" = "255" + * }, * configurable = FALSE * ) */ @@ -42,4 +46,20 @@ public function getPropertyDefinitions() { } return static::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'varchar', + 'length' => $field_definition->getFieldSetting('max_length'), + 'not null' => FALSE, + ), + ), + ); + } + } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php index 23f9701..d920b2d 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/UriItem.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -43,4 +44,19 @@ public function getPropertyDefinitions() { } return self::$propertyDefinitions; } + + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'text', + 'not null' => FALSE, + ), + ), + ); + } + } diff --git a/core/modules/email/lib/Drupal/email/ConfigurableEmailItem.php b/core/modules/email/lib/Drupal/email/ConfigurableEmailItem.php index c67163e..65e08da 100644 --- a/core/modules/email/lib/Drupal/email/ConfigurableEmailItem.php +++ b/core/modules/email/lib/Drupal/email/ConfigurableEmailItem.php @@ -19,52 +19,6 @@ class ConfigurableEmailItem extends EmailItem implements ConfigFieldItemInterface { /** - * Defines the max length for an email address - * - * The maximum length of an e-mail address is 254 characters. RFC 3696 - * specifies a total length of 320 characters, but mentions that - * addresses longer than 256 characters are not normally useful. Erratum - * 1690 was then released which corrected this value to 254 characters. - * @see http://tools.ietf.org/html/rfc3696#section-3 - * @see http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690 - */ - const EMAIL_MAX_LENGTH = 254; - - /** - * {@inheritdoc} - */ - public static function schema(FieldDefinitionInterface $field_definition) { - return array( - 'columns' => array( - 'value' => array( - 'type' => 'varchar', - 'length' => static::EMAIL_MAX_LENGTH, - 'not null' => FALSE, - ), - ), - ); - } - - /** - * {@inheritdoc} - */ - public function getConstraints() { - $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); - $constraints = parent::getConstraints(); - - $constraints[] = $constraint_manager->create('ComplexData', array( - 'value' => array( - 'Length' => array( - 'max' => static::EMAIL_MAX_LENGTH, - 'maxMessage' => t('%name: the e-mail address can not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getFieldLabel(), '@max' => static::EMAIL_MAX_LENGTH)), - ) - ), - )); - - return $constraints; - } - - /** * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state, $has_data) { 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 dc1bac6..09f6b4d 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/ConfigurableEntityReferenceItem.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\ConfigEntityReferenceItemBase; use Drupal\Core\Field\ConfigFieldItemInterface; +use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; /** * Alternative plugin implementation of the 'entity_reference' field type. @@ -26,42 +27,20 @@ class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase impl * {@inheritdoc} */ public static function schema(FieldDefinitionInterface $field_definition) { + $schema = EntityReferenceItem::schema($field_definition); + $target_type = $field_definition->getFieldSetting('target_type'); $target_type_info = \Drupal::entityManager()->getDefinition($target_type); if (is_subclass_of($target_type_info['class'], '\Drupal\Core\Entity\ContentEntityInterface')) { - $columns = array( - 'target_id' => array( - 'description' => 'The ID of the target entity.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - ), - 'revision_id' => array( - 'description' => 'The revision ID of the target entity.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - ), - ); - } - else { - $columns = array( - 'target_id' => array( - 'description' => 'The ID of the target entity.', - 'type' => 'varchar', - 'length' => '255', - ), + $schema['columns']['revision_id'] = array( + 'description' => 'The revision ID of the target entity.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, ); } - $schema = array( - 'columns' => $columns, - 'indexes' => array( - 'target_id' => array('target_id'), - ), - ); - return $schema; } diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index 7e34d3d..1c01abd 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -691,4 +691,19 @@ public function getItemDefinition() { } return $this->itemDefinition; } + + /** + * {@inheritdoc} + */ + public function getSchema() { + return $this->field->getSchema(); + } + + /** + * {@inheritdoc} + */ + public function getColumns() { + return $this->field->getColumns(); + } + } diff --git a/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php b/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php index 1b0ce50..2fa23d7 100644 --- a/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php @@ -7,6 +7,7 @@ namespace Drupal\path\Plugin\Field\FieldType; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemBase; /** @@ -47,4 +48,11 @@ public function getPropertyDefinitions() { return static::$propertyDefinitions; } + /** + * {@inheritdoc} + */ + public static function schema(FieldDefinitionInterface $field_definition) { + return array(); + } + }