*/
abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigInterface {
+ use FieldInputValueNormalizerTrait;
+
/**
* The field ID.
*
*/
protected $constraints = [];
+ /**
+ * Array of property constraint options keyed by property ID. The values are
+ * associative array of constraint options keyed by constraint plugin ID.
+ *
+ * @var array[]
+ */
+ protected $propertyConstraints = [];
+
/**
* {@inheritdoc}
*/
return $changed;
}
-
/**
* {@inheritdoc}
*/
// Allow custom default values function.
if ($callback = $this->getDefaultValueCallback()) {
$value = call_user_func($callback, $entity, $this);
+ $value = $this->normalizeValue($value, $this->getFieldStorageDefinition()->getMainPropertyName());
}
else {
$value = $this->getDefaultValueLiteral();
* {@inheritdoc}
*/
public function setDefaultValue($value) {
- if (!is_array($value)) {
- if ($value === NULL) {
- $value = [];
- }
- $key = $this->getFieldStorageDefinition()->getPropertyNames()[0];
- // Convert to the multi value format to support fields with a cardinality
- // greater than 1.
- $value = [
- [$key => $value],
- ];
- }
- $this->default_value = $value;
+ $this->default_value = $this->normalizeValue($value, $this->getFieldStorageDefinition()->getMainPropertyName());
return $this;
}
if (!isset($this->itemDefinition)) {
$this->itemDefinition = FieldItemDataDefinition::create($this)
->setSettings($this->getSettings());
+
+ // Add any custom property constraints, overwriting as required.
+ $item_constraints = $this->itemDefinition->getConstraint('ComplexData') ?: [];
+ foreach ($this->propertyConstraints as $name => $constraints) {
+ if (isset($item_constraints[$name])) {
+ $item_constraints[$name] = $constraints + $item_constraints[$name];
+ }
+ else {
+ $item_constraints[$name] = $constraints;
+ }
+ $this->itemDefinition->addConstraint('ComplexData', $item_constraints);
+ }
}
+
return $this->itemDefinition;
}
* {@inheritdoc}
*/
public function setPropertyConstraints($name, array $constraints) {
- $item_constraints = $this->getItemDefinition()->getConstraints();
- $item_constraints['ComplexData'][$name] = $constraints;
- $this->getItemDefinition()->setConstraints($item_constraints);
+ $this->propertyConstraints[$name] = $constraints;
+
+ // Reset the field item definition so the next time it is instantiated it
+ // will receive the new constraints.
+ $this->itemDefinition = NULL;
+
return $this;
}
* {@inheritdoc}
*/
public function addPropertyConstraints($name, array $constraints) {
- $item_constraints = $this->getItemDefinition()->getConstraint('ComplexData') ?: [];
- if (isset($item_constraints[$name])) {
- // Add the new property constraints, overwriting as required.
- $item_constraints[$name] = $constraints + $item_constraints[$name];
- }
- else {
- $item_constraints[$name] = $constraints;
+ foreach ($constraints as $constraint_name => $options) {
+ $this->propertyConstraints[$name][$constraint_name] = $options;
}
- $this->getItemDefinition()->addConstraint('ComplexData', $item_constraints);
+
+ // Reset the field item definition so the next time it is instantiated it
+ // will receive the new constraints.
+ $this->itemDefinition = NULL;
+
return $this;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function isInternal() {
+ // Respect the definition, otherwise default to TRUE for computed fields.
+ if (isset($this->definition['internal'])) {
+ return $this->definition['internal'];
+ }
+ return $this->isComputed();
+ }
+
}