Problem/Motivation
When using inline SVGs, the title attribute is automatically added to the SVG. A non-unique ID is automatically assigned to that title element (e.g., <title id="field_svg_icon__title-0">...</title>) and if there are multiple SVGs on the page, accessibility tests throw an error because there shouldn't be elements with duplicate IDs on a page.
Steps to reproduce
There are probably a number of ways to recreate (maybe even just a multi-value field), but in our case we used inline SVG in a paragraph field and the paragraph could be added more than once inside the same page. Once you add multiple to a page, you see the ID duplication.
Proposed resolution
Generate an ID (and aria-labelledby attribute on the SVG itself) that are somehow unique to the SVG field (maybe populate with the same data you automatically populate the title contents from?).
Otherwise, this has been an excellent module for our project. Thank you!
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 3173878-11.patch | 3.5 KB | jeroent |
Comments
Comment #2
gnikolovskiComment #3
gnikolovskiJust tested this with Paragraphs and I can't replicate it.
The ID is unique for each image (field_svgs__title-0 and field_svgs__title-1), check the screentshoot bellow.
Comment #4
evanmwillhite commentedHmm.. maybe something specific to our setup then. In case it helps, I'll provide a little more detail and a screenshot. Our usage is a content type that has a paragraphs field. That field can reference an "Icon Card" paragraph that has a field that uses this module for its display formatter. In our situation, we have added multiple "Icon Card" paragraphs separately (so the SVGs are in separate paragraphs). The settings are set to:
Output SVG inline (checked)
Sanitize inline SVG (checked)
Set image dimensions (checked)
and no tokens or anything used in the Alt/Title fields. See screenshot for error. Hope this helps!
Comment #5
gnikolovskiYeah, so if the SVGs are in separate paragraphs then that makes sense.
I tested it with just one paragraph and multi value SVG field.
The title ID is generated like this:
$title_id = $this->fieldName . '__title-' . $delta;Comment #6
Letharion commentedI have the same problem, where icons can be attached to menu items. Because the icon field is then rendered multiple times, but attached to different entities, this also generates identical IDs, once for every menu entry.
To work around the accessibility issues I've hacked in the file id into the generated ID, but I don't know how applicable that is to others, the same file entity could also be rendered more than once on the same page.
What exactly is the purpose of the ids? Can we maybe have the option to just disable their generation, as a less hacky solution?The purpose of the id appears to be for the aria labelled by. Perhaps we allow the aria information to be inlined as one option instead of putting it in a separate element? Is having a even allowed the way it's rendered now? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title sayswhich isn't the case here.Nevermind, found you can have titles in SVGs: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title
Comment #7
kevinn commentedIm getting this issue to, here is a patch with a workaround.
Comment #8
Letharion commentedOne way to make the ID more unique would be to include the entity ID as well, such as:
That'll get us a little further, but would still break in the same way as soon as someone renders the same entity twice.
It would also break the current IDs so it should probably then only be in a version 2.x of the module.
Another solution that wouldn't break backwards compatibility would be to add an alter here, so everyone with unusual needs could easily use whatever strategy they want.
Comment #9
jeroentDrupal has a method in core that generates unique HTML IDs.
Html::getUniqueId().Comment #11
jeroentComment #12
gnikolovskiComment #13
gnikolovskiSince this breaks existing IDs this goes to version 2.x only.
Thanks!
Comment #14
jeroentIt seems this patch wasn’t committed yet?
Comment #15
jeroentComment #17
gnikolovskiSorry about that.
I pushed it to Github but not to Drupal.org
I tagged a new release, please use beta 2.