Skip to content

allowing ID randomization but keeping the stable selector #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions localgov_base.theme
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,23 @@ function localgov_base_views_pre_render(ViewExecutable $view): void {
/**
* Implements hook_preprocess_container().
*/
function localgov_base_preprocess_container(&$variables): void {

// Randomize form container IDs for accessibility.
function localgov_base_preprocess_container(&$variables): void {
// See https://www.drupal.org/project/drupal/issues/1852090
// Ensure the container has an ID.
if (isset($variables['element']['#id'])) {
$id = $variables['element']['#id'];
if ($id === 'edit-actions') {
$id .= '--' . Crypt::randomBytesBase64(8);
$original_id = $variables['element']['#id'];

// Only modify 'edit-actions' to prevent affecting other elements.
if ($original_id === 'edit-actions') {
$random_suffix = Crypt::randomBytesBase64(8);
$random_id = $original_id . '--' . $random_suffix;

// Keep a stable selector but allow ID randomization.
$variables['attributes']['id'] = $random_id;
$variables['attributes']['data-drupal-selector'] = $original_id;
$variables['element']['#attributes']['data-drupal-selector'] = $original_id;
$variables['element']['#id'] = $random_id;
}
$variables['attributes']['id'] = $id;
$variables['attributes']['data-drupal-selector'] = $id;
$variables['element']['#attributes']['data-drupal-selector'] = $id;
$variables['element']['#id'] = $id;
}
}

Expand Down
Loading