diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index d8b2baf..2488982 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -911,7 +911,7 @@ public function __clone() { // object keyed by language. To avoid creating different field objects // we retain just the original value, as references will be recreated // later as needed. - if (empty($definitions[$name]['translatable']) && count($values) > 1) { + if (!$definitions[$name]->isFieldTranslatable() && count($values) > 1) { $values = array_intersect_key($values, array(Language::LANGCODE_DEFAULT => TRUE)); } foreach ($values as $langcode => $items) { diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 95a4a90..2bce05d 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -366,7 +366,7 @@ public function getFieldDefinitions($entity_type, $bundle = NULL) { if (is_array($definition)) { $definition = FieldDefinition::createFromOldStyleDefinition($definition); } - // Automatically set the field-name for non-configurable fields. + // Automatically set the field name for non-configurable fields. if ($definition instanceof FieldDefinition) { $definition->setFieldName($field_name); } @@ -377,7 +377,7 @@ public function getFieldDefinitions($entity_type, $bundle = NULL) { $hooks = array('entity_field_info', $entity_type . '_field_info'); $this->moduleHandler->alter($hooks, $this->entityFieldInfo[$entity_type], $entity_type); - // Ensure all untranslatable fields are not defined as translatable. + // Ensure all basic fields are not defined as translatable. $entity_info = $this->getDefinition($entity_type); $keys = array_intersect_key(array_filter($entity_info['entity_keys']), array_flip(array('id', 'revision', 'uuid', 'bundle'))); $untranslatable_fields = array_flip(array('langcode') + $keys); diff --git a/core/lib/Drupal/Core/Field/FieldDefinition.php b/core/lib/Drupal/Core/Field/FieldDefinition.php index 6294067..640bee3 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinition.php +++ b/core/lib/Drupal/Core/Field/FieldDefinition.php @@ -61,20 +61,6 @@ public function getFieldType() { } /** - * Sets the field type. - * - * @param string $type - * The field type to set. - * - * @return self - * The object itself for chaining. - */ - public function setFieldType($type) { - $this->getItemDefinition()->setDataType('field_item:' . $type); - return $this; - } - - /** * {@inheritdoc} */ public function getFieldSettings() { diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index 50eaaeb..d260668 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -15,7 +15,7 @@ /** * Represents an entity field; that is, a list of field item objects. * - * An entity field is a list of field items, containing various field item + * An entity field is a list of field items, each containing a set of * properties. Note that even single-valued entity fields are represented as * list of field items, however for easy access to the contained item the entity * field delegates __get() and __set() calls directly to the first item. diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LegacyConfigFieldItemList.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LegacyConfigFieldItemList.php index 82c3237..f239c60 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LegacyConfigFieldItemList.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LegacyConfigFieldItemList.php @@ -102,7 +102,7 @@ public function deleteRevision() { * The name of the hook, e.g. 'presave', 'validate'. */ protected function legacyCallback($hook, $args = array()) { - $type_definition = \Drupal::typedData()->getDefinition($this->getItemDefinition()->getDataType()); + $type_definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($this->getFieldDefinition()->getFieldType()); $module = $type_definition['provider']; $callback = "{$module}_field_{$hook}"; if (function_exists($callback)) { diff --git a/core/lib/Drupal/Core/TypedData/TypedDataInterface.php b/core/lib/Drupal/Core/TypedData/TypedDataInterface.php index bef5a36..4d7b066 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataInterface.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataInterface.php @@ -17,10 +17,8 @@ /** * Gets the data definition. * - * @return array - * The data definition represented as array. - * - * @see \Drupal\Core\TypedData\DataDefinition + * @return \Drupal\Core\TypedData\DataDefinitionInterface + * The data definition object. */ public function getDefinition(); diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index 040749a..66b6d66 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -135,6 +135,7 @@ function content_translation_entity_field_info_alter(&$info, $entity_type) { foreach ($keys as $key) { if (isset($info[$key][$name])) { $info[$key][$name]->setTranslatable((bool) $translatable); + break; } } } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 73ac825..91464f0 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -196,7 +196,7 @@ function field_entity_field_info($entity_type) { $optional = $bundle_name != $entity_type; // @todo: Improve hook_entity_field_info() to allow per-bundle field // definitions, such that we can pass on field instances as field - // definitions here. + // definitions here. See https://drupal.org/node/2114707. foreach ($instances as $field_name => $instance) { if ($optional) { diff --git a/core/modules/field/lib/Drupal/field/Entity/Field.php b/core/modules/field/lib/Drupal/field/Entity/Field.php index 9a032df..5d84c15 100644 --- a/core/modules/field/lib/Drupal/field/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Entity/Field.php @@ -633,6 +633,20 @@ public function isFieldMultiple() { public function getFieldDefaultValue(EntityInterface $entity) { } /** + * {@inheritdoc} + */ + public function isFieldConfigurable() { + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function isFieldQueryable() { + return TRUE; + } + + /** * A list of columns that can not be used as field type columns. * * @return array @@ -677,26 +691,31 @@ public function hasData() { } /** - * {@inheritdoc} + * Implements the magic __sleep() method. + * + * Using the Serialize interface and serialize() / unserialize() methods + * breaks entity forms in PHP 5.4. + * @todo Investigate in https://drupal.org/node/2074253. */ - public function isFieldQueryable() { - return TRUE; + public function __sleep() { + // Only serialize properties from getExportProperties(). + return array_keys(array_intersect_key($this->getExportProperties(), get_object_vars($this))); } /** - * {@inheritdoc} + * Implements the magic __wakeup() method. */ - public function getDataType() { - return 'list'; + public function __wakeup() { + // Run the values from getExportProperties() through __construct(). + $values = array_intersect_key($this->getExportProperties(), get_object_vars($this)); + $this->__construct($values); } /** * {@inheritdoc} */ - public function setDataType($type) { - if ($type != 'list') { - throw new \LogicException('Fields are lists of field items, thus must always be of data type "list".'); - } + public function getDataType() { + return 'list'; } /** @@ -745,11 +764,10 @@ public function isRequired() { * {@inheritdoc} */ public function getClass() { - // Derive list class from the item type. - $item_type_definition = \Drupal::typedData() - ->getDefinition($this->getItemDefinition()->getDataType()); - - return $item_type_definition['list_class']; + // Derive list class from the field type. + $type_definition = \Drupal::service('plugin.manager.field.field_type') + ->getDefinition($this->getFieldType()); + return $type_definition['list_class']; } /** @@ -782,32 +800,4 @@ public function getItemDefinition() { return $this->itemDefinition; } - /** - * Implements the magic __sleep() method. - * - * Using the Serialize interface and serialize() / unserialize() methods - * breaks entity forms in PHP 5.4. - * @todo Investigate in https://drupal.org/node/2074253. - */ - public function __sleep() { - // Only serialize properties from getExportProperties(). - return array_keys(array_intersect_key($this->getExportProperties(), get_object_vars($this))); - } - - /** - * Implements the magic __wakeup() method. - */ - public function __wakeup() { - // Run the values from getExportProperties() through __construct(). - $values = array_intersect_key($this->getExportProperties(), get_object_vars($this)); - $this->__construct($values); - } - - /** - * {@inheritdoc} - */ - public function isFieldConfigurable() { - return TRUE; - } - } diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index 8d169d9..56e0108 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -614,22 +614,22 @@ public function getFieldDefaultValue(EntityInterface $entity) { /** * {@inheritdoc} */ - public function allowBundleRename() { - $this->bundle_rename_allowed = TRUE; + public function isFieldConfigurable() { + return TRUE; } /** * {@inheritdoc} */ - public function isFieldConfigurable() { + public function isFieldQueryable() { return TRUE; } /** * {@inheritdoc} */ - public function isFieldQueryable() { - return TRUE; + public function allowBundleRename() { + $this->bundle_rename_allowed = TRUE; } /* @@ -660,15 +660,6 @@ public function getDataType() { /** * {@inheritdoc} */ - public function setDataType($type) { - if ($type != 'list') { - throw new \LogicException('Fields are lists of field items, thus must always be of data type "list".'); - } - } - - /** - * {@inheritdoc} - */ public function getLabel() { return $this->label(); } @@ -713,11 +704,7 @@ public function isRequired() { * {@inheritdoc} */ public function getClass() { - // Derive list class from the item type. - $item_type_definition = \Drupal::typedData() - ->getDefinition($this->getItemDefinition()->getDataType()); - - return $item_type_definition['list_class']; + return $this->field->getClass(); } /** diff --git a/core/modules/system/entity.api.php b/core/modules/system/entity.api.php index 95ec520..f87f5a3 100644 --- a/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@ -627,7 +627,7 @@ function hook_entity_form_display_alter(\Drupal\entity\Entity\EntityFormDisplay * * @return array * An array of entity field information having the following optional entries: - * - definitions: An array of field definitions to add all entities of this + * - definitions: An array of field definitions to add to all entities of this * type, keyed by field name. * - optional: An array of field definitions for optional entity fields, keyed * by field name. Optional fields are fields that only exist for certain diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php index afb5479..7987635 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Term.php @@ -202,7 +202,6 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $ * {@inheritdoc} */ public static function baseFieldDefinitions($entity_type) { - $fields['tid'] = FieldDefinition::create('integer') ->setLabel(t('Term ID')) ->setDescription(t('The term ID.')) diff --git a/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php index 3b14d87..5dfc084 100644 --- a/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/FieldDefinitionTest.php @@ -33,8 +33,8 @@ protected function setUp() { // Prepare a container with a mock typed data object, that returns no // type definitions. - // @todo: Overhaul this once field definitions to not use the container to - /// get the manager any more. + // @todo: Overhaul how field definitions deal with dependencies and improve + // unit tests. See https://drupal.org/node/2143555. $typed_data = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManager') ->disableOriginalConstructor() ->getMock(); @@ -87,10 +87,9 @@ public function testFieldDescription() { * Tests field type methods. */ public function testFieldType() { - $definition = new FieldDefinition(); - $field_name = $this->randomName(); - $definition->setFieldType($field_name); - $this->assertEquals($field_name, $definition->getFieldType()); + $field_type = $this->randomName(); + $definition = FieldDefinition::create($field_type); + $this->assertEquals($field_type, $definition->getFieldType()); } /**