Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By alangmuir on
Good afternoon all,
I'm trying to write a function related to accessibility, specifically adding an aria-label='whatever label is' attribute to 'section' tags on the site.
With the following, it applies to all blocks (div, nav, etc.). What I want (at least for now) is to target 'section' tags ONLY, but I can't for the life of me find how to target them specifically:
function mytheme_preprocess_block(&$variables) {
$block = $variables['block'];
/* this adds the attribute, but many blocks do not have titles so it needs work */
if (!empty($block->title)) {
$variables['attributes_array']['aria-label'][] = $block->title;
} else {
$variables['attributes_array']['aria-label'][] = 'This is a label';
}
}
Any help is greatly appreciated.