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.
The Block Example has Example: empty block which is not rendered when placed into a region. Would be it useful to demonstrate how to fill it with the content in the block_example_block_view_alter().
Comment | File | Size | Author |
---|---|---|---|
#9 | block_example-2893964-9.patch | 2.04 KB | vaibhavjain |
| |||
#7 | block_example-2893964-5.patch | 3.88 KB | drugan |
| |||
#5 | block_example-2893964-4.patch | 4.05 KB | drugan |
#3 | block_example-2893964-3.patch | 2.75 KB | drugan |
#2 | block_example-2893964-2.patch | 880 bytes | drugan |
|
Comments
Comment #2
drugan CreditAttribution: drugan as a volunteer commentedInspired by https://drupal.stackexchange.com/a/215948/64255
Comment #3
drugan CreditAttribution: drugan as a volunteer commentedAnother method of extending/adding content to any block. No additional pre_render callbacks, no need to individually extend each of the block definition class. All the magic happens in a HOOK_block_view_alter().
Comment #5
drugan CreditAttribution: drugan as a volunteer commentedTest file is updated.
Comment #7
drugan CreditAttribution: drugan as a volunteer commentedOne more attempt.
Comment #8
Mile23Thanks!
This is a fake-out of
BlockViewBuilder::preRender()
, and not the block API. Specifically this line of code:Unfortunately
BlockViewBuilder::preRender()
doesn't have@param
documentation, so we can't even rely on that.So therefore this technique is rather brittle, because all it would take is a minor refactoring with param type hinting to break it. I bet that if the render system didn't need
BlockViewBuilder::preRender()
to be public, it wouldn't be.From https://drupal.stackexchange.com/a/241608/4894 it seems like the main advantage to this technique is that you can change the block title. So if you don't need to change the title this is too brittle and you should just use
hook_block_view_alter()
without the extra class.If you do need to change the title, then
OverrideAnyBlockContent
would need to implementDrupal\block\BlockInterface
, with stub methods, and also act as a façade for the content being altered in the hook.Also:
I really don't like that we'd have an 'uncomment this line' type functionality. Since we're making a class, we can have it do all the things we need, like maybe an
appendRenderArray()
method.Anyway, regardless of how it's implemented, I'd rather separate it out into another block type that's empty, but will be altered in the hook. That way we have both examples.
Comment #9
vaibhavjain@Mile23 Attaching a patch with another approach, where we have another block to depict that the block has been altered. Also, we have implemented hook_block_view_BASE_BLOCK_ID_alter, which would act as another example, rather than adding code in hook_block_view_alter which already existed.
As the approach is a lot different, not adding any interdiff.
Comment #10
vaibhavjainUpdating the status.