Problem/Motivation
Right now if you use the "Content Blocks" there's no preview -- just "No Preview Available" text on a grey background. It makes sense because you can't render a block that doesn't exist yet. Is there any way to get a more accurate preview "image"?

Steps to reproduce
1. Allow a "Content Block" like the "Text" block to be allowed on your node.
2. On the node edit page, use the "Block inserter" library to add the "Text" block. Hover over the block in the "Content Blocks" section and see no preview image, just "No Preview Available".
Proposed resolution
I imagine the ideal solution would be to render these blocks dynamically with some kind of default values, and then use a screenshot of that. Or possibly grab an existing block of the same type and render that?
An alternate, easier approach (explored initially here) is to allow editors to provide an image they load up somewhere to be the preview image.
Remaining tasks
- Submit preview image option patch
- Decide if that's a good approach or if we should go down the rendering path.
- If we do, add documentation about this feature somewhere... where does that go?
User interface changes
None
API changes
Adds a section to the gutenberg.yml file content-block-preview-image: where you can set a preview image per content-block bundle, like this: content-block/simple-hero: images/simple-hero.jpg
Data model changes
None
| Comment | File | Size | Author |
|---|
Issue fork gutenberg-3617959
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
mariacha1 commentedComment #4
marcofernandes commentedThanks @mariacha1! Let's go with the static preview image. It costs nothing at runtime, and the theme decides what editors see. Live previews can be a follow-up, see the end of this comment.
Review of MR !264
previewImageattribute.registerBlock()already has the definition, so the edit component can readdefinition.previewImage, and the block only needsexample: {}when there is an image (blocks withoutexamplekeep "No preview available"). As an attribute, it becomes part of the block's schema for no reason.getContentBlockPreviewImage()goes throughgetDefinitions(), which includes every installed theme, so an image of an installed but unused theme can be picked. Use the default theme and its base themes (getActiveThemeDefinitions()), plus modules._and:replaced (content-block/simple-hero), which site builders don't see anywhere. The bundle ID, as in the UI, is clearer:__unstableIsPreviewModeworks with the block editor 3.0.x ships, but since WP 6.8 the setting isisPreviewMode, andBlockPreviewonly sets the new name. Checking both (settings.isPreviewMode || settings.__unstableIsPreviewMode) keeps the preview working after the next package update, and in 4.0.x.file_url_generatorservice instead ofUrl::fromUri('base:…')and\Drupal::root()in a service.alt="": the inserter already shows the block title next to the preview.*.gutenberg.ymlsettings.About live previews
Rendering previews live is mostly a performance question. Upstream doesn't cache them: server-rendered core blocks (Archives, RSS, Latest Comments…) have
example: {}, so every inserter hover sends a request to/wp/v2/block-renderer, and so does every pattern preview containing one. WordPress gets away with it because the inserter only previews the hovered block. Drupal's case is harder: an unconfigured content block has no entity to render.If we do it in a follow-up, roughly:
SampleEntityGeneratordoes in tempstore.#cachekeys (bundle, view mode). The cache tags of the view display and the fields then invalidate it.ServerSideRenderdoesn't do this.DrupalBlockalready renders from its settings (/editor/blocks/load), so it only needs anexampleand the client side cache. Content blocks come after, with the sample entity.Setting back to Needs work for the points above.