diff --git a/core/core.api.php b/core/core.api.php index b1fb3a2dd2..0a92352654 100644 --- a/core/core.api.php +++ b/core/core.api.php @@ -2339,20 +2339,21 @@ function hook_validation_constraint_alter(array &$definitions) { * an array. Here are the details of its elements, all of which are optional: * - callback: The callback to invoke to handle the server side of the * Ajax event. More information on callbacks is below in @ref sub_callback. - * - wrapper: The HTML 'id' attribute of the area where the content returned by - * the callback should be placed. Note that callbacks have a choice of - * returning content or JavaScript commands; 'wrapper' is used for content - * returns. + * - wrapper_selector: The CSS selector of the area where the content returned + * by the callback should be placed. Note that callbacks have a choice of + * returning content or JavaScript commands; 'wrapper_selector' is used for + * content returns. * - method: The jQuery method for placing the new content (used with - * 'wrapper'). Valid options are 'replaceWith' (default), 'append', 'prepend', - * 'before', 'after', or 'html'. See + * 'wrapper_selector'). Valid options are 'replaceWith' (default), 'append', + * 'prepend', 'before', 'after', or 'html'. See * http://api.jquery.com/category/manipulation/ for more information on these * methods. * - effect: The jQuery effect to use when placing the new HTML (used with - * 'wrapper'). Valid options are 'none' (default), 'slide', or 'fade'. - * - speed: The effect speed to use (used with 'effect' and 'wrapper'). Valid - * options are 'slow' (default), 'fast', or the number of milliseconds the - * effect should run. + * 'wrapper_selector'). Valid options are 'none' (default), 'slide', or + * 'fade'. + * - speed: The effect speed to use (used with 'effect' and 'wrapper_selector'). + * Valid options are 'slow' (default), 'fast', or the number of milliseconds + * the effect should run. * - event: The JavaScript event to respond to. This is selected automatically * for the type of form element; provide a value to override the default. * - prevent: A JavaScript event to prevent when the event is triggered. For @@ -2392,8 +2393,8 @@ function hook_validation_constraint_alter(array &$definitions) { * Once you have processed the input, you have your choice of returning HTML * markup or a set of Ajax commands. If you choose to return HTML markup, you * can return it as a string or a renderable array, and it will be placed in - * the defined 'wrapper' element (see documentation above in @ref sub_form). - * In addition, any messages returned by + * the defined 'wrapper_selector' element (see documentation above in + * @ref sub_form). In addition, any messages returned by * \Drupal\Core\Messenger\Messenger::all(), themed as in * status-messages.html.twig, will be prepended. * @@ -2417,8 +2418,8 @@ function hook_validation_constraint_alter(array &$definitions) { * invokes an arbitrary jQuery command. * * As noted above, status messages are prepended automatically if you use the - * 'wrapper' method and return HTML markup. This is not the case if you return - * commands, but if you would like to show status messages, you can add + * 'wrapper_selector' method and return HTML markup. This is not the case if you + * return commands, but if you would like to show status messages, you can add * @code * array('#type' => 'status_messages') * @endcode diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 43cd540bb2..62111412f3 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -666,7 +666,7 @@ public static function fieldSettingsAjaxProcessElement(&$element, $main_form) { if (!empty($element['#ajax'])) { $element['#ajax'] = [ 'callback' => [static::class, 'settingsAjax'], - 'wrapper' => $main_form['#id'], + 'wrapper_selector' => '#' . $main_form['#id'], 'element' => $main_form['#array_parents'], ]; } diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index 39daf849aa..9fe6bdeb74 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -263,7 +263,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f '#submit' => [[static::class, 'addMoreSubmit']], '#ajax' => [ 'callback' => [static::class, 'addMoreAjax'], - 'wrapper' => $wrapper_id, + 'wrapper_selector' => '#' . $wrapper_id, 'effect' => 'fade', ], ]; diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php index 627b041e04..389346ca42 100644 --- a/core/lib/Drupal/Core/Render/Element/RenderElement.php +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php @@ -246,7 +246,7 @@ public static function processAjaxForm(&$element, FormStateInterface $form_state * - #ajax['url'] * - #ajax['callback'] * - #ajax['options'] - * - #ajax['wrapper'] + * - #ajax['wrapper_selector'] * - #ajax['parameters'] * - #ajax['effect'] * - #ajax['accepts'] @@ -331,6 +331,13 @@ public static function preRenderAjaxForm($element) { $settings = $element['#ajax']; + // @todo Remove support for 'wrapper' and solely use 'wrapper_selector'. + if (isset($settings['wrapper'])) { + @trigger_error('Use of "wrapper" is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Use "wrapper_selector" instead and prefix the value with either "#" for IDs or "." for classes.', E_USER_DEPRECATED); + $settings['wrapper_selector'] = '#' . $settings['wrapper']; + unset($settings['wrapper']); + } + // Assign default settings. When 'url' is set to NULL, ajax.js submits the // Ajax request to the same URL as the form or link destination is for // someone with JavaScript disabled. This is generally preferred as a way to diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 31959e6414..94007e6d1a 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -410,11 +410,12 @@ // - Call this 'selector'. // - Include the '#' for ID-based selectors. // - Support non-ID-based selectors. - if (this.wrapper) { + if (this.wrapper && !this.wrapper_selector) { /** * @type {string} */ - this.wrapper = `#${this.wrapper}`; + this.wrapper_selector = '#'.concat(this.wrapper); + this.wrapper = this.wrapper_selector; } /** @@ -1156,7 +1157,7 @@ this.progress.object.stopMonitoring(); } // Undo hide. - $(this.wrapper).show(); + $(this.wrapper_selector).show(); // Re-enable the element. $(this.element).prop('disabled', false); // Reattach behaviors, if they were detached in beforeSerialize(), and the @@ -1274,7 +1275,7 @@ // our presets. const $wrapper = response.selector ? $(response.selector) - : $(ajax.wrapper); + : $(ajax.wrapper_selector); const method = response.method || ajax.method; const effect = ajax.getEffect(response); diff --git a/core/misc/dialog/dialog.ajax.js b/core/misc/dialog/dialog.ajax.js index dd62949273..4a4794e204 100644 --- a/core/misc/dialog/dialog.ajax.js +++ b/core/misc/dialog/dialog.ajax.js @@ -116,8 +116,8 @@ ).appendTo('body'); } // Set up the wrapper, if there isn't one. - if (!ajax.wrapper) { - ajax.wrapper = $dialog.attr('id'); + if (!ajax.selector) { + ajax.selector = `#${$dialog.attr('id')}`; } // Use the ajax.js insert command to populate the dialog contents. diff --git a/core/modules/action/tests/action_form_ajax_test/src/Plugin/Action/ActionAjaxTest.php b/core/modules/action/tests/action_form_ajax_test/src/Plugin/Action/ActionAjaxTest.php index 8afc98cf54..02075a1d28 100644 --- a/core/modules/action/tests/action_form_ajax_test/src/Plugin/Action/ActionAjaxTest.php +++ b/core/modules/action/tests/action_form_ajax_test/src/Plugin/Action/ActionAjaxTest.php @@ -50,7 +50,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#type' => 'checkbox', '#title' => $this->t('Are we having a party?'), '#ajax' => [ - 'wrapper' => 'party-container', + 'wrapper_selector' => '#party-container', 'callback' => [$this, 'partyCallback'], ], '#default_value' => $having_a_party, diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 8eb188020e..87ba3658c5 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -165,7 +165,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $theme, '#ajax' => [ 'callback' => '::themeSwitch', - 'wrapper' => 'edit-block-region-wrapper', + 'wrapper_selector' => '#edit-block-region-wrapper', ], ]; } diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php index 06dd84fdce..28f241e8b6 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -283,7 +283,7 @@ public function addFormElements(array $form, FormStateInterface $form_state, Nod '#attributes' => ['class' => ['book-title-select']], '#ajax' => [ 'callback' => 'book_form_update', - 'wrapper' => 'edit-book-plid-wrapper', + 'wrapper_selector' => '#edit-book-plid-wrapper', 'effect' => 'fade', 'speed' => 'fast', ], diff --git a/core/modules/config/src/Form/ConfigSingleExportForm.php b/core/modules/config/src/Form/ConfigSingleExportForm.php index c31daca71f..2d7e13315d 100644 --- a/core/modules/config/src/Form/ConfigSingleExportForm.php +++ b/core/modules/config/src/Form/ConfigSingleExportForm.php @@ -97,7 +97,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $config_t '#default_value' => $config_type, '#ajax' => [ 'callback' => '::updateConfigurationType', - 'wrapper' => 'js-config-form-wrapper', + 'wrapper_selector' => '#edit-config-type-wrapper', ], ]; $default_type = $form_state->getValue('config_type', $config_type); @@ -110,7 +110,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $config_t '#suffix' => '', '#ajax' => [ 'callback' => '::updateExport', - 'wrapper' => 'edit-export-wrapper', + 'wrapper_selector' => '#edit-export-wrapper', ], ]; diff --git a/core/modules/config/tests/config_test/src/ConfigTestForm.php b/core/modules/config/tests/config_test/src/ConfigTestForm.php index 6f1eac0b99..4127ce3d37 100644 --- a/core/modules/config/tests/config_test/src/ConfigTestForm.php +++ b/core/modules/config/tests/config_test/src/ConfigTestForm.php @@ -74,7 +74,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $size, '#ajax' => [ 'callback' => '::updateSize', - 'wrapper' => 'size-wrapper', + 'wrapper_selector' => '#size-wrapper', ], ]; $form['size_wrapper']['size_submit'] = [ diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index a4d86712fb..a77e95d53c 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -119,7 +119,7 @@ function editor_form_filter_format_form_alter(&$form, FormStateInterface $form_s '#ajax' => [ 'trigger_as' => ['name' => 'editor_configure'], 'callback' => 'editor_form_filter_admin_form_ajax', - 'wrapper' => 'editor-settings-wrapper', + 'wrapper_selector' => '#editor-settings-wrapper', ], '#weight' => -10, ]; @@ -131,7 +131,7 @@ function editor_form_filter_format_form_alter(&$form, FormStateInterface $form_s '#submit' => ['editor_form_filter_admin_format_editor_configure'], '#ajax' => [ 'callback' => 'editor_form_filter_admin_form_ajax', - 'wrapper' => 'editor-settings-wrapper', + 'wrapper_selector' => '#editor-settings-wrapper', ], '#weight' => -10, '#attributes' => ['class' => ['js-hide']], diff --git a/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php b/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php index 170bcd093a..8e2f2e3fb4 100644 --- a/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php +++ b/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php @@ -65,7 +65,7 @@ public function form(array $form, FormStateInterface $form_state) { '#default_value' => $layout_plugin->getPluginId(), '#ajax' => [ 'callback' => '::settingsAjax', - 'wrapper' => 'field-layout-settings-wrapper', + 'wrapper_selector' => '#field-layout-settings-wrapper', 'trigger_as' => ['name' => 'field_layout_change'], ], ]; @@ -77,7 +77,7 @@ public function form(array $form, FormStateInterface $form_state) { '#attributes' => ['class' => ['js-hide']], '#ajax' => [ 'callback' => '::settingsAjax', - 'wrapper' => 'field-layout-settings-wrapper', + 'wrapper_selector' => '#field-layout-settings-wrapper', ], ]; diff --git a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php index 6cc3f6bd05..6ba9446db0 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php @@ -258,7 +258,7 @@ public function form(array $form, FormStateInterface $form_state) { '#submit' => ['::multistepSubmit'], '#ajax' => [ 'callback' => '::multistepAjax', - 'wrapper' => 'field-display-overview-wrapper', + 'wrapper_selector' => '#field-display-overview-wrapper', 'effect' => 'fade', // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc // spinners will be added manually by the client-side script. @@ -370,7 +370,7 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr '#submit' => ['::multistepSubmit'], '#ajax' => [ 'callback' => '::multistepAjax', - 'wrapper' => 'field-display-overview-wrapper', + 'wrapper_selector' => '#field-display-overview-wrapper', 'effect' => 'fade', ], '#field_name' => $field_name, diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php index 8672aba730..9288b96039 100644 --- a/core/modules/file/src/Element/ManagedFile.php +++ b/core/modules/file/src/Element/ManagedFile.php @@ -233,7 +233,7 @@ public static function processManagedFile(&$element, FormStateInterface $form_st 'element_parents' => implode('/', $element['#array_parents']), ], ], - 'wrapper' => $ajax_wrapper_id, + 'wrapper_selector' => '#' . $ajax_wrapper_id, 'effect' => 'fade', 'progress' => [ 'type' => $element['#progress_indicator'], diff --git a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php index 70bd4a7166..34c59eb429 100644 --- a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php +++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php @@ -431,11 +431,11 @@ public static function process($element, FormStateInterface $form_state, $form) ], ]; $field_element = NestedArray::getValue($form, $parents); - $new_wrapper = $field_element['#id'] . '-ajax-wrapper'; + $new_wrapper = '#' . $field_element['#id'] . '-ajax-wrapper'; foreach (Element::children($element) as $key) { if (isset($element[$key]['#ajax'])) { $element[$key]['#ajax']['options'] = $new_options; - $element[$key]['#ajax']['wrapper'] = $new_wrapper; + $element[$key]['#ajax']['wrapper_selector'] = $new_wrapper; } } unset($element['#prefix'], $element['#suffix']); diff --git a/core/modules/layout_builder/src/Form/MoveBlockForm.php b/core/modules/layout_builder/src/Form/MoveBlockForm.php index beb3aa9757..70e23158a0 100644 --- a/core/modules/layout_builder/src/Form/MoveBlockForm.php +++ b/core/modules/layout_builder/src/Form/MoveBlockForm.php @@ -149,7 +149,7 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt '#title' => $this->t('Region'), '#default_value' => "$selected_delta:$selected_region", '#ajax' => [ - 'wrapper' => 'layout-builder-components-table', + 'wrapper_selector' => '#layout-builder-components-table', 'callback' => '::getComponentsWrapper', ], ]; diff --git a/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php index 1cdb92f917..c51dd9f616 100644 --- a/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php +++ b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php @@ -267,7 +267,7 @@ public function blockForm($form, FormStateInterface $form_state) { '#default_value' => $config['formatter']['type'], '#ajax' => [ 'callback' => [static::class, 'formatterSettingsAjaxCallback'], - 'wrapper' => 'formatter-settings-wrapper', + 'wrapper_selector' => '#formatter-settings-wrapper', ], ]; diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Block/TestAjaxBlock.php b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Block/TestAjaxBlock.php index 1f1eeb0d7f..440921b0b3 100644 --- a/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Block/TestAjaxBlock.php +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Block/TestAjaxBlock.php @@ -32,7 +32,7 @@ public function blockForm($form, FormStateInterface $form_state) { '@time' => time(), ]), '#ajax' => [ - 'wrapper' => 'test-ajax-wrapper', + 'wrapper_selector' => '#test-ajax-wrapper', 'callback' => [$this, 'ajaxCallback'], ], ]; diff --git a/core/modules/media_library/src/Form/AddFormBase.php b/core/modules/media_library/src/Form/AddFormBase.php index 83742a3a17..149923d4a7 100644 --- a/core/modules/media_library/src/Form/AddFormBase.php +++ b/core/modules/media_library/src/Form/AddFormBase.php @@ -293,7 +293,7 @@ protected function buildEntityFormElement(MediaInterface $media, array $form, Fo ], '#ajax' => [ 'callback' => '::updateFormCallback', - 'wrapper' => 'media-library-add-form-wrapper', + 'wrapper_selector' => '#media-library-add-form-wrapper', 'message' => $this->t('Removing @label.', ['@label' => $media->getName()]), ], '#submit' => ['::removeButtonSubmit'], @@ -466,7 +466,7 @@ protected function buildActions(array $form, FormStateInterface $form_state) { '#value' => $this->t('Save'), '#ajax' => [ 'callback' => '::updateLibrary', - 'wrapper' => 'media-library-add-form-wrapper', + 'wrapper_selector' => '#media-library-add-form-wrapper', ], ], ]; @@ -477,7 +477,7 @@ protected function buildActions(array $form, FormStateInterface $form_state) { '#value' => $this->t('Save and insert'), '#ajax' => [ 'callback' => '::updateWidget', - 'wrapper' => 'media-library-add-form-wrapper', + 'wrapper_selector' => '#media-library-add-form-wrapper', ], ]; } @@ -588,7 +588,7 @@ public function removeButtonSubmit(array $form, FormStateInterface $form_state) */ public function updateFormCallback(array &$form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); - $wrapper_id = $triggering_element['#ajax']['wrapper']; + $wrapper_selector = $triggering_element['#ajax']['wrapper_selector']; $added_media = $form_state->get('media'); $response = new AjaxResponse(); @@ -615,7 +615,7 @@ public function updateFormCallback(array &$form, FormStateInterface $form_state) // the next media item. If the last list item is removed, shift focus to // the previous item. else { - $response->addCommand(new ReplaceCommand("#$wrapper_id", $form)); + $response->addCommand(new ReplaceCommand($wrapper_selector, $form)); // Find the delta of the next media item. If there is no item with a // bigger delta, we automatically use the delta of the previous item and @@ -633,7 +633,7 @@ public function updateFormCallback(array &$form, FormStateInterface $form_state) } // Update the form and shift focus to the added media items. else { - $response->addCommand(new ReplaceCommand("#$wrapper_id", $form)); + $response->addCommand(new ReplaceCommand($wrapper_selector, $form)); $response->addCommand(new InvokeCommand('.js-media-library-add-form-added-media', 'focus')); } diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php index b7cbe7c6a6..a964ceb172 100644 --- a/core/modules/media_library/src/Form/FileUploadForm.php +++ b/core/modules/media_library/src/Form/FileUploadForm.php @@ -237,7 +237,7 @@ public function processUploadElement(array $element, FormStateInterface $form_st ]; $element['upload_button']['#ajax'] = [ 'callback' => '::updateFormCallback', - 'wrapper' => 'media-library-wrapper', + 'wrapper_selector' => '#media-library-wrapper', // Add a fixed URL to post the form since AJAX forms are automatically // posted to instead of $form['#action']. // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115 diff --git a/core/modules/media_library/src/Form/OEmbedForm.php b/core/modules/media_library/src/Form/OEmbedForm.php index 8b70c89691..b638800124 100644 --- a/core/modules/media_library/src/Form/OEmbedForm.php +++ b/core/modules/media_library/src/Form/OEmbedForm.php @@ -126,7 +126,7 @@ protected function buildInputElement(array $form, FormStateInterface $form_state // @todo Move validation in https://www.drupal.org/node/2988215 '#ajax' => [ 'callback' => '::updateFormCallback', - 'wrapper' => 'media-library-wrapper', + 'wrapper_selector' => '#media-library-wrapper', // Add a fixed URL to post the form since AJAX forms are automatically // posted to instead of $form['#action']. // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115 diff --git a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php index 4bd939f753..67169175ee 100644 --- a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php +++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php @@ -433,7 +433,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ], '#ajax' => [ 'callback' => [static::class, 'updateWidget'], - 'wrapper' => $wrapper_id, + 'wrapper_selector' => '#' . $wrapper_id, 'progress' => [ 'type' => 'throbber', 'message' => $media_item->access('view label') ? $this->t('Removing @label.', ['@label' => $media_item->label()]) : $this->t('Removing media.'), @@ -574,7 +574,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#name' => $field_name . '-media-library-update' . $id_suffix, '#ajax' => [ 'callback' => [static::class, 'updateWidget'], - 'wrapper' => $wrapper_id, + 'wrapper_selector' => '#' . $wrapper_id, 'progress' => [ 'type' => 'throbber', 'message' => $this->t('Adding selection.'), @@ -693,7 +693,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface */ public static function updateWidget(array $form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); - $wrapper_id = $triggering_element['#ajax']['wrapper']; + $wrapper_selector = $triggering_element['#ajax']['wrapper_selector']; // This callback is either invoked from the remove button or the update // button, which have different nesting levels. @@ -721,7 +721,7 @@ public static function updateWidget(array $form, FormStateInterface $form_state) } $response = new AjaxResponse(); - $response->addCommand(new ReplaceCommand("#$wrapper_id", $element)); + $response->addCommand(new ReplaceCommand($wrapper_selector, $element)); $response->addCommand(new AnnounceCommand($announcement)); // When the remove button is clicked, shift focus to the next remove button. @@ -746,7 +746,7 @@ public static function updateWidget(array $form, FormStateInterface $form_state) break; } } - $response->addCommand(new InvokeCommand("#$wrapper_id [data-media-library-item-delta=$delta_to_focus]", 'focus')); + $response->addCommand(new InvokeCommand("$wrapper_selector [data-media-library-item-delta=$delta_to_focus]", 'focus')); } // Shift focus to the open button if the user removed the last selected // item, or when the user has added items to the selection and is allowed to @@ -757,7 +757,7 @@ public static function updateWidget(array $form, FormStateInterface $form_state) // here. // @see Drupal.behaviors.MediaLibraryWidgetDisableButton elseif ($removed_last || (!$is_remove_button && !isset($element['open_button']['#attributes']['data-disabled-focus']))) { - $response->addCommand(new InvokeCommand("#$wrapper_id .js-media-library-open-button", 'focus')); + $response->addCommand(new InvokeCommand("$wrapper_selector .js-media-library-open-button", 'focus')); } return $response; diff --git a/core/modules/responsive_image/src/ResponsiveImageStyleForm.php b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php index dbecba4c70..92f6d7f50d 100644 --- a/core/modules/responsive_image/src/ResponsiveImageStyleForm.php +++ b/core/modules/responsive_image/src/ResponsiveImageStyleForm.php @@ -101,7 +101,7 @@ public function form(array $form, FormStateInterface $form_state) { '#description' => $description, '#ajax' => [ 'callback' => '::breakpointMappingFormAjax', - 'wrapper' => 'responsive-image-style-breakpoints-wrapper', + 'wrapper_selector' => '#responsive-image-style-breakpoints-wrapper', ], ]; diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestAjaxElementsForm.php b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestAjaxElementsForm.php index 41a9b6d92a..c657399ea8 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestAjaxElementsForm.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestAjaxElementsForm.php @@ -39,7 +39,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#type' => 'datetime', '#ajax' => [ 'callback' => [$callback_object, 'datetimeCallback'], - 'wrapper' => 'ajax_datetime_value', + 'wrapper_selector' => '#ajax_datetime_value', ], ]; diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestLazyLoadForm.php b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestLazyLoadForm.php index 7dddf877c4..e9433a49d9 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestLazyLoadForm.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestLazyLoadForm.php @@ -37,7 +37,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#type' => 'submit', '#value' => $this->t('Submit'), '#ajax' => [ - 'wrapper' => 'ajax-forms-test-lazy-load-ajax-wrapper', + 'wrapper_selector' => '#ajax-forms-test-lazy-load-ajax-wrapper', 'callback' => 'ajax_forms_test_lazy_load_form_ajax', ], '#prefix' => '
', diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php index 35e0b08e20..32d8514fdc 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php @@ -86,7 +86,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#title' => $this->t('AJAX checkbox in a group'), '#ajax' => [ 'callback' => [$object, 'checkboxGroupCallback'], - 'wrapper' => 'checkbox-wrapper', + 'wrapper_selector' => '#checkbox-wrapper', ], ], 'nested_group' => [ @@ -100,7 +100,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#title' => $this->t('AJAX checkbox in a nested group'), '#ajax' => [ 'callback' => [$object, 'checkboxGroupCallback'], - 'wrapper' => 'checkbox-wrapper', + 'wrapper_selector' => '#checkbox-wrapper', ], ], ]; diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestValidationForm.php b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestValidationForm.php index ef6ddaf736..6f42facde6 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestValidationForm.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestValidationForm.php @@ -30,7 +30,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $form_state->getValue('drivertext', ''), '#ajax' => [ 'callback' => 'ajax_forms_test_validation_form_callback', - 'wrapper' => 'message_area', + 'wrapper_selector' => '#message_area', 'method' => 'replace', ], '#suffix' => '
', @@ -43,7 +43,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $form_state->getValue('drivernumber', ''), '#ajax' => [ 'callback' => 'ajax_forms_test_validation_number_form_callback', - 'wrapper' => 'message_area_number', + 'wrapper_selector' => '#message_area_number', 'method' => 'replace', ], '#suffix' => '
', diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php b/core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php index 65e1372d99..3a623bde24 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php @@ -96,7 +96,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], '#ajax' => [ 'callback' => '::updateOptions', - 'wrapper' => 'edit-test1-wrapper', + 'wrapper_selector' => '#edit-test1-wrapper', ], '#prefix' => '
', '#suffix' => '
', diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php index 22cbb7cf45..42f31f8dba 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameValidationForm.php @@ -76,7 +76,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => TRUE, '#ajax' => [ 'callback' => '::buildAjaxSnackConfigureForm', - 'wrapper' => 'snack-config-form', + 'wrapper_selector' => '#snack-config-form', 'method' => 'replace', 'effect' => 'fade', ], diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestTableSelectFormBase.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestTableSelectFormBase.php index 356b1d8ab6..f719a4e171 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestTableSelectFormBase.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestTableSelectFormBase.php @@ -40,7 +40,7 @@ public function tableselectFormBuilder($form, FormStateInterface $form_state, $e '#empty' => t('Empty text.'), '#ajax' => [ 'callback' => 'form_test_tableselect_ajax_callback', - 'wrapper' => 'tableselect-wrapper', + 'wrapper_selector' => '#tableselect-wrapper', ], ]; diff --git a/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php b/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php index 6240ec4e95..2e20094c33 100644 --- a/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php +++ b/core/modules/system/tests/modules/js_webassert_test/src/Form/JsWebAssertTestForm.php @@ -38,7 +38,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'type' => 'throbber', 'message' => NULL, ], - 'wrapper' => 'js_webassert_test_form_wrapper', + 'wrapper_selector' => '#js_webassert_test_form_wrapper', ], ]; // Button to test for the waitForLink() assertion. @@ -52,7 +52,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'type' => 'throbber', 'message' => NULL, ], - 'wrapper' => 'js_webassert_test_form_wrapper', + 'wrapper_selector' => '#js_webassert_test_form_wrapper', ], ]; // Button to test for the waitForField() assertion. @@ -66,7 +66,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'type' => 'throbber', 'message' => NULL, ], - 'wrapper' => 'js_webassert_test_form_wrapper', + 'wrapper_selector' => '#js_webassert_test_form_wrapper', ], ]; // Button to test for the waitForId() assertion. @@ -80,7 +80,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'type' => 'throbber', 'message' => NULL, ], - 'wrapper' => 'js_webassert_test_form_wrapper', + 'wrapper_selector' => '#js_webassert_test_form_wrapper', ], ]; @@ -95,7 +95,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'type' => 'throbber', 'message' => NULL, ], - 'wrapper' => 'js_webassert_test_form_wrapper', + 'wrapper_selector' => '#js_webassert_test_form_wrapper', ], ]; @@ -110,7 +110,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'type' => 'throbber', 'message' => NULL, ], - 'wrapper' => 'js_webassert_test_form_wrapper', + 'wrapper_selector' => '#js_webassert_test_form_wrapper', ], ]; diff --git a/core/modules/views_ui/admin.inc b/core/modules/views_ui/admin.inc index 230dd773ae..6fa7704113 100644 --- a/core/modules/views_ui/admin.inc +++ b/core/modules/views_ui/admin.inc @@ -54,7 +54,7 @@ function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_pa // to request so that the wrapper can be recognized by the AJAX system and // its content can be dynamically updated. So instead, we will keep track of // duplicate IDs (within a single request) on our own, later in this function. - $triggering_element['#ajax']['wrapper'] = 'edit-view-' . implode('-', $refresh_parents) . '-wrapper'; + $triggering_element['#ajax']['wrapper_selector'] = '#edit-view-' . implode('-', $refresh_parents) . '-wrapper'; // Add a submit button for users who do not have JavaScript enabled. It // should be displayed next to the triggering element on the form. @@ -107,11 +107,11 @@ function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_pa // Attach custom data to the triggering element and submit button, so we can // use it in both the process function and AJAX callback. $ajax_data = [ - 'wrapper' => $triggering_element['#ajax']['wrapper'], + 'wrapper_selector' => $triggering_element['#ajax']['wrapper_selector'], 'trigger_key' => $trigger_key, 'refresh_parents' => $refresh_parents, ]; - $seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE; + $seen_ids[$triggering_element['#ajax']['wrapper_selector']] = TRUE; $triggering_element['#views_ui_ajax_data'] = $ajax_data; $wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data; } @@ -164,7 +164,7 @@ function views_ui_add_ajax_wrapper($element, FormStateInterface $form_state) { // The HTML ID that AJAX expects was also stored in a property on the // element, so use that information to insert the wrapper
here. - $id = $element['#views_ui_ajax_data']['wrapper']; + $id = substr($element['#views_ui_ajax_data']['wrapper_selector'], 1); $refresh_element += [ '#prefix' => '', '#suffix' => '', diff --git a/core/modules/views_ui/js/ajax.js b/core/modules/views_ui/js/ajax.js index 398e6d2ed8..0f3a9369ea 100644 --- a/core/modules/views_ui/js/ajax.js +++ b/core/modules/views_ui/js/ajax.js @@ -251,7 +251,7 @@ return true; } - elementSettings.wrapper = 'views-preview-wrapper'; + elementSettings.wrapper_selector = '.views-preview-wrapper'; elementSettings.method = 'replaceWith'; elementSettings.base = link.id; elementSettings.element = link; @@ -279,7 +279,7 @@ return true; } - elementSettings.wrapper = 'views-preview-wrapper'; + elementSettings.wrapper_selector = '.views-preview-wrapper'; elementSettings.method = 'replaceWith'; elementSettings.event = 'click'; elementSettings.base = submit.id; diff --git a/core/modules/views_ui/src/ViewPreviewForm.php b/core/modules/views_ui/src/ViewPreviewForm.php index 82f93ee79a..42e482e5e7 100644 --- a/core/modules/views_ui/src/ViewPreviewForm.php +++ b/core/modules/views_ui/src/ViewPreviewForm.php @@ -18,7 +18,7 @@ class ViewPreviewForm extends ViewFormBase { public function form(array $form, FormStateInterface $form_state) { $view = $this->entity; - $form['#prefix'] = '
'; + $form['#prefix'] = '
'; $form['#suffix'] = '
'; $form['#id'] = 'views-ui-preview-form'; @@ -87,7 +87,7 @@ protected function actions(array $form, FormStateInterface $form_state) { '#id' => 'preview-submit', '#ajax' => [ 'url' => Url::fromRoute('entity.view.preview_form', ['view' => $view->id(), 'display_id' => $this->displayID]), - 'wrapper' => 'views-preview-wrapper', + 'wrapper_selector' => '.views-preview-wrapper', 'event' => 'click', 'progress' => ['type' => 'fullscreen'], 'method' => 'replaceWith', diff --git a/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php b/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php index be8f53c2af..df1e040b85 100644 --- a/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php +++ b/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php @@ -44,33 +44,70 @@ protected function setUp(): void { /** * @covers ::preRenderAjaxForm + * @dataProvider providerTestPreRenderAjaxForm + * + * @group legacy */ - public function testPreRenderAjaxForm() { + public function testPreRenderAjaxForm($element, $expected, $url) { $request = Request::create('/test'); $request->query->set('foo', 'bar'); $this->requestStack->push($request); $prophecy = $this->prophesize('Drupal\Core\Routing\UrlGeneratorInterface'); - $url = '/test?foo=bar&ajax_form=1'; $prophecy->generateFromRoute('', [], ['query' => ['foo' => 'bar', FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]], TRUE) ->willReturn((new GeneratedUrl())->setCacheContexts(['route'])->setGeneratedUrl($url)); $url_generator = $prophecy->reveal(); $this->container->set('url_generator', $url_generator); + $result = RenderElement::preRenderAjaxForm($element); - $element = [ - '#type' => 'select', - '#id' => 'test', - '#ajax' => [ - 'wrapper' => 'foo', + $this->assertTrue($result['#ajax_processed']); + $this->assertSame($expected, $result['#attached']['drupalSettings']['ajax']['test']); + } + + /** + * Provides test data for testPreRenderAjaxForm(). + */ + public function providerTestPreRenderAjaxForm() { + $data = []; + $data['wrapper_selector'] = [ + [ + '#type' => 'select', + '#id' => 'test', + '#ajax' => [ + 'wrapper_selector' => '#foo', + 'callback' => 'test-callback', + ], + ], + [ + 'wrapper_selector' => '#foo', 'callback' => 'test-callback', + 'event' => 'change', + 'url' => '/test?foo=bar&ajax_form=1', + 'dialogType' => 'ajax', ], + '/test?foo=bar&ajax_form=1', ]; - - $element = RenderElement::preRenderAjaxForm($element); - - $this->assertTrue($element['#ajax_processed']); - $this->assertEquals($url, $element['#attached']['drupalSettings']['ajax']['test']['url']); + // @todo Remove support for 'wrapper' and solely use 'wrapper_selector'. + $data['wrapper'] = [ + [ + '#type' => 'select', + '#id' => 'test', + '#ajax' => [ + 'wrapper' => 'foo', + 'callback' => 'test-callback', + ], + ], + [ + 'callback' => 'test-callback', + 'event' => 'change', + 'wrapper_selector' => '#foo', + 'url' => '/test?foo=bar&ajax_form=1', + 'dialogType' => 'ajax', + ], + '/test?foo=bar&ajax_form=1', + ]; + return $data; } /** @@ -93,7 +130,7 @@ public function testPreRenderAjaxFormWithQueryOptions() { '#type' => 'select', '#id' => 'test', '#ajax' => [ - 'wrapper' => 'foo', + 'wrapper_selector' => '#foo', 'callback' => 'test-callback', 'options' => [ 'query' => [