diff --git a/core/includes/form.inc b/core/includes/form.inc index 05f2a9e1fcea5dcdc81c063ee47100c826505b9a..9d253351e27ffb0738841d4c88c034e6b93f8e9d 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -370,12 +370,13 @@ function template_preprocess_form(&$variables) { * @param array $variables * An associative array containing: * - element: An associative array containing the properties of the element. - * Properties used: #title, #value, #description, #rows, #cols, - * #placeholder, #required, #attributes, #resizable + * Properties used: #title, #value, #description, #rows, #cols, #maxlength, + * #placeholder, #required, #attributes, #resizable. */ function template_preprocess_textarea(&$variables) { $element = $variables['element']; - Element::setAttributes($element, ['id', 'name', 'rows', 'cols', 'placeholder']); + $attributes = ['id', 'name', 'rows', 'cols', 'maxlength', 'placeholder']; + Element::setAttributes($element, $attributes); RenderElement::setAttributes($element, ['form-textarea']); $variables['wrapper_attributes'] = new Attribute(); $variables['attributes'] = new Attribute($element['#attributes']); diff --git a/core/lib/Drupal/Core/Render/Element/Textarea.php b/core/lib/Drupal/Core/Render/Element/Textarea.php index 340bc114e602f680bf9b7699626418fc592b1220..aa809b20802326e40c3c5e303b58f3bf30f5a531 100644 --- a/core/lib/Drupal/Core/Render/Element/Textarea.php +++ b/core/lib/Drupal/Core/Render/Element/Textarea.php @@ -12,6 +12,7 @@ * - #cols: Number of columns in the text box. * - #resizable: Controls whether the text area is resizable. Allowed values * are "none", "vertical", "horizontal", or "both" (defaults to "vertical"). + * - #maxlength: The maximum amount of characters to accept as input. * * Usage example: * @code diff --git a/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php b/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php new file mode 100644 index 0000000000000000000000000000000000000000..94026b65414f7f88025e6a81536570bf81f27738 --- /dev/null +++ b/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php @@ -0,0 +1,80 @@ +<?php + +namespace Drupal\Tests\system\Kernel\Form; + +use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormState; +use Drupal\Core\Form\FormStateInterface; +use Drupal\KernelTests\KernelTestBase; +use Symfony\Component\CssSelector\CssSelectorConverter; + +/** + * Tests the maxlength HTML attribute on form elements. + * + * @group Form + */ +class FormElementMaxlengthTest extends KernelTestBase implements FormInterface { + + /** + * {@inheritdoc} + */ + public static $modules = ['system']; + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'form_test_maxlength'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form['title'] = [ + '#type' => 'textfield', + '#maxlength' => 255, + ]; + + $form['description'] = [ + '#type' => 'textarea', + '#maxlength' => 255, + ]; + + $form['submit'] = [ + '#type' => 'submit', + '#value' => 'Submit', + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) {} + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) {} + + /** + * Ensures maxlength attribute can be used in compatible elements. + */ + public function testAttributes() { + + /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */ + $form_builder = $this->container->get('form_builder'); + $form_state = new FormState(); + $elements = $form_builder->buildForm($this, $form_state); + $this->render($elements); + + $css_selector_converter = new CssSelectorConverter(); + $elements = $this->xpath($css_selector_converter->toXPath('input[name=title][maxlength=255]')); + $this->assertCount(1, $elements, 'Text field has correct maxlength in form.'); + $elements = $this->xpath($css_selector_converter->toXPath('textarea[name=description][maxlength=255]')); + $this->assertCount(1, $elements, 'Textarea field has correct maxlength in form.'); + } + +}