diff --git a/core/core.services.yml b/core/core.services.yml index 183f7e2..1c31e28 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -560,8 +560,8 @@ services: tags: - { name: event_subscriber } arguments: ['@settings'] - ajax_response_subscriber: - class: Drupal\Core\EventSubscriber\AjaxResponseSubscriber + ajax_subscriber: + class: Drupal\Core\EventSubscriber\AjaxSubscriber tags: - { name: event_subscriber } route_enhancer.param_conversion: diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php index 04ce629..106e9d6 100644 --- a/core/lib/Drupal/Component/Utility/Html.php +++ b/core/lib/Drupal/Component/Utility/Html.php @@ -35,6 +35,13 @@ class Html { protected static $seenIds; /** + * Contains the current AJAX HTML IDs. + * + * @var array + */ + protected static $ajaxHTMLIDs; + + /** * Prepares a string for use as a valid class name. * * Do not pass one string containing multiple classes as they will be @@ -95,6 +102,16 @@ public static function cleanCssIdentifier($identifier, array $filter = array( } /** + * Sets the AJAX HTML IDs. + * + * @param array $ajax_html_ids + * The AJAX HTML IDs, probably coming from the current request. + */ + public static function setAjaxHtmlIds(array $ajax_html_ids = []) { + static::$ajaxHTMLIDs = $ajax_html_ids; + } + + /** * Prepares a string for use as a valid HTML ID and guarantees uniqueness. * * This function ensures that each passed HTML ID value only exists once on @@ -137,7 +154,7 @@ public static function getUniqueId($id) { // normally not recommended as it could open up security risks, but // because the raw POST data is cast to a number before being returned by // this function, this usage is safe. - if (empty($_POST['ajax_html_ids'])) { + if (empty(static::$ajaxHTMLIDs)) { static::$seenIdsInit = array(); } else { @@ -146,7 +163,7 @@ public static function getUniqueId($id) { // that requested id. $_POST['ajax_html_ids'] contains the ids as they // were returned by this function, potentially with the appended // counter, so we parse that to reconstruct the $seen_ids array. - $ajax_html_ids = explode(' ', $_POST['ajax_html_ids']); + $ajax_html_ids = explode(' ', static::$ajaxHTMLIDs); foreach ($ajax_html_ids as $seen_id) { // We rely on '--' being used solely for separating a base id from the // counter, which this function ensures when returning an id. diff --git a/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php index cf4dca1..3dd052e 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php @@ -2,18 +2,29 @@ /** * @file - * Contains \Drupal\Core\EventSubscriber\AjaxResponseSubscriber. + * Contains \Drupal\Core\EventSubscriber\AjaxSubscriber. */ namespace Drupal\Core\EventSubscriber; +use Drupal\Component\Utility\Html; use Drupal\Core\Ajax\AjaxResponse; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; -class AjaxResponseSubscriber implements EventSubscriberInterface { +class AjaxSubscriber implements EventSubscriberInterface { + + /** + * Sets the AJAX HTML IDs from the current request. + * + * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * The response event, which contains the current request. + */ + public function onRequest(GetResponseEvent $event) { + Html::setAjaxHtmlIds($event->getRequest()->request->get('ajax_html_ids', [])); + } /** * Renders the ajax commands right before preparing the result. @@ -33,6 +44,7 @@ public function onResponse(FilterResponseEvent $event) { */ public static function getSubscribedEvents() { $events[KernelEvents::RESPONSE][] = array('onResponse', -100); + $events[KernelEvents::REQUEST][] = array('onREquest', 50); return $events; } diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index bb88b0c..e1bcbe3 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -1136,31 +1136,6 @@ protected function getElementInfo($type) { } /** - * Wraps drupal_html_class(). - * - * @return string - */ - protected function drupalHtmlClass($class) { - return drupal_html_class($class); - } - - /** - * Wraps drupal_html_id(). - * - * @return string - */ - protected function drupalHtmlId($id) { - return drupal_html_id($id); - } - - /** - * Wraps drupal_static_reset(). - */ - protected function drupalStaticReset($name = NULL) { - drupal_static_reset($name); - } - - /** * Gets the current active user. * * @return \Drupal\Core\Session\AccountInterface diff --git a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php index 108f35a..1f09bbe 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormTestBase.php +++ b/core/tests/Drupal/Tests/Core/Form/FormTestBase.php @@ -7,7 +7,8 @@ namespace Drupal\Tests\Core\Form { -use Drupal\Core\Form\FormBuilder; + use Drupal\Component\Utility\Html; + use Drupal\Core\Form\FormBuilder; use Drupal\Core\Form\FormInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; @@ -193,7 +194,7 @@ protected function setUp() { * {@inheritdoc} */ protected function tearDown() { - $this->formBuilder->drupalStaticReset(); + Html::resetSeenIds(); } /**