diff --git a/core/lib/Drupal/Core/Field/FieldItemListComputed.php b/core/lib/Drupal/Core/Field/FieldItemListComputed.php new file mode 100644 index 0000000..b00dc30 --- /dev/null +++ b/core/lib/Drupal/Core/Field/FieldItemListComputed.php @@ -0,0 +1,59 @@ +list = []; + $values = $this->computeItemValues(); + if (is_array($values) && !empty($values)) { + foreach ($values AS $delta => $value) { + $this->list[] = $this->createItem($delta, $value); + } + } + } + + /** + * This should compute the values that are used to create the field items. + * + * @return array The values for the field items. + */ + protected abstract function computeItemValues(); + + /** + * This will do nothing as computed lists are readonly bz default. + */ + public function setValue($values, $notify = TRUE) {} + + /** + * This will do nothing as computed lists are readonly bz default. + */ + public function appendItem($value = NULL) {} + + /** + * This will do nothing as computed lists are readonly bz default. + */ + public function defaultValuesForm(array &$form, FormStateInterface $form_state) {} + + /** + * This will do nothing as computed lists are readonly bz default. + */ + public function removeItem($index) {} + + /** + * This will do nothing as computed lists are readonly bz default. + */ + public function set($index, $value) {} +} diff --git a/core/lib/Drupal/Core/Field/FieldItemListComputedInterface.php b/core/lib/Drupal/Core/Field/FieldItemListComputedInterface.php new file mode 100644 index 0000000..f6a6769 --- /dev/null +++ b/core/lib/Drupal/Core/Field/FieldItemListComputedInterface.php @@ -0,0 +1,9 @@ +itemDefinition = $definition; return $this; } + + /** + * {@inheritdoc} + */ + public function isComputed() { + if ($this->isComputedByClassInheritance()) { + return TRUE; + } + return parent::isComputed(); + } + + /** + * {@inheritdoc} + * @throws \LogicException + * Thrown if computed is to be set to FALSE for a field that requires to be computed by it's list class. + */ + public function setComputed($computed) { + if (!$computed && $this->isComputedByClassInheritance()) { + throw new \LogicException("The list class used enforces this field to be a computed field."); + } + return parent::setComputed($computed); + } + + /** + * Helper to deduce whether the the field should be computed based on it's list class. + * + * @return bool + * + * @see \Drupal\Core\Field\FieldItemListComputed + */ + protected function isComputedByClassInheritance() { + $class = $this->getClass(); + if (!empty($class)) { + $reflectionClass = new ReflectionClass($class); + return $reflectionClass->isSubclassOf('\Drupal\Core\Field\FieldItemListComputed'); + } + return FALSE; + + } } diff --git a/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php b/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php index 2a868db..003cd57 100644 --- a/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php +++ b/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php @@ -102,10 +102,7 @@ public function get($index) { if (!is_numeric($index)) { throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.'); } - // Automatically create the first item for computed fields. - if ($index == 0 && !isset($this->list[0]) && $this->definition->isComputed()) { - $this->list[0] = $this->createItem(0); - } + return isset($this->list[$index]) ? $this->list[$index] : NULL; } diff --git a/core/lib/Drupal/Core/TypedData/TypedDataManager.php b/core/lib/Drupal/Core/TypedData/TypedDataManager.php index fb75edc..ef52582 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -12,6 +12,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Field\FieldItemListComputed; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\TypedData\Validation\MetadataFactory; use Drupal\Core\Validation\ConstraintManager; @@ -309,6 +310,9 @@ public function getPropertyInstance(TypedDataInterface $object, $property_name, $property->setContext($property_name, $object); if (isset($value)) { $property->setValue($value, FALSE); + } elseif ($property instanceof FieldItemListComputed) { + // populate the computed list with values as there are no initial values to set. + $property->computeItems(); } return $property; } diff --git a/core/modules/field/tests/modules/field_computed_test/field_computed_test.info.yml b/core/modules/field/tests/modules/field_computed_test/field_computed_test.info.yml new file mode 100644 index 0000000..42f6216 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/field_computed_test.info.yml @@ -0,0 +1,6 @@ +name: 'Field Computed Test' +type: module +description: 'Support module for the computed field tests.' +core: 8.x +package: Testing +version: VERSION diff --git a/core/modules/field/tests/modules/field_computed_test/field_computed_test.module b/core/modules/field/tests/modules/field_computed_test/field_computed_test.module new file mode 100644 index 0000000..28bd677 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/field_computed_test.module @@ -0,0 +1,60 @@ +id() === 'node') { + $fields = array(); + + $fields['field_dice_count'] = BaseFieldDefinition::create('integer') + ->setCustomStorage(false) + ->setLabel(t('Dice count')) + ->setDisplayOptions('view', array( + 'label' => 'inline', + 'type' => 'text_textfield', + 'weight' => 0, + )) + ->setDisplayOptions('form', array( + 'type' => 'integer', + 'weight' => 10, + )) + ->setDisplayConfigurable('form', TRUE); + + // set a custom list class an explicitly define the field to be computed + $fields['field_dice_result'] = BaseFieldDefinition::create('integer') + ->setLabel(t('Dice result')) + ->setClass('\Drupal\field_computed_test\Plugin\Field\FieldType\DiceItemList') + ->setComputed(TRUE) + ->setDisplayOptions('view', array( + 'label' => 'inline', + 'type' => 'number_integer', + 'weight' => 0, + )); + + // set a custom list class and rely on that class to mark the field as computed + $fields['field_dice_result_v2'] = BaseFieldDefinition::create('integer') + ->setLabel(t('Dice result V2')) + ->setClass('\Drupal\field_computed_test\Plugin\Field\FieldType\DiceItemList') + ->setDisplayOptions('view', array( + 'label' => 'inline', + 'type' => 'number_integer', + 'weight' => 0, + )); + + // use a field type which uses a list class that marks the field as computed + $fields['field_dice_result_v3'] = BaseFieldDefinition::create('dice') + ->setLabel(t('Dice result V3')) + ->setDisplayOptions('view', array( + 'label' => 'inline', + 'weight' => 0, + )) + ->setDisplayOptions('form', array( + 'type' => 'integer', + 'weight' => 10, + )); + + return $fields; + } + +} diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldFormatter/DiceFormatter.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldFormatter/DiceFormatter.php new file mode 100644 index 0000000..09a106b --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldFormatter/DiceFormatter.php @@ -0,0 +1,32 @@ + $item) { + $elements[$delta] = [ + '#markup' => 'The value: '. $item->value, + ]; + } + return $elements; + } +} diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItem.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItem.php new file mode 100644 index 0000000..c0061b1 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItem.php @@ -0,0 +1,35 @@ +setLabel(t('Integer value')); + return $properties; + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldStorageDefinitionInterface $field_definition) { + return []; + } +} diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItemList.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItemList.php new file mode 100644 index 0000000..a71f274 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItemList.php @@ -0,0 +1,24 @@ +getEntity()->field_dice_count->value; + $values = []; + foreach (range(0, $items_count - 1) as $delta) { + $values[$delta] = [ + 'value' => rand(1, 6) + ]; + } + return $values; + } +} diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldWidget/DiceWidget.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldWidget/DiceWidget.php new file mode 100644 index 0000000..9e22a97 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldWidget/DiceWidget.php @@ -0,0 +1,30 @@ + 'This is a computed field that contains a number of random values where the amount of items depends on value of the field_dice_count field on the same entity.', + ]; + return $element; + } +}