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
In MarkupInterface we say
* If the object is going to be used directly in Twig templates it should
* implement \Countable so it can be used in if statements.
However, FormattableMarkup, TranslatableMarkup etc... are all not implementing this interface and they should.
Proposed resolution
Make MarkupInterface extend \Countable
so that if
in Twig works as expected for all of these objects.
Remaining tasks
Commit
User interface changes
None
API changes
API additions to all MarkupInterface implementing objects.
Data model changes
None
Why should this be done during RC
Because working with these objects in Twig should be simple and not doing this will make it difficult to do simple things in twig like...
{% if my_translated_variable is not empty %}
<div class='morten_loves_divs'>
<div class='for_good_measure'>
{{ my_translated_variable }}
</div>
</div>
{% endif %}
Comment | File | Size | Author |
---|---|---|---|
#35 | 2600672-35.patch | 6.76 KB | alexpott |
#35 | 21-35-interdiff.txt | 456 bytes | alexpott |
#21 | 2600672-21.patch | 6.79 KB | alexpott |
#21 | 19-21-interdiff.txt | 632 bytes | alexpott |
#19 | 2600672-19.patch | 7.41 KB | alexpott |
Comments
Comment #2
dawehnerThere we go.
Comment #3
dawehnerWrong code
Comment #4
alexpottThe patch still had extra stuff.
Comment #5
dawehnerYeah I was really wondering why the patchfile didn't changed
Comment #6
dawehnerLet's readd the test.
Comment #9
alexpottAdded some more tests and fixed the fails.
Comment #11
dawehnerThis text itself is not too verbose
In case you want to you could also expand the test coverage to include a GeneratedLink ...
Comment #12
alexpottImprove the tests and fixed GeneratedLink (nice spot).
Comment #13
dawehnerOh shit, sometimes we actually have to test stuff ...
I'd just use inheritdoc, but nevermind
Comment #17
alexpottComment #18
effulgentsia CreditAttribution: effulgentsia at Acquia commentedDiscussed with the other committers, and we agreed it makes sense to make core MarkupInterface implementations implement \Countable per the existing docs of MarkupInterface that say to do that. But we shouldn't change MarkupInterface itself, since that could result in a fatal error on existing sites using contrib/custom code with their own MarkupInterface implementations.
Comment #19
alexpottChanged the implementation to match #18 and adjusted test to ensure that objects that don't implement countable work as expected. Given the originally I didn't implement
\Countable
onAttribute
I removed it.Comment #21
alexpottSome left over test code.
Comment #22
dawehnerCan we at least document on MarkupInterface that implementations of this interface should ideally implement \Countable as well, in order to be better usable in twig?
Comment #23
alexpott#22 that documentation exists already :)
Comment #24
dawehnerAh okay, so core just ignored its own documentation, nothing new.
Comment #25
neclimdul:(
Comment #26
joelpittet@neclimdul can be fixed on commit, thanks for noticing that though.
Comment #35
alexpottLet's fix it so a committer doesn't have to remember.
Comment #36
catchCommitted/pushed to 8.0.x, thanks!