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 @@ +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 7733427..c267815 100644 --- a/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php +++ b/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php @@ -97,10 +97,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 4904150..c15b0de 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -7,6 +7,7 @@ use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Field\FieldItemListComputed; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\TypedData\Validation\ExecutionContextFactory; use Drupal\Core\TypedData\Validation\RecursiveValidator; @@ -193,6 +194,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..44bda1b --- /dev/null +++ b/core/modules/field/tests/modules/field_computed_test/src/Plugin/Field/FieldType/DiceItem.php @@ -0,0 +1,37 @@ +setLabel(t('Integer value')); + return $properties; + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldStorageDefinitionInterface $field_definition) { + return [ + 'columns' => [] + ]; + } +} 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; + } +} diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php index 90bd2cc..b39dd5a 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php @@ -83,6 +83,13 @@ protected function setUp() { ->method('getDefaultFieldSettings') ->willReturn([]); + $typed_data_manager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface'); + // @todo: maybe use a reasonable argument + $typed_data_manager->expects($this->any()) + ->method('getDefinition') + ->with($this->anything()) + ->will($this->returnValue(['list_class' => '\Drupal\Core\Field\FieldItemList'])); + $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->renderer = $this->getMock('Drupal\Core\Render\RendererInterface'); @@ -94,6 +101,7 @@ protected function setUp() { $this->container = new ContainerBuilder(); $this->container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); + $this->container->set('typed_data_manager', $typed_data_manager); \Drupal::setContainer($this->container); } diff --git a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php index dbd50e7..9c6b735 100644 --- a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php @@ -64,8 +64,16 @@ protected function setUp() { ->with($this->fieldType) ->will($this->returnValue($this->fieldTypeDefinition['field_settings'])); + $typed_data_manager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface'); + // @todo: maybe use a reasonable argument + $typed_data_manager->expects($this->any()) + ->method('getDefinition') + ->with($this->anything()) + ->will($this->returnValue(['list_class' => '\Drupal\Core\Field\FieldItemList'])); + $container = new ContainerBuilder(); $container->set('plugin.manager.field.field_type', $field_type_manager); + $container->set('typed_data_manager', $typed_data_manager); \Drupal::setContainer($container); } diff --git a/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php index 646f17a..b1ce768 100644 --- a/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php @@ -140,8 +140,15 @@ protected function setUp() { $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class); $this->typedDataManager->expects($this->any()) ->method('getDefinition') - ->with('entity') - ->will($this->returnValue(['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter'])); + ->will($this->returnCallback(function($plugin_id){ + switch ($plugin_id) { + case 'entity': + return ['class' => '\Drupal\Core\Entity\Plugin\DataType\EntityAdapter']; + default: + // @todo: maybe use a reasonable argument value + return ['list_class' => '\Drupal\Core\Field\FieldItemList']; + } + })); $this->typedDataManager->expects($this->any()) ->method('getDefaultConstraints') ->willReturn([]);