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 @@ +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/src/Plugin/Field/FieldType/DiceFieldItemList.php b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceFieldItemList.php index 242ade0..06a125d 100644 --- 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 @@ -2,30 +2,21 @@ namespace Drupal\field_computed_test\Plugin\Field\FieldType; -use Drupal\Core\Field\FieldItemList; +use Drupal\Core\Field\FieldItemListComputed; /** * Represents a configurable dice result. */ -class DiceFieldItemList extends FieldItemList { - +class DiceFieldItemList extends FieldItemListComputed { /** * {@inheritdoc} */ - public function getValue($include_computed = FALSE) { + protected function computeItemValues() { $items_count = $this->getParent()->getValue()->field_dice_count->value; - $values = array(); + $values = []; foreach (range(0, $items_count - 1) as $delta) { $values[$delta] = rand(1, 6); } return $values; } - - /** - * Implements \Countable::count(). - */ - public function count() { - $items_count = $this->getParent()->getValue()->field_dice_count->value; - return $items_count; - } }