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/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..242ade0 --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceFieldItemList.php @@ -0,0 +1,31 @@ +getParent()->getValue()->field_dice_count->value; + $values = array(); + 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; + } +}