diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 1cbf329..f552c54 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -494,12 +494,14 @@ public function getFieldDefinitions($entity_type, $bundle = NULL) { // Enforce field definitions to be objects. foreach ($this->entityFieldInfo[$entity_type]['definitions'] as $field_name => &$definition) { if (is_array($definition)) { - $definition = FieldDefinition::createFromOldStyleDefinition($field_name, $definition); + $definition = FieldDefinition::createFromOldStyleDefinition($definition) + ->setFieldName($field_name); } } foreach ($this->entityFieldInfo[$entity_type]['optional'] as $field_name => &$definition) { if (is_array($definition)) { - $definition = FieldDefinition::createFromOldStyleDefinition($field_name, $definition); + $definition = FieldDefinition::createFromOldStyleDefinition($definition) + ->setFieldName($field_name); } } $this->cache->set($cid, $this->entityFieldInfo[$entity_type], CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE, 'entity_field_info' => TRUE)); diff --git a/core/lib/Drupal/Core/Entity/Field/FieldDefinition.php b/core/lib/Drupal/Core/Entity/Field/FieldDefinition.php index 9ab8bf6..226a9ca 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldDefinition.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldDefinition.php @@ -247,38 +247,38 @@ public function getFieldDefaultValue(EntityInterface $entity) { * * @todo: Remove once no old-style definition arrays need to be supported. */ - public static function createFromOldStyleDefinition($field_name, array $definition) { + public static function createFromOldStyleDefinition(array $definition) { unset($definition['list']); + // Separate the list item definition from the list definition. $list_definition = $definition; $list_definition['type'] = 'entity_field'; + + // Constraints and settings apply to the list item. unset($list_definition['constraints']); unset($list_definition['settings']); - $new = new FieldDefinition($list_definition); + $field_definition = new FieldDefinition($list_definition); if (isset($definition['list_class'])) { - $new->setClass($definition['list_class']); + $field_definition->setClass($definition['list_class']); } else { $type_definition = \Drupal::typedData()->getDefinition($definition['type']); if (isset($type_definition['list_class'])) { - $new->setClass($type_definition['list_class']); + $field_definition->setClass($type_definition['list_class']); } } // Take care of the item definition. - $new->setItemDefinition(new DataDefinition($definition)); - $new->setFieldName($field_name); + $item_definition = new DataDefinition($definition); if (isset($definition['settings'])) { - $new->setFieldSettings($definition['settings']); + $item_definition->setSettings($definition['settings']); } if (isset($definition['constraints'])) { - $new->getItemDefinition()->setConstraints($definition['constraints']); - } - if (isset($definition['class'])) { - $new->getItemDefinition()->setClass($definition['class']); + $item_definition->setConstraints($definition['constraints']); } - return $new; + $field_definition->setItemDefinition($item_definition); + return $field_definition; } /** diff --git a/core/lib/Drupal/Core/Entity/Field/FieldItemList.php b/core/lib/Drupal/Core/Entity/Field/FieldItemList.php index 9a6a4d4..a2b308c 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldItemList.php @@ -214,10 +214,8 @@ public function defaultAccess($operation = 'view', AccountInterface $account = N * {@inheritdoc} */ public function applyDefaultValue($notify = TRUE) { - // @todo Remove getDefaultValue() and directly call - // FieldDefinition::getFieldDefaultValue() here, once - // https://drupal.org/node/2047229 is fixed. - $value = $this->getDefaultValue(); + $value = $this->getFieldDefinition()->getFieldDefaultValue($this->getEntity()); + // NULL or array() mean "no default value", but 0, '0' and the empty string // are valid default values. if (!isset($value) || (is_array($value) && empty($value))) { @@ -231,18 +229,6 @@ public function applyDefaultValue($notify = TRUE) { } /** - * Returns the default value for the field. - * - * @return array - * The default value for the field. - */ - protected function getDefaultValue() { - if (isset($this->definition['settings']['default_value'])) { - return $this->definition['settings']['default_value']; - } - } - - /** * {@inheritdoc} */ public function getConstraints() { diff --git a/core/lib/Drupal/Core/TypedData/DataDefinition.php b/core/lib/Drupal/Core/TypedData/DataDefinition.php index cc0fc68..e7016c7 100644 --- a/core/lib/Drupal/Core/TypedData/DataDefinition.php +++ b/core/lib/Drupal/Core/TypedData/DataDefinition.php @@ -301,4 +301,48 @@ public function offsetUnset($offset) { public function toArray() { return $this->definition; } + + /** + * Allows creating data definition objects from old style definition arrays. + * + * @todo: Remove once no old-style definition arrays need to be supported. + */ + public static function createFromOldStyleDefinition(array $definition) { + if (empty($definition['list'])) { + return new DataDefinition($definition); + } + + // If the definition describes a list, separate the list item definition + // from the list definition. + unset($definition['list']); + + $list_definition = $definition; + $list_definition['type'] = 'list'; + + // Constraints and settings apply to the list item. + unset($list_definition['constraints']); + unset($list_definition['settings']); + + $list_definition = new ListDefinition($list_definition); + if (isset($definition['list_class'])) { + $list_definition->setClass($definition['list_class']); + } + else { + $type_definition = \Drupal::typedData()->getDefinition($definition['type']); + if (isset($type_definition['list_class'])) { + $list_definition->setClass($type_definition['list_class']); + } + } + + // Take care of the item definition. + $item_definition = new DataDefinition($definition); + if (isset($definition['settings'])) { + $item_definition->setSettings($definition['settings']); + } + if (isset($definition['constraints'])) { + $item_definition->setConstraints($definition['constraints']); + } + $list_definition->setItemDefinition($item_definition); + return $list_definition; + } } diff --git a/core/lib/Drupal/Core/TypedData/ListInterface.php b/core/lib/Drupal/Core/TypedData/ListInterface.php index 2088c2d..d5593ef 100644 --- a/core/lib/Drupal/Core/TypedData/ListInterface.php +++ b/core/lib/Drupal/Core/TypedData/ListInterface.php @@ -29,7 +29,7 @@ public function isEmpty(); /** * Gets the definition of a contained item. * - * @return array + * @return \Drupal\Core\TypedData\DataDefinitionInterface * The data definition of contained items. */ public function getItemDefinition(); diff --git a/core/lib/Drupal/Core/TypedData/TypedDataManager.php b/core/lib/Drupal/Core/TypedData/TypedDataManager.php index 35ce304..107ad2f 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -121,7 +121,7 @@ public function createInstance($data_type, array $configuration) { */ public function create($definition, $value = NULL, $name = NULL, $parent = NULL) { if (is_array($definition)) { - $definition = new DataDefinition($definition); + $definition = DataDefinition::createFromOldStyleDefinition($definition); } $typed_data = $this->createInstance($definition->getDataType(), array( 'data_definition' => $definition, diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemList.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemList.php index 2400e3d..90555cf 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemList.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigFieldItemList.php @@ -70,13 +70,6 @@ public function getConstraints() { /** * {@inheritdoc} */ - protected function getDefaultValue() { - return $this->getFieldDefinition()->getFieldDefaultValue($this->getEntity()); - } - - /** - * {@inheritdoc} - */ public function defaultValuesForm(array &$form, array &$form_state) { if (empty($this->getFieldDefinition()->default_value_function)) { // Place the input in a separate place in the submitted values tree. diff --git a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItemList.php b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItemList.php index 22cd8a0..5d20b0c 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItemList.php +++ b/core/modules/field/lib/Drupal/field/Plugin/field/field_type/LegacyConfigFieldItemList.php @@ -102,8 +102,8 @@ public function deleteRevision() { * The name of the hook, e.g. 'presave', 'validate'. */ protected function legacyCallback($hook, $args = array()) { - $definition = $this->getPluginDefinition(); - $module = $definition['provider']; + $type_definition = \Drupal::typedData()->getDefinition($this->getItemDefinition()->getDataType()); + $module = $type_definition['provider']; $callback = "{$module}_field_{$hook}"; if (function_exists($callback)) { // We need to remove the empty "prototype" item here.