Commit 0f8b4e79 authored by alexpott's avatar alexpott

Issue #2023563 by Berdir, smiletrl, fago: Convert entity field types to the field_type plugin.

parent 5b026c38
......@@ -7,7 +7,7 @@
namespace Drupal\Core\Entity\Annotation;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\TypedData\Annotation\DataType;
/**
* Defines a FieldType annotation object.
......@@ -17,7 +17,7 @@
*
* @Annotation
*/
class FieldType extends Plugin {
class FieldType extends DataType {
/**
* The plugin ID.
......@@ -103,8 +103,6 @@ class FieldType extends Plugin {
/**
* A boolean stating that fields of this type are configurable.
*
* @todo: Make field module respect this.
*
* @var boolean
*/
public $configurable = TRUE;
......@@ -112,10 +110,13 @@ class FieldType extends Plugin {
/**
* A boolean stating that fields of this type cannot be created through the UI.
*
* If TRUE, fields of this type can only be created programmatically.
*
* @var boolean
*/
public $no_ui = FALSE;
/**
* {@inheritdoc}
*/
public $list_class;
}
......@@ -7,7 +7,6 @@
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
......
......@@ -471,8 +471,16 @@ public function getFieldDefinitions($entity_type, $bundle = NULL) {
}
else {
$class = $this->factory->getPluginClass($entity_type, $this->getDefinition($entity_type));
$base_definitions = $class::baseFieldDefinitions($entity_type);
foreach ($base_definitions as &$base_definition) {
// Support old-style field types to avoid that all base field
// definitions need to be changed.
// @todo: Remove after https://drupal.org/node/2047229.
$base_definition['type'] = preg_replace('/(.+)_field/', 'field_item:$1', $base_definition['type']);
}
$this->entityFieldInfo[$entity_type] = array(
'definitions' => $class::baseFieldDefinitions($entity_type),
'definitions' => $base_definitions,
// Contains definitions of optional (per-bundle) fields.
'optional' => array(),
// An array keyed by bundle name containing the optional fields added
......
......@@ -25,7 +25,6 @@ class FieldTypePluginManager extends DefaultPluginManager {
protected $defaults = array(
'settings' => array(),
'instance_settings' => array(),
'list_class' => '\Drupal\field\Plugin\Type\FieldType\ConfigFieldItemList',
);
/**
......@@ -51,6 +50,21 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
$this->discovery = new LegacyFieldTypeDiscoveryDecorator($this->discovery, $module_handler);
}
/**
* {@inheritdoc}
*/
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (!isset($definition['list_class'])) {
if ($definition['configurable']) {
$definition['list_class'] = '\Drupal\field\Plugin\Type\FieldType\ConfigFieldItemList';
}
else {
$definition['list_class'] = '\Drupal\Core\Entity\Field\FieldItemList';
}
}
}
/**
* Returns the default field-level settings for a field type.
*
......@@ -81,4 +95,17 @@ public function getDefaultInstanceSettings($type) {
return isset($info['instance_settings']) ? $info['instance_settings'] : array();
}
/**
* Gets the definition of all field types that are configurable.
*
* @return array
* An array of field type definitions.
*/
public function getConfigurableDefinitions() {
$definitions = $this->getDefinitions();
return array_filter($definitions, function ($definition) {
return $definition['configurable'];
});
}
}
......@@ -2,23 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\BooleanItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\BooleanItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'boolean_field' entity field item.
* Defines the 'boolean' entity field type.
*
* @DataType(
* id = "boolean_field",
* label = @Translation("Boolean field item"),
* @FieldType(
* id = "boolean",
* label = @Translation("Boolean"),
* description = @Translation("An entity field containing a boolean value."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList"
* configurable = FALSE
* )
*/
class BooleanItem extends FieldItemBase {
......
......@@ -2,23 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\DateItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\DateItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'date_field' entity field item.
* Defines the 'date' entity field type.
*
* @DataType(
* id = "date_field",
* label = @Translation("Date field item"),
* @FieldType(
* id = "date",
* label = @Translation("Date"),
* description = @Translation("An entity field containing a date value."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList"
* configurable = FALSE
* )
*/
class DateItem extends FieldItemBase {
......
......@@ -2,27 +2,24 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\EmailItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\EmailItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
use Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem;
/**
* Defines the 'email_field' entity field item.
* Defines the 'email' entity field type.
*
* @DataType(
* id = "email_field",
* label = @Translation("E-mail field item"),
* @FieldType(
* id = "email",
* label = @Translation("E-mail"),
* description = @Translation("An entity field containing an e-mail value."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList"
* configurable = FALSE
* )
*/
class EmailItem extends LegacyConfigFieldItem {
class EmailItem extends FieldItemBase {
/**
* Definitions of the contained properties.
......@@ -54,4 +51,5 @@ public function getPropertyDefinitions() {
public function isEmpty() {
return $this->value === NULL || $this->value === '';
}
}
......@@ -2,10 +2,10 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\EntityReferenceItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
......@@ -13,7 +13,7 @@
use Drupal\Core\TypedData\TypedDataInterface;
/**
* Defines the 'entity_reference_item' entity field item.
* Defines the 'entity_reference' entity field type.
*
* Supported settings (below the definition's 'settings' key) are:
* - target_type: The entity type to reference. Required.
......@@ -21,11 +21,11 @@
* may be referenced. May be set to an single bundle, or to an array of
* allowed bundles.
*
* @DataType(
* id = "entity_reference_field",
* label = @Translation("Entity reference field item"),
* @FieldType(
* id = "entity_reference",
* label = @Translation("Entity reference"),
* description = @Translation("An entity field containing an entity reference."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList",
* configurable = FALSE,
* constraints = {"ValidReference" = TRUE}
* )
*/
......
......@@ -2,23 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\FloatItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\FloatItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'float_field' entity field item.
* Defines the 'float' entity field type.
*
* @DataType(
* id = "float_field",
* label = @Translation("Float field item"),
* @FieldType(
* id = "float",
* label = @Translation("Float"),
* description = @Translation("An entity field containing an float value."),
* list_class = "\Drupal\Core\Entity\Field\Field"
* configurable = FALSE
* )
*/
class FloatItem extends FieldItemBase {
......
......@@ -2,23 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\IntegerItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\IntegerItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'integer_field' entity field item.
* Defines the 'integer' entity field type.
*
* @DataType(
* id = "integer_field",
* label = @Translation("Integer field item"),
* @FieldType(
* id = "integer",
* label = @Translation("Integer"),
* description = @Translation("An entity field containing an integer value."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList"
* configurable = FALSE
* )
*/
class IntegerItem extends FieldItemBase {
......
......@@ -2,24 +2,22 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\LanguageItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\LanguageItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
use Drupal\Core\Language\Language;
/**
* Defines the 'language_field' entity field item.
* Defines the 'language' entity field item.
*
* @DataType(
* id = "language_field",
* label = @Translation("Language field item"),
* @FieldType(
* id = "language",
* label = @Translation("Language"),
* description = @Translation("An entity field referencing a language."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList",
* configurable = FALSE,
* constraints = {
* "ComplexData" = {
* "value" = {"Length" = {"max" = 12}}
......
......@@ -2,23 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\StringItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\StringItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'string_field' entity field item.
* Defines the 'string' entity field type.
*
* @DataType(
* id = "string_field",
* label = @Translation("String field item"),
* @FieldType(
* id = "string",
* label = @Translation("String"),
* description = @Translation("An entity field containing a string value."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList"
* configurable = FALSE
* )
*/
class StringItem extends FieldItemBase {
......
......@@ -2,23 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\UriItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\UriItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
namespace Drupal\Core\Entity\Plugin\field\field_type;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'uri_field' entity field item.
* Defines the 'uri' entity field type.
*
* @DataType(
* id = "uri_field",
* label = @Translation("URI field item"),
* @FieldType(
* id = "uri",
* label = @Translation("URI"),
* description = @Translation("An entity field containing a URI."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList"
* configurable = FALSE
* )
*/
class UriItem extends FieldItemBase {
......
......@@ -2,24 +2,21 @@
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\DataType\UuidItem.
* Contains \Drupal\Core\Entity\Plugin\field\field_type\UuidItem.
*/
namespace Drupal\Core\Entity\Plugin\DataType;
use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
namespace Drupal\Core\Entity\Plugin\field\field_type;
/**
* Defines the 'uuid_field' entity field item.
* Defines the 'uuid' entity field type.
*
* The field uses a newly generated UUID as default value.
*
* @DataType(
* id = "uuid_field",
* label = @Translation("UUID field item"),
* @FieldType(
* id = "uuid",
* label = @Translation("UUID"),
* description = @Translation("An entity field containing a UUID."),
* list_class = "\Drupal\Core\Entity\Field\FieldItemList",
* configurable = FALSE,
* constraints = {
* "ComplexData" = {
* "value" = {"Length" = {"max" = 128}}
......
......@@ -7,7 +7,7 @@
namespace Drupal\comment;
use Drupal\Core\Entity\Plugin\DataType\StringItem;
use Drupal\Core\Entity\Plugin\field\field_type\StringItem;
/**
* The field item for the 'fieldname' field.
......
......@@ -49,11 +49,8 @@ public function getPropertyDefinitions() {
'label' => t('Comment status value'),
),
'cid' => array(
'type' => 'entity_reference_field',
'type' => 'integer',
'label' => t('Last comment ID'),
'settings' => array(
'target_type' => 'comment',
),
),
'last_comment_timestamp' => array(
'label' => t('Last comment timestamp'),
......@@ -66,11 +63,8 @@ public function getPropertyDefinitions() {
'type' => 'string',
),
'last_comment_uid' => array(
'type' => 'entity_reference_field',
'type' => 'integer',
'label' => t('Last comment user ID'),
'settings' => array(
'target_type' => 'user',
),
),
'comment_count' => array(
'label' => t('Number of comments'),
......
......@@ -31,9 +31,16 @@ function email_help($path, $arg) {
* Implements hook_field_info_alter().
*/
function email_field_info_alter(&$info) {
$info['email']['configurable'] = TRUE;
$info['email']['class'] = '\Drupal\email\ConfigurableEmailItem';
$info['email']['list_class'] = '\Drupal\field\Plugin\Type\FieldType\ConfigFieldItemList';
$info['email']['default_widget'] = 'email_default';
if (\Drupal::moduleHandler()->moduleExists('text')) {
$info['email']['default_formatter'] = 'text_plain';
}
else {
$info['email']['default_formatter'] = 'email_mailto';
}
}
/**
......
......@@ -2,26 +2,21 @@
/**
* @file
* Contains \Drupal\email\Plugin\field\field_type\ConfigurableEmailItem.
* Contains \Drupal\email\ConfigurableEmailItem.
*/
namespace Drupal\email\Plugin\field\field_type;
namespace Drupal\email;
use Drupal\Core\Entity\Plugin\DataType\EmailItem;
use Drupal\Core\Entity\Plugin\field\field_type\EmailItem;
use Drupal\field\FieldInterface;
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface;
/**
* Plugin implementation of the 'email' field type.
* Alternative plugin implementation for the 'email' entity field type.
*
* @FieldType(
* id = "email",
* label = @Translation("E-mail"),
* description = @Translation("This field stores an e-mail address in the database."),
* default_widget = "email_default",
* default_formatter = "email_mailto"
* )
* Replaces the default implementation and supports configurable fields.
*/
class ConfigurableEmailItem extends EmailItem {
class ConfigurableEmailItem extends EmailItem implements ConfigFieldItemInterface {
/**
* Defines the max length for an email address
......@@ -69,4 +64,19 @@ public function getConstraints() {
return $constraints;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state, $has_data) {
return array();
}
/**
* {@inheritdoc}
*/
public function instanceSettingsForm(array $form, array &$form_state) {
return array();
}
}
......@@ -15,11 +15,15 @@
* Implements hook_field_info_alter().
*/
function entity_reference_field_info_alter(&$info) {
if (!\Drupal::moduleHandler()->moduleExists('node')) {
// Fall back to another primary entity type if the Node module is not
// available.
$info['entity_reference']['settings']['target_type'] = 'user';
}
// Make the entity reference field configurable.
$info['entity_reference']['configurable'] = TRUE;
$info['entity_reference']['class'] = '\Drupal\entity_reference\ConfigurableEntityReferenceItem';
$info['entity_reference']['list_class'] = '\Drupal\field\Plugin\Type\FieldType\ConfigFieldItemList';
$info['entity_reference']['settings']['target_type'] = \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user';
$info['entity_reference']['instance_settings']['handler'] = 'default';
$info['entity_reference']['instance_settings']['handler_settings'] = array();
$info['entity_reference']['default_widget'] = 'entity_reference_autocomplete';
$info['entity_reference']['default_formatter'] = 'entity_reference_label';
}
/**
......@@ -27,7 +31,7 @@ function entity_reference_field_info_alter(&$info) {
*
* Set the "target_type" property definition for entity reference fields.
*
* @see \Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem::getPropertyDefinitions()
* @see \Drupal\Core\Entity\Plugin\field\field_type\EntityReferenceItem::getPropertyDefinitions()
*
* @param array $info
* The property info array as returned by hook_entity_field_info().
......
......@@ -2,39 +2,23 @@
/**
* @file
* Contains \Drupal\entity_reference\Plugin\field\field_type\ConfigurableEntityReferenceItem.
* Contains \Drupal\entity_reference\ConfigurableEntityReferenceItem.
*/
namespace Drupal\entity_reference\Plugin\field\field_type;
namespace Drupal\entity_reference;
use Drupal\field\Plugin\Type\FieldType\ConfigEntityReferenceItemBase;
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface;
use Drupal\field\FieldInterface;
/**
* Plugin implementation of the 'entity_reference' field type.
* Alternative plugin implementation of the 'entity_reference' field type.
*
* @FieldType(
* id = "entity_reference",
* label = @Translation("Entity Reference"),
* description = @Translation("This field references another entity."),
* settings = {
* "target_type" = "node"
* },
* instance_settings = {
* "handler" = "default",
* "handler_settings" = { }
* },
* default_widget = "entity_reference_autocomplete",
* default_formatter = "entity_reference_label",
* constraints = {"ValidReference" = TRUE}
* )
* Replaces the Core 'entity_reference' entity field type implementation, this
* supports configurable fields, auto-creation of referenced entities and more.
*
* Extends the Core 'entity_reference' entity field item with properties for
* revision ids, labels (for autocreate) and access.
* @see entity_reference_field_info_alter().
*
* Required settings (below the definition's 'settings' key) are:
* - target_type: The entity type to reference.
*/
class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface {
......@@ -84,7 +68,7 @@ public function preSave() {
$entity = $this->get('entity')->getValue();
$target_id = $this->get('target_id')->getValue();
if (empty($target_id) && !empty($entity) && $entity->isNew()) {
if (!$target_id && !empty($entity) && $entity->isNew()) {
$entity->save();
$this->set('target_id', $entity->id());
}
......
......@@ -24,14 +24,14 @@
* @deprecated as of Drupal 8.0. Use
* \Drupal::service('plugin.manager.entity.field.field_type')->getDefinition()
* or
* \Drupal::service('plugin.manager.entity.field.field_type')->getDefinitions()
* \Drupal::service('plugin.manager.entity.field.field_type')->getConfigurableDefinitions().
*/
function field_info_field_types($field_type = NULL) {
if ($field_type) {
return \Drupal::service('plugin.manager.entity.field.field_type')->getDefinition($field_type);
}
else {
return \Drupal::service('plugin.manager.entity.field.field_type')->getDefinitions();
return \Drupal::service('plugin.manager.entity.field.field_type')->getConfigurableDefinitions();
}