diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php index 69b2400..6c70241 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php @@ -9,8 +9,8 @@ use Drupal\Core\TypedData\Annotation\DataType; use Drupal\Core\Annotation\Translation; -use Drupal\Core\Entity\Field\FieldItemBase; -use Drupal\Core\TypedData\TypedDataInterface; +use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase; +use Drupal\field\FieldInterface; /** * Defines the 'entity_reference_item' entity field item. @@ -29,7 +29,7 @@ * constraints = {"ValidReference" = TRUE} * ) */ -class EntityReferenceItem extends FieldItemBase { +class EntityReferenceItem extends ConfigFieldItemBase { /** * Definitions of the contained properties. @@ -41,7 +41,7 @@ class EntityReferenceItem extends FieldItemBase { static $propertyDefinitions; /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). + * {@inheritdoc} */ public function getPropertyDefinitions() { // Definitions vary by entity type and bundle, so key them accordingly. @@ -76,7 +76,7 @@ public function getPropertyDefinitions() { } /** - * Overrides \Drupal\Core\Entity\Field\FieldItemBase::__get(). + * {@inheritdoc} */ public function __get($name) { $name = ($name == 'value') ? 'target_id' : $name; @@ -84,7 +84,7 @@ public function __get($name) { } /** - * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get(). + * {@inheritdoc} */ public function get($property_name) { $property_name = ($property_name == 'value') ? 'target_id' : $property_name; @@ -92,7 +92,7 @@ public function get($property_name) { } /** - * Implements \Drupal\Core\Entity\Field\FieldItemInterface::__isset(). + * {@inheritdoc} */ public function __isset($property_name) { $property_name = ($property_name == 'value') ? 'target_id' : $property_name; @@ -100,7 +100,7 @@ public function __isset($property_name) { } /** - * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get(). + * {@inheritdoc} */ public function setValue($values, $notify = TRUE) { if (isset($values) && !is_array($values)) { @@ -134,4 +134,40 @@ public function onChange($property_name) { } parent::onChange($property_name); } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + // Avoid loading the entity by first checking the 'target_id'. + $target_id = $this->get('target_id')->getValue(); + if (!empty($target_id) && is_numeric($target_id)) { + return FALSE; + } + // Allow auto-create entities. + if (empty($target_id) && ($entity = $this->get('entity')->getValue()) && $entity->isNew()) { + return FALSE; + } + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function preSave() { + $entity = $this->get('entity')->getValue(); + $target_id = $this->get('target_id')->getValue(); + + if (empty($target_id) && !empty($entity) && $entity->isNew()) { + $entity->save(); + $this->set('target_id', $entity->id()); + } + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldInterface $field) { + return array(); + } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php index ee5e79a..edf1530 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php @@ -9,11 +9,7 @@ use Drupal\Core\Annotation\Translation; use Drupal\Core\Entity\Annotation\FieldType; -use Drupal\Core\Entity\Field\Type\EntityReferenceItem; -use Drupal\field\Plugin\Type\FieldType\ConfigEntityReferenceItemBase; -use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase; -use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface; -use Drupal\field\FieldInterface; +use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem; /** * Plugin implementation of the 'entity_reference' field type. @@ -40,60 +36,41 @@ * Required settings (below the definition's 'settings' key) are: * - target_type: The entity type to reference. */ -class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface { +class ConfigurableEntityReferenceItem extends EntityReferenceItem { /** * {@inheritdoc} */ - public static function schema(FieldInterface $field) { - $schema = array( - 'columns' => array( - 'target_id' => array( - 'description' => 'The ID of the target entity.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, + public function getPropertyDefinitions() { + // Definitions vary by entity type and bundle, so key them accordingly. + $key = $this->definition['settings']['target_type'] . ':'; + $key .= isset($this->definition['settings']['target_bundle']) ? $this->definition['settings']['target_bundle'] : ''; + + if (!isset(static::$propertyDefinitions[$key])) { + // Call the parent to define the target_id and entity properties. + parent::getPropertyDefinitions(); + + static::$propertyDefinitions[$key]['revision_id'] = array( + + // @todo: Lookup the entity type's ID data type and use it here. + 'type' => 'integer', + 'label' => t('Revision ID'), + 'constraints' => array( + 'Range' => array('min' => 0), ), - 'revision_id' => array( - 'description' => 'The revision ID of the target entity.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - ), - ), - 'indexes' => array( - 'target_id' => array('target_id'), - ), - ); - - // Create a foreign key to the target entity type base type. - $entity_manager = \Drupal::service('entity.manager'); - if (is_subclass_of($entity_manager->getControllerClass($field['settings']['target_type'], 'storage'), 'Drupal\Core\Entity\DatabaseStorageController')) { - $entity_info = $entity_manager->getDefinition($field['settings']['target_type']); - - $base_table = $entity_info['base_table']; - $id_column = $entity_info['entity_keys']['id']; - - $schema['foreign keys'][$base_table] = array( - 'table' => $base_table, - 'columns' => array('target_id' => $id_column), + ); + static::$propertyDefinitions[$key]['label'] = array( + 'type' => 'string', + 'label' => t('Label (auto-create)'), + 'computed' => TRUE, + ); + static::$propertyDefinitions[$key]['access'] = array( + 'type' => 'boolean', + 'label' => t('Access'), + 'computed' => TRUE, ); } - - return $schema; - } - - /** - * {@inheritdoc} - */ - public function preSave() { - $entity = $this->get('entity')->getValue(); - $target_id = $this->get('target_id')->getValue(); - - if (empty($target_id) && !empty($entity) && $entity->isNew()) { - $entity->save(); - $this->set('target_id', $entity->id()); - } + return static::$propertyDefinitions[$key]; } /** diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php deleted file mode 100644 index 6b3c137..0000000 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigEntityReferenceItemBase.php +++ /dev/null @@ -1,175 +0,0 @@ -definition['settings']['target_type'] . ':'; - $key .= isset($this->definition['settings']['target_bundle']) ? $this->definition['settings']['target_bundle'] : ''; - - if (!isset(static::$propertyDefinitions[$key])) { - // Call the parent to define the target_id and entity properties. - parent::getPropertyDefinitions(); - - static::$propertyDefinitions[$key]['revision_id'] = array( - // @todo: Lookup the entity type's ID data type and use it here. - 'type' => 'integer', - 'label' => t('Revision ID'), - 'constraints' => array( - 'Range' => array('min' => 0), - ), - ); - static::$propertyDefinitions[$key]['label'] = array( - 'type' => 'string', - 'label' => t('Label (auto-create)'), - 'computed' => TRUE, - ); - static::$propertyDefinitions[$key]['access'] = array( - 'type' => 'boolean', - 'label' => t('Access'), - 'computed' => TRUE, - ); - } - return static::$propertyDefinitions[$key]; - } - - /** - * {@inheritdoc} - * - * Copied from \Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem, - * since we cannot extend it. - */ - public static function schema(FieldInterface $field) { - $definition = \Drupal::service('plugin.manager.entity.field.field_type')->getDefinition($field->type); - $module = $definition['provider']; - module_load_install($module); - $callback = "{$module}_field_schema"; - if (function_exists($callback)) { - return $callback($field); - } - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - // Avoid loading the entity by first checking the 'target_id'. - $target_id = $this->get('target_id')->getValue(); - if (!empty($target_id) && is_numeric($target_id)) { - return FALSE; - } - // Allow auto-create entities. - if (empty($target_id) && ($entity = $this->get('entity')->getValue()) && $entity->isNew()) { - return FALSE; - } - return TRUE; - } - - /** - * {@inheritdoc} - * - * Copied from \Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem, - * since we cannot extend it. - */ - public function settingsForm(array $form, array &$form_state, $has_data) { - if ($callback = $this->getLegacyCallback('settings_form')) { - $instance = $this->getFieldDefinition(); - if (!($instance instanceof FieldInstanceInterface)) { - throw new \UnexpectedValueException('ConfigEntityReferenceItemBase::settingsForm() called for a field whose definition is not a field instance.'); - } - // hook_field_settings_form() used to receive the $instance (not actually - // needed), and the value of field_has_data(). - return $callback($instance->getField(), $instance, $has_data); - } - return array(); - } - - /** - * {@inheritdoc} - * - * Copied from \Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem, - * since we cannot extend it. - */ - public function instanceSettingsForm(array $form, array &$form_state) { - if ($callback = $this->getLegacyCallback('instance_settings_form')) { - $instance = $this->getFieldDefinition(); - if (!($instance instanceof FieldInstanceInterface)) { - throw new \UnexpectedValueException('ConfigEntityReferenceItemBase::instanceSettingsForm() called for a field whose definition is not a field instance.'); - } - return $callback($instance->getField(), $instance, $form_state); - } - return array(); - } - - /** - * Returns options provided via the legacy callback hook_options_list(). - * - * @todo: Convert all legacy callback implementations to methods. - * - * @see \Drupal\Core\TypedData\AllowedValuesInterface - */ - public function getSettableOptions() { - $definition = $this->getPluginDefinition(); - $callback = "{$definition['provider']}_options_list"; - if (function_exists($callback)) { - // We are at the field item level, so we need to go two levels up to get - // to the entity object. - return $callback($this->getFieldDefinition(), $this->getEntity()); - } - } - - /** - * Returns the legacy callback for a given field type "hook". - * - * Copied from \Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem, - * since we cannot extend it. - * - * @param string $hook - * The name of the hook, e.g. 'settings_form', 'is_empty'. - * - * @return string|null - * The name of the legacy callback, or NULL if it does not exist. - */ - protected function getLegacyCallback($hook) { - $definition = $this->getPluginDefinition(); - $module = $definition['provider']; - $callback = "{$module}_field_{$hook}"; - if (function_exists($callback)) { - return $callback; - } - } - -} diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php index 5de9a76..11e4911 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php @@ -11,7 +11,6 @@ use Drupal\Core\Entity\Annotation\FieldType; use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem; use Drupal\field\FieldInterface; -use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface; /** * Plugin implementation of the 'file' field type. @@ -36,7 +35,7 @@ * list_class = "\Drupal\file\Plugin\field\field_type\FileField" * ) */ -class FileItem extends EntityReferenceItem implements ConfigFieldItemInterface { +class FileItem extends EntityReferenceItem { /** * Property definitions of the contained properties. @@ -213,6 +212,13 @@ public function instanceSettingsForm(array $form, array &$form_state) { /** * {@inheritdoc} */ + public function preSave() { + return; + } + + /** + * {@inheritdoc} + */ public function insert() { // @todo Move in FileField in https://drupal.org/node/2073033. $entity = $this->getRoot(); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php index 3997007..a2bdd38 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php @@ -11,7 +11,6 @@ use Drupal\Core\Annotation\Translation; use Drupal\field\FieldInterface; use Drupal\Core\Entity\Plugin\DataType\EntityReferenceItem; -use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\TypedData\AllowedValuesInterface; @@ -36,7 +35,7 @@ * default_formatter = "taxonomy_term_reference_link" * ) */ -class TaxonomyTermReferenceItem extends EntityReferenceItem implements ConfigFieldItemInterface, AllowedValuesInterface { +class TaxonomyTermReferenceItem extends EntityReferenceItem implements AllowedValuesInterface { /** * {@inheritdoc} @@ -86,30 +85,6 @@ public function getPropertyDefinitions() { /** * {@inheritdoc} */ - public static function schema(FieldInterface $field) { - return array( - 'columns' => array( - 'target_id' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - ), - ), - 'indexes' => array( - 'target_id' => array('target_id'), - ), - 'foreign keys' => array( - 'target_id' => array( - 'table' => 'taxonomy_term_data', - 'columns' => array('target_id' => 'tid'), - ), - ), - ); - } - - /** - * {@inheritdoc} - */ public function settingsForm(array $form, array &$form_state, $has_data) { // Get proper values for 'allowed_values_function', which is a core setting. $vocabularies = entity_load_multiple('taxonomy_vocabulary'); @@ -144,29 +119,25 @@ public function settingsForm(array $form, array &$form_state, $has_data) { /** * {@inheritdoc} */ - public function instanceSettingsForm(array $form, array &$form_state) { - return; - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - $target_id = $this->get('target_id')->getValue(); - $entity = $this->get('entity')->getValue(); - return (empty($target_id) && empty($entity)); - } - - /** - * {@inheritdoc} - */ - public function preSave() { - $target_id = $this->get('target_id')->getValue(); - $entity = $this->get('entity')->getValue(); - if (!$target_id) { - $this->entity = $entity->save(); - $this->target_id = $entity->id(); - } + public static function schema(FieldInterface $field) { + return array( + 'columns' => array( + 'target_id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), + ), + 'indexes' => array( + 'target_id' => array('target_id'), + ), + 'foreign keys' => array( + 'target_id' => array( + 'table' => 'taxonomy_term_data', + 'columns' => array('target_id' => 'tid'), + ), + ), + ); } /**