Skip to content

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

Open
wants to merge 7 commits into
base: 1.x
Choose a base branch
from

Conversation

avpaderno
Copy link
Member

@backdrop-ci
Copy link
Collaborator

Related to: backdrop/backdrop-issues#6893

Benjamin Wheeler added 3 commits May 16, 2025 14:09
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
@bennybobw bennybobw force-pushed the 6893-block-render branch from 50a448f to 696e43f Compare May 16, 2025 18:40
Benjamin Wheeler added 4 commits May 17, 2025 13:29
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
@dragonbot
Copy link
Collaborator

Tugboat has finished building a preview for this pull request!

Website: https://pr5076-dpodjcpubcljnovljnmlbz4gftblsuue.tugboatqa.com/
Username: admin
Password: 8c330181e0cb

This preview will automatically expire on the 17th of July, 2025.

Copy link
Contributor

@herbdool herbdool left a 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() {
Copy link
Contributor

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.

Copy link
Contributor

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.
Copy link
Contributor

@herbdool herbdool May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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();
Copy link
Contributor

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.

Comment on lines +2773 to +2775
* Render a block
* @param Block $block
* The Block entity to render
Copy link
Contributor

@herbdool herbdool May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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().

Comment on lines +2778 to +2783
$output = '';
$render_array = $block->getRenderArray();
if ($render_array) {
$output = backdrop_render($render_array);
}
return $output;
Copy link
Contributor

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:

Suggested change
$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()) {
Copy link
Contributor

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()):

Suggested change
if ($render_array = $block->getRenderArray()) {
if ($build = $block->view()) {

Copy link
Contributor

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
Copy link
Member Author

@avpaderno avpaderno May 18, 2025

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
Copy link
Member Author

@avpaderno avpaderno May 18, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a block render function outside layouts
4 participants