diff --git a/core/lib/Drupal/Core/Render/Element/FormElement.php b/core/lib/Drupal/Core/Render/Element/FormElement.php index ab13289aa7c177eca64a3a33df06a5f53eec222d..173a55cb93f54906e2076a2cf29a9f32a5d4756a 100644 --- a/core/lib/Drupal/Core/Render/Element/FormElement.php +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php @@ -128,10 +128,10 @@ public static function processAutocomplete(&$element, FormStateInterface $form_s $metadata = BubbleableMetadata::createFromRenderArray($element); if ($access->isAllowed()) { $element['#attributes']['class'][] = 'form-autocomplete'; - $element['#attached']['library'][] = 'core/drupal.autocomplete'; + $metadata->addAttachments(['library' => ['core/drupal.autocomplete']]); // Provide a data attribute for the JavaScript behavior to bind to. $element['#attributes']['data-autocomplete-path'] = $url->getGeneratedUrl(); - $metadata->merge($url); + $metadata = $metadata->merge($url); } $metadata ->merge(BubbleableMetadata::createFromObject($access)) diff --git a/core/modules/system/src/Tests/Form/ElementTest.php b/core/modules/system/src/Tests/Form/ElementTest.php index d7085dd621e4f7aca99d14bd71571945368c2039..6555a4f063fbf592ffb069b2a6f572982b23e334 100644 --- a/core/modules/system/src/Tests/Form/ElementTest.php +++ b/core/modules/system/src/Tests/Form/ElementTest.php @@ -151,6 +151,9 @@ public function testFormAutocomplete() { $this->drupalLogin($user); $this->drupalGet('form-test/autocomplete'); + // Make sure that the autocomplete library is added. + $this->assertRaw('core/misc/autocomplete.js'); + $result = $this->xpath('//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]'); $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion'); $result = $this->xpath('//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');