Verified Commit 084bd436 authored by godotislate's avatar godotislate
Browse files

task: #3603310 Use a data attribute to streamline form element wrapper selection

By: fathershawn
By: smustgrave
By: godotislate
By: nitinkumar_7
(cherry picked from commit 23942ea0)
parent eff71394
Loading
Loading
Loading
Loading
Loading
+2 −0
Changes for core/lib/Drupal/Core/Form/FormBuilder.php: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1045,11 +1045,13 @@ public function doBuildForm($form_id, &$element, FormStateInterface &$form_state
      // Provide a selector usable by JavaScript. As the ID is unique, it's not
      // possible to rely on it in JavaScript.
      $element['#attributes']['data-drupal-selector'] = Html::getId($unprocessed_id);
      $element['#wrapper_attributes']['data-drupal-wrapper-selector'] = Html::getId($unprocessed_id);
    }
    else {
      // Provide a selector usable by JavaScript. As the ID is unique, it's not
      // possible to rely on it in JavaScript.
      $element['#attributes']['data-drupal-selector'] = Html::getId($element['#id']);
      $element['#wrapper_attributes']['data-drupal-wrapper-selector'] = Html::getId($element['#id']);
    }

    // Add the aria-describedby attribute to associate the form control with its
+5 −8
Changes for core/modules/config/src/Form/ConfigSingleExportForm.php: 5 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -101,8 +101,8 @@ public function buildForm(array $form, FormStateInterface $form_state, string $c
    (new Htmx())
      ->post($form_url)
      ->onlyMainContent()
      ->select('*:has(>select[name="config_name"])')
      ->target('*:has(>select[name="config_name"])')
      ->select('[data-drupal-wrapper-selector="edit-config-name"]')
      ->target('[data-drupal-wrapper-selector="edit-config-name"]')
      ->swap('outerHTML')
      ->applyTo($form['config_type']);

@@ -119,8 +119,8 @@ public function buildForm(array $form, FormStateInterface $form_state, string $c
    (new Htmx())
      ->post($form_url)
      ->onlyMainContent()
      ->select('[data-export-wrapper]')
      ->target('[data-export-wrapper]')
      ->select('[data-drupal-wrapper-selector="edit-export"]')
      ->target('[data-drupal-wrapper-selector="edit-export"]')
      ->swap('outerHTML')
      ->applyTo($form['config_name']);

@@ -128,9 +128,6 @@ public function buildForm(array $form, FormStateInterface $form_state, string $c
      '#title' => $this->t('Here is your configuration:'),
      '#type' => 'textarea',
      '#rows' => 24,
      '#wrapper_attributes' => [
        'data-export-wrapper' => TRUE,
      ],
    ];

    $pushUrl = FALSE;
@@ -139,7 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state, string $c
      $form = $this->updateConfigurationType($form, $form_state);
      // Also update the empty export element "out of band".
      (new Htmx())
        ->swapOob('outerHTML:[data-export-wrapper]')
        ->swapOob('outerHTML:[data-drupal-wrapper-selector="edit-export"]')
        ->applyTo($form['export'], '#wrapper_attributes');
      $pushUrl = Url::fromRoute('config.export_single', ['config_type' => $default_type, 'config_name' => '']);
    }
+2 −2
Changes for core/modules/config/tests/src/Functional/ConfigDraggableListBuilderTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@

namespace Drupal\Tests\config\Functional;

use Drupal\Component\Utility\Html;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use PHPUnit\Framework\Attributes\Group;
@@ -60,7 +59,8 @@ public function testDraggableList(): void {
    $role->set('label', $role_name)->save();

    $this->drupalGet('admin/people/roles');
    $this->assertSession()->responseContains('<td>' . Html::escape($role_name));
    $roleElementLabel = $this->cssSelect('td[data-drupal-wrapper-selector="edit-entities-role-0-label"]');
    $this->assertEquals($role_name, $roleElementLabel[0]->getText());
  }

}
+5 −0
Changes for core/modules/config/tests/src/FunctionalJavascript/ConfigExportTest.php: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -90,6 +90,11 @@ public function testAjaxOnExportPage(): void {
    $this->assertSession()->fieldValueEquals('export', '');

    // Check that the export is emptied again when selecting a config type.
    // First select a config name to populate the export.
    $page->selectFieldOption('config_name', 'system.site');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertSession()->fieldValueNotEquals('export', '');
    // Then select a config type to empty the export.
    $page->selectFieldOption('config_type', 'Action');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->assertSession()->fieldValueEquals('export', '');