Loading core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php +1 −1 Original line number Diff line number Diff line Loading @@ -187,7 +187,7 @@ protected function getSelectedOptions(FieldItemListInterface $items) { $value = $item->{$this->column}; // Keep the value if it actually is in the list of options (needs to be // checked against the flat list). if (isset($flat_options[$value])) { if ($value !== NULL && isset($flat_options[$value])) { $selected_options[] = $value; } } Loading core/modules/options/tests/options_test/src/Plugin/Field/FieldWidget/OptionsSelectNoMultipleWidget.php 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\options_test\Plugin\Field\FieldWidget; use Drupal\Core\Field\Attribute\FieldWidget; use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget; use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Widget extending OptionsSelectWidget without declaring multiple_values. */ #[FieldWidget( id: 'options_select_no_multiple', label: new TranslatableMarkup('Options select (no multiple_values)'), field_types: ['list_integer', 'list_float', 'list_string'], )] class OptionsSelectNoMultipleWidget extends OptionsSelectWidget { } core/modules/options/tests/src/Kernel/OptionsWidgetBaseTest.php 0 → 100644 +42 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\options\Kernel; use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase; use Drupal\entity_test\Entity\EntityTest; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; /** * Tests OptionsWidgetBase. */ #[CoversClass(OptionsWidgetBase::class)] #[Group('options')] #[RunTestsInSeparateProcesses] class OptionsWidgetBaseTest extends OptionsFieldUnitTestBase { /** * {@inheritdoc} */ protected static $modules = ['options', 'options_test']; /** * Tests that getSelectedOptions() skips NULL item values. */ public function testGetSelectedOptionsWithNullValue(): void { \Drupal::service('entity_display.repository') ->getFormDisplay('entity_test', 'entity_test') ->setComponent($this->fieldName, ['type' => 'options_select_no_multiple']) ->save(); // An entity with no value triggers formMultipleElements() which appends // an item with value = NULL before calling getSelectedOptions(). $entity = EntityTest::create(); $form = \Drupal::service('entity.form_builder')->getForm($entity); $this->assertSame([], $form[$this->fieldName]['widget'][0]['#default_value']); } } Loading
core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php +1 −1 Original line number Diff line number Diff line Loading @@ -187,7 +187,7 @@ protected function getSelectedOptions(FieldItemListInterface $items) { $value = $item->{$this->column}; // Keep the value if it actually is in the list of options (needs to be // checked against the flat list). if (isset($flat_options[$value])) { if ($value !== NULL && isset($flat_options[$value])) { $selected_options[] = $value; } } Loading
core/modules/options/tests/options_test/src/Plugin/Field/FieldWidget/OptionsSelectNoMultipleWidget.php 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\options_test\Plugin\Field\FieldWidget; use Drupal\Core\Field\Attribute\FieldWidget; use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget; use Drupal\Core\StringTranslation\TranslatableMarkup; /** * Widget extending OptionsSelectWidget without declaring multiple_values. */ #[FieldWidget( id: 'options_select_no_multiple', label: new TranslatableMarkup('Options select (no multiple_values)'), field_types: ['list_integer', 'list_float', 'list_string'], )] class OptionsSelectNoMultipleWidget extends OptionsSelectWidget { }
core/modules/options/tests/src/Kernel/OptionsWidgetBaseTest.php 0 → 100644 +42 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\options\Kernel; use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase; use Drupal\entity_test\Entity\EntityTest; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; /** * Tests OptionsWidgetBase. */ #[CoversClass(OptionsWidgetBase::class)] #[Group('options')] #[RunTestsInSeparateProcesses] class OptionsWidgetBaseTest extends OptionsFieldUnitTestBase { /** * {@inheritdoc} */ protected static $modules = ['options', 'options_test']; /** * Tests that getSelectedOptions() skips NULL item values. */ public function testGetSelectedOptionsWithNullValue(): void { \Drupal::service('entity_display.repository') ->getFormDisplay('entity_test', 'entity_test') ->setComponent($this->fieldName, ['type' => 'options_select_no_multiple']) ->save(); // An entity with no value triggers formMultipleElements() which appends // an item with value = NULL before calling getSelectedOptions(). $entity = EntityTest::create(); $form = \Drupal::service('entity.form_builder')->getForm($entity); $this->assertSame([], $form[$this->fieldName]['widget'][0]['#default_value']); } }