Return parsed block content when querying pages from REST export

Created on 14 September 2021, over 3 years ago
Updated 15 January 2024, 11 months ago

When displaying the content of a Gutenberg page via a custom REST export from Views, is there any way to display the content as an array of objects parsed from the HTML-commented Gutenberg string? For example, if I were to create a page with a simple paragraph block:

<!-- wp:paragraph {
    "placeholder":"Summary",
    "textColor":"accent",
    "backgroundColor":"secondary"
} -->
<p class="has-text-color has-background has-accent-color has-secondary-background-color">
This is a new paragraph.
</p>
<!-- /wp:paragraph -->

Could I return something from the REST export that looks more like this:

  [
    {
      blockName: "core/paragraph",
      attrs: [
        {
          placeholder: "Summary",
          textColor: "accent",
          backgroundColor: "secondary"
        }
       ],
      innerBlocks: [],
      innerHTML: '<p class="has-text-color has-background has-accent-color has-secondary-background-color">This is a new paragraph.</p>',
      innerContent: [
        '<p class="has-text-color has-background has-accent-color has-secondary-background-color">This is a new paragraph.</p>'
       ]
  ]

In WordPress, I've generally had success with by using the parse_blocks function with filters.

Feature request
Status

Needs review

Version

3.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France sashainparis Provins

    See JsonAPI Gutenberg Blocks module I've just published.

  • 🇫🇷France sashainparis Provins

    Reopening the issue to integrate the JsonAPI Gutenberg Blocks module inside the Gutenberg module - as compatibility with JsonAPI Core module.

  • Assigned to sashainparis
  • Status changed to Needs work 12 months ago
  • 🇫🇷France sashainparis Provins

    Branch 3232930-return-parsed-block: Gutenberg v3

  • Status changed to Needs review 12 months ago
  • 🇵🇹Portugal marcofernandes

    Regarding the parser, WP as a new version of it but not sure if addresses the issues you pointed out. The issue with heading block attributes, I guess that's because level 2 is the default. To address that I guess we would need some kind of block registration mechanism at server level (which it is planned to do in the future) to fetch the defaults.
    Anyway, as you said, these details should be addressed on a different issue.
    Could you create the Merge Requests to dev branch for both versions?

  • 🇫🇷France sashainparis Provins

    Worked on better parsing in the same file.
    So I didn't open another Issue.

    Commited to V2 module version.
    V3 needs to be done.

    Please note: structure is made to converge with the WP API to make sure Components working on WP API should work out-of-the-box on Drupal.
    This needs some controls too.

Production build 0.71.5 2024