diff --git a/core/lib/Drupal/Core/Render/Element/FormElement.php b/core/lib/Drupal/Core/Render/Element/FormElement.php index ab13289aa7c..173a55cb93f 100644 --- a/core/lib/Drupal/Core/Render/Element/FormElement.php +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php @@ -128,10 +128,10 @@ abstract class FormElement extends RenderElement implements FormElementInterface $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 d7085dd621e..6555a4f063f 100644 --- a/core/modules/system/src/Tests/Form/ElementTest.php +++ b/core/modules/system/src/Tests/Form/ElementTest.php @@ -151,6 +151,9 @@ class ElementTest extends WebTestBase { $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")]');