Skip to content

Comments

Remote Data Template block#428

Merged
chriszarate merged 13 commits intotrunkfrom
improve/loop-template-blocks
Apr 1, 2025
Merged

Remote Data Template block#428
chriszarate merged 13 commits intotrunkfrom
improve/loop-template-blocks

Conversation

@chriszarate
Copy link
Member

As a prerequisite for pagination, this PR introduces a Remote Data Template block that serves to encapsulate the blocks that render remote data:

template-block.mov

This block's function (and implementation) is very similar to Core's Post Template block: It allows us to configure a single representation of remote data in the editor, then use it as a template during render.

We had a half-measure towards this functionality in the existing LoopTemplate component; however, without a dedicated, registered block, we are unable to access the block and its contents during render. It is also needed so that we can insert other blocks (like pagination blocks) that are explicitly not included in the template—and therefore not repeated for each item.

While working on this, the existing "loop" blocks became superfluous. We already effectively perform loop rendering in non-loop blocks via multi-select. The hardcoded "Load items" button was the only remaining functionality keyed to the injected loop property, and it was easily replaced by a new "selector" type. This can be further improved by Brooke's work but for now it simply preserves the current functionality.

This change is backwards-compatible and will not break existing remote data blocks that were saved without a template block.

Major changes:

  • Introduce a Remote Data Template block that is automatically inserted whenever remote data is selected.
  • Remove "loop" blocks and move existing template functionality into the new template block.
  • Add render.php to the remote-data-container block to make its dynamic rendering explicit.
  • Update the rendering code in BlockBindings to look for and use a template block, when present.

@chriszarate chriszarate added the enhancement New feature or request label Mar 21, 2025
@github-actions
Copy link
Contributor

Test this PR in WordPress Playground.

chriszarate and others added 3 commits March 21, 2025 17:16
Signed-off-by: brookewp <brooke.kaminski@automattic.com>
@brookewp
Copy link
Contributor

@chriszarate there were a lot of small conflicts after I merged #418, so I resolved those here: 865dea2 (#428).

Disclaimer: Nothing was changed/removed from this PR.

Comment on lines 87 to 105
// If the render query has no required input variables and outputs a
// collection, we should provide a "load collection" selector.
$required_variables = array_filter( $input_schema, function ( array $input_var ): bool {
return true === ( $input_var['required'] ?? true );
} );
if ( empty( $required_variables ) && true === ( $output_schema['is_collection'] ?? false ) ) {
// Add a "load collection" selector.
array_unshift(
$config['selectors'],
[
'image_url' => $display_query->get_image_url(),
'inputs' => [],
'name' => 'Load collection',
'query_key' => self::DISPLAY_QUERY_KEY,
'type' => 'load-collection',
]
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think instead of adding another selector, we should add a conditional check to the single selector to see if it's type input or load-collection (can we rename it to collection)? So it behaves the same as before. Otherwise, we will get the 'Load collection' button and 'Provide manual input' buttons, like this:

An example of a collection with no inputs:
Screenshot 2025-03-25 at 2 47 31 PM

And the built-in Airtable loop block:
Screenshot 2025-03-25 at 3 29 01 PM

Copy link
Contributor

Choose a reason for hiding this comment

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

I borrowed the logic here for a related fix: #435

But I added it as a conditional in the render_query as I mentioned above. But if there's a reason it should be a selector, we can use the version here instead!

brookewp added a commit that referenced this pull request Mar 26, 2025
Signed-off-by: brookewp <brooke.kaminski@automattic.com>
brookewp added a commit that referenced this pull request Mar 26, 2025
* Add fix inspired by #428

Signed-off-by: brookewp <brooke.kaminski@automattic.com>

* Failing test fixes

Signed-off-by: brookewp <brooke.kaminski@automattic.com>

---------

Signed-off-by: brookewp <brooke.kaminski@automattic.com>
@chriszarate
Copy link
Member Author

Thanks for the improvements, @brookewp! I fixed a couple more conflicts in 82cb060

@chriszarate chriszarate requested a review from brookewp March 26, 2025 14:11
@chriszarate
Copy link
Member Author

I think instead of adding another selector, we should add a conditional check to the single selector to see if it's type input or load-collection (can we rename it to collection)?

@brookewp We separately need to check if the query is loading a collection or not. It could be loading a single item. I've made this change in 3ba760a

Copy link
Contributor

@brookewp brookewp left a comment

Choose a reason for hiding this comment

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

This looks great and is working well! 🎉

A small non-blocker, I wonder if we can hide the 'Remote Data' Panel when it's empty as it might be confusing?

Screenshot 2025-04-01 at 10 00 22 AM

@chriszarate
Copy link
Member Author

A small non-blocker, I wonder if we can hide the 'Remote Data' Panel when it's empty as it might be confusing?

Good catch, thanks! Will tackle in a separate PR.

@chriszarate chriszarate merged commit a776116 into trunk Apr 1, 2025
13 checks passed
@chriszarate chriszarate deleted the improve/loop-template-blocks branch April 1, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants