diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php index e27a073..c6e78ad 100644 --- a/core/lib/Drupal/Core/Render/Element/RenderElement.php +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php @@ -237,8 +237,13 @@ public static function preRenderAjaxForm($element) { // One such substantial difference is form elements that use // #ajax['callback'] for determining which part of the form needs // re-rendering. For that, we have a special 'system/ajax' route. + $url = NULL; + if (!isset($settings['url']) && isset($settings['callback'])) { + $url = Url::fromRouteMatch(\Drupal::routeMatch()); + $settings['options']['query']['magic_string_to_be_renamed'] = 'drupal_ajax_post'; + } $settings += array( - 'url' => isset($settings['callback']) ? Url::fromRouteMatch(\Drupal::routeMatch()) : NULL, + 'url' => $url, 'options' => array(), 'dialogType' => 'ajax', ); diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 62db411..4cafb3e 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -853,19 +853,3 @@ function _search_find_match_with_simplify($key, $text, $boundary, $langcode = NU // If we get here, we couldn't find a match. return NULL; } - -/** - * Implements hook_form_FORM_ID_alter() for the search_block_form form. - * - * Since the exposed form is a GET form, we don't want it to send the form - * tokens. However, you cannot make this happen in the form builder function - * itself, because the tokens are added to the form after the builder function - * is called. So, we have to do it in a form_alter. - * - * @see \Drupal\search\Form\SearchBlockForm - */ -function search_form_search_block_form_alter(&$form, FormStateInterface $form_state) { - $form['form_build_id']['#access'] = FALSE; - $form['form_token']['#access'] = FALSE; - $form['form_id']['#access'] = FALSE; -} diff --git a/core/modules/search/src/Form/SearchBlockForm.php b/core/modules/search/src/Form/SearchBlockForm.php index e154863..b07557e 100644 --- a/core/modules/search/src/Form/SearchBlockForm.php +++ b/core/modules/search/src/Form/SearchBlockForm.php @@ -64,6 +64,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { } $route = 'search.view_' . $entity_id; + $form['#pre_render'][] = [get_class(), 'removeFormTokens']; $form['#action'] = $this->url($route); $form['#token'] = FALSE; $form['#method'] = 'get'; @@ -89,6 +90,22 @@ public function buildForm(array $form, FormStateInterface $form_state) { } /** + * Remove form tokens from the search form. + * + * Since the exposed form is a GET form, we don't want it to send the form + * tokens. However, you cannot make this happen in the form builder function + * itself, because the tokens are added to the form after the builder function + * is called. So, we have to do it in a #pre_render. + */ + public static function removeFormTokens($element) { + $element['form_build_key']['#access'] = FALSE; + $element['form_build_id']['#access'] = FALSE; + $element['form_token']['#access'] = FALSE; + $element['form_id']['#access'] = FALSE; + return $element; + } + + /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { diff --git a/core/modules/search/src/Tests/SearchBlockTest.php b/core/modules/search/src/Tests/SearchBlockTest.php index bf18056..e2b7265 100644 --- a/core/modules/search/src/Tests/SearchBlockTest.php +++ b/core/modules/search/src/Tests/SearchBlockTest.php @@ -69,11 +69,7 @@ public function testSearchFormBlock() { /** @var $search_page_repository \Drupal\search\SearchPageRepositoryInterface */ $search_page_repository = \Drupal::service('search.search_page_repository'); $entity_id = $search_page_repository->getDefaultSearchPage(); - $this->assertEqual( - $this->getUrl(), - \Drupal::url('search.view_' . $entity_id, array(), array('query' => array('keys' => $terms['keys']), 'absolute' => TRUE)), - 'Submitted to correct url.' - ); + $this->assertUrl(\Drupal::url('search.view_' . $entity_id, array(), array('query' => array('keys' => $terms['keys']), 'absolute' => TRUE))); // Test an empty search via the block form, from the front page. $terms = array('keys' => ''); @@ -83,11 +79,7 @@ public function testSearchFormBlock() { // Confirm that the user is redirected to the search page, when form is // submitted empty. - $this->assertEqual( - $this->getUrl(), - \Drupal::url('search.view_' . $entity_id, array(), array('query' => array('keys' => ''), 'absolute' => TRUE)), - 'Redirected to correct url.' - ); + $this->assertUrl(\Drupal::url('search.view_' . $entity_id, array(), array('query' => array('keys' => ''), 'absolute' => TRUE))); // Test that after entering a too-short keyword in the form, you can then // search again with a longer keyword. First test using the block form. diff --git a/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php b/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php index a99ea45..6466014 100644 --- a/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php +++ b/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php @@ -178,9 +178,7 @@ function testSearchModuleDisabling() { // to this plugin's search results page. $terms = array('keys' => $info['keys']); $this->submitGetForm('node', $terms, t('Search')); - $current = $this->getURL(); - $expected = \Drupal::url('search.view_' . $entity->id(), array(), array('query' => array('keys' => $info['keys']), 'absolute' => TRUE)); - $this->assertEqual( $current, $expected, 'Block redirected to right search page'); + $this->assertUrl(\Drupal::url('search.view_' . $entity->id(), array(), array('query' => array('keys' => $info['keys']), 'absolute' => TRUE))); // Try an invalid search path, which should 404. $this->drupalGet('search/not_a_plugin_path'); diff --git a/core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php b/core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php index 0fdd9af..e561fe6 100644 --- a/core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php +++ b/core/modules/system/src/Tests/Entity/Element/EntityAutocompleteElementFormTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Entity\Element; +use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Entity\Element\EntityAutocomplete; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormInterface; @@ -23,6 +24,8 @@ */ class EntityAutocompleteElementFormTest extends EntityUnitTestBase implements FormInterface { + use DependencySerializationTrait; + /** * User for testing. * diff --git a/core/modules/views/src/Form/ViewsExposedForm.php b/core/modules/views/src/Form/ViewsExposedForm.php index 29300af..358ac02 100644 --- a/core/modules/views/src/Form/ViewsExposedForm.php +++ b/core/modules/views/src/Form/ViewsExposedForm.php @@ -164,7 +164,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $view->exposed_data = $form_state->getValues(); $view->exposed_raw_input = []; - $exclude = array('submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', 'reset'); + $exclude = array('submit', 'form_build_id', 'form_build_key', 'form_id', 'form_token', 'exposed_form_plugin', 'reset'); /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */ $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form'); $exposed_form_plugin->exposedFormSubmit($form, $form_state, $exclude);