-
Notifications
You must be signed in to change notification settings - Fork 391
Issue #6893: Add a block render function outside layouts #5076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.x
Are you sure you want to change the base?
Conversation
Related to: backdrop/backdrop-issues#6893 |
Move block render code outside layouts This commit moves the block render code outside layouts so that third party modules can render blocks without having to re-implement core code.
I totally borked this block_render function.
After looking through this, it appears that layout module doesn't require the block module so it was throwing an error about not finding block functions. I figured we probablty didn't want to make layout depend on block so I've moved those functions into the layout module. Fixes backdrop/backdrop-issues#6893
50a448f
to
696e43f
Compare
I had to change a few things to get the test to run -- namely after the block hooks run it returns a string instead of an array. In order to get the layout contextual links to be added I had to switch it back to an array. I'm not super happy about how that code is, but I'm going to tackle that in another commit. Fixes backdrop/backdrop-issues#6893
I've moved layout_block_get_renderable_array to the Block class which I think I kind of like. I renamed it getRenderArray() for brevity and I think they're called render arrays in the documentation. I didn't like that the function assumed that $block_prepare() was called, so I've added a new property to the block class $isPrepared that we can check in getRenderArray(). Fixes backdrop/backdrop-issues#6893
Tugboat has finished building a preview for this pull request! Website: https://pr5076-dpodjcpubcljnovljnmlbz4gftblsuue.tugboatqa.com/ This preview will automatically expire on the 17th of July, 2025. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bennybobw generally it looks good! I added a few suggested changes for consistency. I haven't tested this at all.
/** | ||
* Get a render array for a block. | ||
*/ | ||
function getRenderArray() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this view()
so it matches the general approach with entities, like with Node::view()
. I don't think including the variable type in the name follows Backdrop convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize that there's also hook_block_view
which is used by the BlockLegacy class to build the content part. And here this takes that content render array and outputs another content array.
@@ -426,6 +435,53 @@ class Block extends LayoutHandler { | |||
function getChildren() { | |||
return NULL; | |||
} | |||
|
|||
/** | |||
* Get a render array for a block. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Get a render array for a block. | |
* Generate an array for rendering the block. | |
* | |
* @return array | |
* A render array of the block as expected by backdrop_render(). |
} | ||
|
||
if (!$this->isPrepared) { | ||
$this->prepare(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking this too; a way to track if prepare() was called.
* Render a block | ||
* @param Block $block | ||
* The Block entity to render |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Render a block | |
* @param Block $block | |
* The Block entity to render | |
* Generate an array for rendering the given block. | |
* | |
* @param Block $block | |
* The Block to render. | |
* | |
* @return array | |
* An array as expected by backdrop_render(). |
$output = ''; | ||
$render_array = $block->getRenderArray(); | ||
if ($render_array) { | ||
$output = backdrop_render($render_array); | ||
} | ||
return $output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If getRenderArray()
(aka view()
) always returns an array then this can be simplified:
$output = ''; | |
$render_array = $block->getRenderArray(); | |
if ($render_array) { | |
$output = backdrop_render($render_array); | |
} | |
return $output; | |
return backdrop_render($block->view()); |
backdrop_render()
returns early if the array is empty.
@@ -520,38 +520,20 @@ class LayoutRendererStandard { | |||
} | |||
|
|||
$output = ''; | |||
if ($content = $block->getContent()) { | |||
if ($render_array = $block->getRenderArray()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, following the approach elsewhere (such as in node_search_execute()
):
if ($render_array = $block->getRenderArray()) { | |
if ($build = $block->view()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And rename everywhere $render_array
to $build
.
@@ -73,6 +73,12 @@ class Block extends LayoutHandler { | |||
*/ | |||
public $layout_title; | |||
|
|||
/** | |||
* A boolean holding whether prepare() has run | |||
* @public boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The property description must end with a period.
There must be an empty line between the description and the next line containing the tag.
The correct tag is @var
.
|
||
/** | ||
* Render a block | ||
* @param Block $block |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method description must end with a period.
There must be an empty line before the first @param
.
The description of the parameters must be indented.
Fixes backdrop/backdrop-issues#6893