Skip to content

Docs: Add user input dynamic block info - #8765

Closed
wpmark wants to merge 2 commits into
WordPress:masterfrom
wpmark:patch-1
Closed

wpmark wants to merge 2 commits into
WordPress:masterfrom
wpmark:patch-1

Conversation

@wpmark

@wpmark wpmark commented Aug 9, 2018

Copy link
Copy Markdown

This addition attempts to explain how to create a dynamic block that takes user input in the editor (in this example in the form of a rich text input) and then uses PHP to output this on the front-end. The benefit here is that the blocks markup can change without needing to update each of the posts using that block.

I found this page lacking in how to do this as it focused on dynamic blocks that carry out a query in PHP for the content rather than output the takes user input from the block itself. Hopefully it will help others.

Description

How has this been tested?

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

This addition attempts to explain how to create a dynamic block that takes user input in the editor (in this example in the form of a rich text input) and then uses PHP to output this on the front-end. The benefit here is that the blocks markup can change without needing to update each of the posts using that block.

I found this page lacking in how to do this as it focused on dynamic blocks that carry out a query in PHP for the content rather than output the takes user input from the block itself. Hopefully it will help others.
@wpmark wpmark changed the title Add user input dynamic block info Docs: Add user input dynamic block info Aug 10, 2018

@tofumatt tofumatt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is the right messaging for dynamic blocks, but the example of how to write a dynamic block with both JS and PHP code for reference is cool. 😄

I think this would be better refactored with just the example and not the implication/suggestion to developers to use dynamic blocks for markup management.

Comment thread docs/blocks/creating-dynamic-blocks.md Outdated

## Rendering block markup with PHP

Dynamic blocks can be used in order to control the markup of your block with PHP. This is useful as if you decide to change your blocks markup, you won't have to go back and re-save all the posts which use that block, in order for your blocks markup change to be reflected on all posts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are some issues here with grammar (blocks markup should be block’s markup).

But beyond that, I don't think we should be promoting dynamic blocks in this way… dynamic blocks exist as a way to render content that requires interaction with the server, but not as a way to not have to worry about changing block markup or avoiding dealing with deprecating block markup.

This write-up makes it sound like the purpose of dynamic blocks is to make it easy for block developers to change their markup without effort… but that's not their purpose 😄

That approach can be problematic for users too because their content will change without them expecting it when a block is updated.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@tofumatt thanks for the feedback - grammar is not my strong point.

The wording here doesn't insist that dynamic blocks can be used for controlling block markup, only that they can be used in this way.

Dynamic blocks can be used in order to control the markup of your block with PHP

The key word here is 'can'.

If dynamic blocks shouldn't be used to for controlling block markup, what can please? Saving the markup in the the_content means that the markup cannot be changed or updated without the post being re-saved. Obviously, there must be another method for doing this and I have got mixed up between the two.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No worries about the grammar! 😄

I suppose if you wanted to centrally control markup you could use dynamic blocks, but my question would be why you'd want to control the markup itself. I can see using dynamic blocks for things like Latest Posts because the client can't render them, but the client can render HTML just fine and keeping content local improves the editing experience.

That said, there's (currently) no method to deal with updating block content en masse if it's not dynamic.

My worry is that the docs, as worded, will push developers toward dynamic blocks for ease of creation at the cost of user experience editing content. Dynamic blocks can't re-render offline, will be slower because of network latency, etc. Maybe I'm being a bit too cautious but I know there's a temptation to use dynamic blocks for everything to avoid much JS 😅

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If the current implementation means that markup cannot be changed without updating all posts that use a block then this is a massive problem. Imagine you make a block for a multi-site install of WordPress (we have 2 with over 500 sites on each) and realise that you need to change the markup for whatever reason - it happens more than you think. For example, if the design of the site changes and an additional element is needed to wrap, go before or after content to meet the needs of the design.

Each site might have hundreds of posts, pages etc. using that block and therefore a way to do this without saving the block on all posts is a must here.

Maybe this is a seperate issue that needs addressing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indeed, that is how Gutenberg works and you're right: it is a separate issue. You can follow the discussion here #8015 and possibly #7604 as well.

I think it would be cool to have a mechanism to update existing blocks in old posts but I don't think we've settled on anything yet.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Umm. In #4849 @youknowriad says:

Yes, dynamic blocks are the way to go in this case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Heh, I see. 😅

Blocks markup to block's markup.
function my_render_dynamic_block( $attributes, $content ) {

// we can now return the rich text fields content with whatever markup we like.
return '<div class="my-richtext-block">' . $content . '</div>';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As I said in #4849 (comment) I don't think we should add this to our docs because it recommends to people to use dynamic blocks for static ones.

@ajitbohra ajitbohra added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Nov 29, 2018
@gziolo gziolo added the [Type] Developer Documentation Documentation for developers label Jan 29, 2019
@gziolo

gziolo commented Jan 29, 2019

Copy link
Copy Markdown
Member

@chrisvanpatten, @ajitbohra, @mkaz and @nosolosw - given that we have now several tutorials, how does it fit to the picture? Should we invest more time to apply those changes or is it fine to close it?

@ajitbohra

Copy link
Copy Markdown
Member

@gziolo we already have a tutorial covering the creation of dynamic blocks using ServerSideRender

https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/creating-dynamic-blocks/

Current tutorial address the concern raised by @youknowriad as we are recommending to use it as a fallback.

Server-side render is meant as a fallback; client-side rendering in JavaScript is always preferred (client rendering is faster and allows better editor manipulation).

Seems like its fine to close this.

@mkaz

mkaz commented Jan 29, 2019

Copy link
Copy Markdown
Member

Agreed, we can close this one, as @ajitbohra commented the block tutorial covers dynamic blocks

@gziolo

gziolo commented Jan 30, 2019

Copy link
Copy Markdown
Member

@wpmark thank for your contribution. As discussed above docs has been improved a lot in the recent weeks based on the feedback shared by the community. There is a common agreement amongst those who are involved in all docs efforts the most that the changes you proposed are already covered in other places. I'm closing this PR. Feel free to open a new PR if you want to target some specific aspects of docs.

@gziolo gziolo closed this Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. [Type] Developer Documentation Documentation for developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants