*/
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}
*/
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];
+ foreach ($constraints as $constraint_name => $options) {
+ $this->propertyConstraints[$name][$constraint_name] = $options;
}
- else {
- $item_constraints[$name] = $constraints;
- }
- $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();
+ }
+
}