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.
Problem/Motivation
Currently alt and title are auto generated. This doesn't allow the user to customise the output.
Proposed resolution
Add configuration that allows the user to use tokens to specify where to get the values.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#12 | 3079043-12-alt-title-tokens.patch | 15.63 KB | Primsi |
#12 | 3079043-12-alt-title-tokens.interdiff.txt | 2.04 KB | Primsi |
#11 | 3079043-11-alt-title-tokens.patch | 15.62 KB | Primsi |
#11 | 3079043-11-alt-title-tokens.interdiff.txt | 6.54 KB | Primsi |
#9 | 3079043-9-1-alt-title-tokens.patch | 15.59 KB | Primsi |
Comments
Comment #2
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedInitial work.
Comment #3
Berdirwe should then unset this description if the token module is installed.
the token types that we have are file and the host entity type, e.g. node or media, which you can get from the field definition.
you can probably prepare the $data argument once and reuse it.. building it almost costs nothing.
Also not quite sure.. I think like this, with an isset(), you could also just as well keep the $attributes['alt'] assignment above and overwrite it inside the condition. because it will always at least return an empty string. We should also use the clear option in the third argument, because if a field is empty, we should make sure that we don't display the raw token as the alt title.
A test would be great, shouldn't be too complicated to create e.g. a media entity with a file and two text fields and then test with some tokens, then test a media with and without text in those fields.
Comment #4
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedThx for the review.
1. Changed
2. Added logic
3. Changed
Not sure if I got correctly this part, but I did change it :)
Added the clear option. The title attribute is still set here though. Not quite sure if we just don't set it or fallback to the auto generated.
Todo.
Comment #5
Berdir> Added the clear option. The title attribute is still set here though. Not quite sure if we just don't set it or fallback to the auto generated.
I wouldn't fall back, if there's no title then we should skip the alt/title attribute. So maybe it does need to be a bit more complicated again, so we only set it if we have a value:
<?php
$alt = $this->getSetting() ? use-setting : default;
if ($alt) {
$attributes['alt'] = $alt;
}
that said, I'd actually go for a more explicit:
$alt = NULL;
if (use token string) {
$alt = ...;
}
elseif (use fallback) {
$alt = ...;
}
if ($alt) {
// set it
}
Also, maybe use alt_string or something like that instead of token, technically you're not actually required to use a token, can be any string or multiple tokens.
Comment #6
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedAdded the test and addressed #5
Comment #7
BerdirI'd add a composer.json with require-dev instead. a test_dependencies change needs to be committed first, require-dev works immediately.
Can we assert the whole thing?
alt="thisisthealttext"
, so that we can specifically assert that there is no empty alt/title attribute in the second case.Comment #8
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedAddressed both.
Comment #9
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedSome code style and copycat leftovers.
Comment #10
BerdirWas thinking that it should be easier to write a CSS Based selector for this, but I guess a bit tricky as you render it through the API. I guess you could also switch the setting to allow access to /media/1, then this could be something like this:
$this->assertSession()->elementAttributeContains('css', '.field--name-field-media-file img', 'alt', 'thisisthealttext');
(elementNotExists doesn't exist, but you could do
$this->assertFalse($this->assertSession()->elementExists('css', '.field--name-field-media-file img')->hasAttribute('title'));
for that.Comment #11
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commented1. switched approach as suggested in #10
2. using entity repository to get the right translation for token replacement data as suggested in chat
3. minor cosmetic changes
Comment #12
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedCoding style changes
Comment #13
gnikolovskiComment #15
gnikolovskiThanks guys!