Problem/Motivation
In the Toastify module, we're trying to intercept the status messages in order to render them as toasts. We're doing this by extending the Drupal\Core\Render\Element\StatusMessages class to add the messages to drupalSettings instead of rendering them using ['#theme' => 'status_messages']. The only problem: the parent class uses get_class() in its lazy_builder and pre_render callbacks, requiring us to duplicate a bunch of code instead of being able to just override the renderMessages method.
Proposed resolution
Use static::class instead of get_class().
Issue fork drupal-3346560
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
dieterholvoet commentedComment #4
smustgrave commentedThis seems like more a feature request.
Either way think test coverage should be included.
Comment #5
dieterholvoet commentedI wrote a test. I don't have much experience writing tests though, so any feedback is welcome.
Comment #6
smustgrave commentedSeems there was a CI failure with the test
Not sure you can declare multiple classes within the test, least that I have seen. Could these be private functions for the main class or part of the setup()?
Did run the test locally though without the fix and it did fail which is good means it's a valid test run
Comment #7
dieterholvoet commentedFixed the test.
Comment #8
smustgrave commentedGood job!
Confirmed the test fails locally without the fix so it's valid.
Small change and don't see anything glaring.
Comment #9
longwaveThe MR does not merge cleanly.
Comment #10
dieterholvoet commentedI rebased the MR against the latest 10.0.x.
Comment #13
catchI'm a bit borderline on the test coverage here, also discussed with @longwave who was also somewhat borderline. We have a general rule about using late static binding unless there's a very good reason to, so I don't think this is something that would regress, and then we need to maintain the tests over time. Also thought about whether we could enforce this with phpstan instead of tests although that might be taking things to far.
On balance I went ahead and committed this without the test coverage.
Committed/pushed to 11.x and cherry-picked back through to 10.0.x, thanks!