Loading core/modules/text/src/Plugin/Field/FieldType/TextFieldItemList.php +6 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,12 @@ public function defaultValuesFormValidate(array $element, array &$form, FormStat 'default_value_input', $field_name, ]); // Remove the 'add_more' button from the submitted values if it exists. if (isset($submitted_values['add_more'])) { unset($submitted_values['add_more']); } foreach ($submitted_values as $delta => $value) { if (!in_array($value['format'], $allowed_formats, TRUE)) { $form_state->setErrorByName( Loading core/modules/text/tests/src/Unit/Plugin/Field/FieldType/TextFieldItemListTest.php 0 → 100644 +60 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Unit\Plugin\Field\FieldType; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Tests\UnitTestCase; use Drupal\text\Plugin\Field\FieldType\TextFieldItemList; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; /** * Tests TextFieldItemList. */ #[CoversClass(TextFieldItemList::class)] #[Group('text')] class TextFieldItemListTest extends UnitTestCase { /** * Tests ::defaultValuesFormValidate() if the field allows multiple values. * * Ensures that the add_more button is filtered out of the submitted values * before validation, preventing PHP warnings due to trying to access array * keys on a TranslatableMarkup object. */ public function testDefaultValuesFormValidateWithMultipleCardinality(): void { $field_name = 'field_test_text'; $definition = $this->createMock(FieldDefinitionInterface::class); $definition->expects($this->once()) ->method('getName') ->willReturn($field_name); $definition->expects($this->once()) ->method('getSetting') ->with('allowed_formats') ->willReturn(['restricted_html']); $form_state = $this->createMock(FormStateInterface::class); $form_state->expects($this->once()) ->method('getValue') ->with(['default_value_input', $field_name]) ->willReturn([ 'add_more' => new TranslatableMarkup('Add another item'), ]); $form_state->expects($this->once()) ->method('has') ->with('default_value_widget') ->willReturn(TRUE); $form_state->expects($this->once()) ->method('get') ->with('default_value_widget') ->willReturn([]); $item_list = new TextFieldItemList($definition); $form = []; $item_list->defaultValuesFormValidate([], $form, $form_state); } } Loading
core/modules/text/src/Plugin/Field/FieldType/TextFieldItemList.php +6 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,12 @@ public function defaultValuesFormValidate(array $element, array &$form, FormStat 'default_value_input', $field_name, ]); // Remove the 'add_more' button from the submitted values if it exists. if (isset($submitted_values['add_more'])) { unset($submitted_values['add_more']); } foreach ($submitted_values as $delta => $value) { if (!in_array($value['format'], $allowed_formats, TRUE)) { $form_state->setErrorByName( Loading
core/modules/text/tests/src/Unit/Plugin/Field/FieldType/TextFieldItemListTest.php 0 → 100644 +60 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Unit\Plugin\Field\FieldType; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Tests\UnitTestCase; use Drupal\text\Plugin\Field\FieldType\TextFieldItemList; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; /** * Tests TextFieldItemList. */ #[CoversClass(TextFieldItemList::class)] #[Group('text')] class TextFieldItemListTest extends UnitTestCase { /** * Tests ::defaultValuesFormValidate() if the field allows multiple values. * * Ensures that the add_more button is filtered out of the submitted values * before validation, preventing PHP warnings due to trying to access array * keys on a TranslatableMarkup object. */ public function testDefaultValuesFormValidateWithMultipleCardinality(): void { $field_name = 'field_test_text'; $definition = $this->createMock(FieldDefinitionInterface::class); $definition->expects($this->once()) ->method('getName') ->willReturn($field_name); $definition->expects($this->once()) ->method('getSetting') ->with('allowed_formats') ->willReturn(['restricted_html']); $form_state = $this->createMock(FormStateInterface::class); $form_state->expects($this->once()) ->method('getValue') ->with(['default_value_input', $field_name]) ->willReturn([ 'add_more' => new TranslatableMarkup('Add another item'), ]); $form_state->expects($this->once()) ->method('has') ->with('default_value_widget') ->willReturn(TRUE); $form_state->expects($this->once()) ->method('get') ->with('default_value_widget') ->willReturn([]); $item_list = new TextFieldItemList($definition); $form = []; $item_list->defaultValuesFormValidate([], $form, $form_state); } }