diff --git a/core/lib/Drupal/Core/Entity/Field/Field.php b/core/lib/Drupal/Core/Entity/Field/Field.php index 0ff36d7..6cdd88c 100644 --- a/core/lib/Drupal/Core/Entity/Field/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Field.php @@ -224,8 +224,8 @@ public function defaultAccess($operation = 'view', AccountInterface $account = N * {@inheritdoc} */ public function applyDefaultValue($notify = TRUE) { - if (isset($this->definition['settings']['default_value'])) { - $this->setValue($this->definition['settings']['default_value'], $notify); + if ($value = $this->getDefaultValue()) { + $this->setValue($value, $notify); } else { // Create one field item and apply defaults. @@ -235,6 +235,18 @@ 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/Entity/Field/FieldDefinitionInterface.php b/core/lib/Drupal/Core/Entity/Field/FieldDefinitionInterface.php index 386d47d..bad055b 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldDefinitionInterface.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldDefinitionInterface.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Entity\Field; +use Drupal\Core\Entity\EntityInterface; + /** * Defines an interface for entity field definitions. * @@ -156,9 +158,21 @@ public function getFieldCardinality(); * Currently, required-ness is only enforced at the Form API level in entity * edit forms, not during direct API saves. * - * @var bool + * @return bool * TRUE if the field is required. */ public function isFieldRequired(); + /** + * Returns the default value for the field in a newly created entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity being created. + * + * @return array + * The default value for the field, as an array keyed by delta and by + * property names. + */ + public function getFieldDefaultValue(EntityInterface $entity); + } diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index ebbcdd8..107e954 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -7,7 +7,8 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Datetime\DrupalDateTime; -use Drupal\Core\Template\Attribute; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\Field\FieldDefinitionInterface; use Drupal\datetime\DateHelper; use Drupal\node\NodeInterface; diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 3203f2e..1e0fd23 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -234,44 +234,6 @@ function field_system_info_alter(&$info, $file, $type) { } /** - * Implements hook_entity_create(). - */ -function field_entity_create(EntityInterface $entity) { - $info = $entity->entityInfo(); - if (!empty($info['fieldable'])) { - field_populate_default_values($entity, $entity->language()->id); - } -} - -/** - * Inserts a default value for each entity field not having one. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity for the operation. - * @param string $langcode - * (optional) The field language code to fill-in with the default value. - * Defaults to the entity language. - */ -function field_populate_default_values(EntityInterface $entity, $langcode = NULL) { - // Ensure we are working with a BC mode entity. - $entity = $entity->getBCEntity(); - - $entity_type = $entity->entityType(); - $langcode = $langcode ?: $entity->language()->id; - foreach (field_info_instances($entity_type, $entity->bundle()) as $field_name => $instance) { - $field = field_info_field($field_name); - $field_langcode = field_is_translatable($entity_type, $field) ? $langcode : Language::LANGCODE_NOT_SPECIFIED; - // We need to preserve existing values. - if (empty($entity->{$field_name}) || !array_key_exists($field_langcode, $entity->{$field_name})) { - $items = field_get_default_value($entity, $field, $instance, $field_langcode); - if (!empty($items)) { - $entity->{$field_name}[$field_langcode] = $items; - } - } - } -} - -/** * Implements hook_entity_field_info() to define all configured fields. */ function field_entity_field_info($entity_type) { @@ -488,17 +450,15 @@ function field_sync_field_status() { * The instance structure. * @param $langcode * The field language to fill-in with the default value. + * + * @return array + * The default value for the field. + * + * @deprecated as of Drupal 8.0. Use + * $instance->getFieldDefaultValue($entity) */ function field_get_default_value(EntityInterface $entity, $field, $instance, $langcode = NULL) { - $items = array(); - if (!empty($instance['default_value_function'])) { - $function = $instance['default_value_function']; - $items = $function($entity, $field, $instance, $langcode); - } - elseif (!empty($instance['default_value'])) { - $items = $instance['default_value']; - } - return $items; + return $instance->getFieldDefaultValue($entity); } /** diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php index bccc64b..ca0eed1 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal\Core\Entity\EntityInterface; use Drupal\field\FieldException; use Drupal\field\FieldInterface; @@ -651,6 +652,13 @@ public function isFieldRequired() { /** * {@inheritdoc} */ + public function getFieldDefaultValue(EntityInterface $entity) { + return array(); + } + + /** + * {@inheritdoc} + */ public function offsetExists($offset) { return isset($this->{$offset}) || in_array($offset, array('columns', 'foreign keys', 'bundles', 'storage_details')); } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php index 9628256..e00267f 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal\Core\Entity\EntityInterface; use Drupal\field\FieldException; use Drupal\field\FieldInstanceInterface; @@ -565,6 +566,19 @@ public function isFieldRequired() { /** * {@inheritdoc} */ + public function getFieldDefaultValue(EntityInterface $entity) { + if (!empty($this->default_value_function)) { + $function = $this->default_value_function; + return $function($entity->entityType(), $entity, $this->getField(), $this, $entity->language()->id); + } + elseif (!empty($this->default_value)) { + return $this->default_value; + } + } + + /** + * {@inheritdoc} + */ public function allowBundleRename() { $this->bundle_rename_allowed = TRUE; } diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php index 29ce028..dfed088 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/FieldType/ConfigField.php @@ -72,4 +72,11 @@ public function getConstraints() { return $constraints; } + /** + * {@inheritdoc} + */ + protected function getDefaultValue() { + return $this->getInstance()->getFieldDefaultValue($this->getParent()); + } + }