Commit a2d86efb authored by catch's avatar catch

Issue #2050801 by yched, amateescu: Unify handling of default values between...

Issue #2050801 by yched, amateescu: Unify handling of default values between base and configurable fields.
parent 7dc6bbf0
......@@ -224,16 +224,34 @@ 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);
}
else {
// @todo Remove getDefaultValue() and directly call
// FieldDefinition::getFieldDefaultValue() here, once
// https://drupal.org/node/2047229 is fixed.
$value = $this->getDefaultValue();
// 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))) {
// Create one field item and apply defaults.
$this->offsetGet(0)->applyDefaultValue(FALSE);
}
else {
$this->setValue($value, $notify);
}
return $this;
}
/**
* 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}
*/
......
......@@ -7,6 +7,8 @@
namespace Drupal\Core\Entity\Field;
use Drupal\Core\Entity\EntityInterface;
/**
* Defines an interface for entity field definitions.
*
......@@ -156,9 +158,26 @@ 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 mixed
* The default value for the field, as accepted by
* Drupal\field\Plugin\Core\Entity\Field::setValue(). This can be either:
* - a literal, in which case it will be assigned to the first property of
* the first item.
* - a numerically indexed array of items, each item being a property/value
* array.
* - NULL or array() for no default value.
*/
public function getFieldDefaultValue(EntityInterface $entity);
}
......@@ -52,6 +52,6 @@ public function getPropertyDefinitions() {
* {@inheritdoc}
*/
public function isEmpty() {
return !isset($this->values['value']) || $this->values['value'] === '';
return $this->value === NULL || $this->value === '';
}
}
......@@ -1188,3 +1188,25 @@ function field_get_items(EntityInterface $entity, $field_name, $langcode = NULL)
$langcode = field_language($entity, $field_name, $langcode);
return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
}
/**
* Helper function to get the default value for a field on an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity for the operation.
* @param $field
* The field structure.
* @param $instance
* 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) {
return $instance->getFieldDefaultValue($entity);
}
......@@ -233,44 +233,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.
*/
......@@ -492,30 +454,6 @@ function field_sync_field_status() {
field_cache_clear();
}
/**
* Helper function to get the default value for a field on an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity for the operation.
* @param $field
* The field structure.
* @param $instance
* The instance structure.
* @param $langcode
* The field language to fill-in with the default value.
*/
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;
}
/**
* Gets or sets administratively defined bundle settings.
*
......
......@@ -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;
......@@ -648,6 +649,11 @@ public function isFieldRequired() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getFieldDefaultValue(EntityInterface $entity) { }
/**
* {@inheritdoc}
*/
......
......@@ -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;
......@@ -582,6 +583,19 @@ public function isFieldRequired() {
return $this->required;
}
/**
* {@inheritdoc}
*/
public function getFieldDefaultValue(EntityInterface $entity) {
if (!empty($this->default_value_function)) {
$function = $this->default_value_function;
return $function($entity, $this->getField(), $this, $entity->language()->id);
}
elseif (!empty($this->default_value)) {
return $this->default_value;
}
}
/**
* {@inheritdoc}
*/
......
......@@ -72,4 +72,11 @@ public function getConstraints() {
return $constraints;
}
/**
* {@inheritdoc}
*/
protected function getDefaultValue() {
return $this->getInstance()->getFieldDefaultValue($this->getParent());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment