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 @@ +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 7fe362e..552098d 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..941cdfe --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/field_computed_test.module @@ -0,0 +1,37 @@ +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); + + $fields['field_dice_result'] = BaseFieldDefinition::create('integer') + ->setLabel(t('Dice result')) + ->setComputed(TRUE) + ->setClass('\Drupal\field_computed_test\Plugin\Field\FieldType\DiceFieldItemList') + ->setDisplayOptions('view', array( + 'label' => 'inline', + 'type' => 'number_integer', + 'weight' => 0, + )); + + return $fields; + } + +} diff --git a/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceFieldItemList.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceFieldItemList.php new file mode 100644 index 0000000..06a125d --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceFieldItemList.php @@ -0,0 +1,22 @@ +getParent()->getValue()->field_dice_count->value; + $values = []; + foreach (range(0, $items_count - 1) as $delta) { + $values[$delta] = rand(1, 6); + } + return $values; + } +}