Gutenberg: Dynamic Rendering of RichText

Created on 17 Jun 2018  Â·  8Comments  Â·  Source: WordPress/gutenberg

I haven't found help for this anywhere else.
But this could also be considered a feature request, if what I'm asking is not possible yet.

When I save <RichText />s content into my attributes and want to use this with render_callback in php, it get's kinda messy. As soon as the text contains RichText (bold, italic, line-breaks etc.), $attributes stores it really complicated

attributes: {
    description: {
        type: "text",
        // no source, so it goes straight into the html comment, nice for dynamic rendering
    }
}

```php
...
"render_callback" => function( $attributes ) {
return "

" . $attributes["description"] . "

"
}

```php
// var_dump($attributes["description"]
array(3) {
    [0]=>
    string(24) "Rendering RichText with "
    [1]=>
    array(5) {
      ["type"]=>
      string(6) "strong"
      ["key"]=>
      string(10) "_domReact4"
      ["ref"]=>
      NULL
      ["props"]=>
      array(1) {
        ["children"]=>
        string(3) "php"
      }
      ["_ower"]=>
      NULL
    }
    [2]=>
    string(22) " is a pain in the a..."
  }

I guess there is a reason why you save RichText like this.
So how do you suggest to use it when rendering dynamically?

  1. Is there a function like decode_craziness($attributes["text"])?
  2. (rather a workaround) Don't render this text dynamically at all; save it statically and use it as a parameter in render_callback [a request from #7331]
[Feature] Block API [Feature] Extensibility [Type] Help Request

Most helpful comment

I've filed this against Merge Proposal: Plugins so we can come to a conclusion on it sooner rather than later.

All 8 comments

Somewhat related #5739

I don't see how these issues are related. (Disclaimer: read only the owners text and scanned the answers) – could you @danielbachhuber elaborate why you think so?

Since you didn't assign a bug-label, I suppose this behaviour is intended.

How do you suggest to use RichText dynamically? Decoding this object should be implemented by gutenberg; even if I wanted to, I don't think I have the skills to create a <p> out of this object.

I don't see how these issues are related. (Disclaimer: read only the owners text and scanned the answers) – could you @danielbachhuber elaborate why you think so?

Related in the sense that var_dump($attributes["description"] doesn't necessarily produce the expected behavior:

// var_dump($attributes["description"]
array(3) {
    [0]=>
    string(24) "Rendering RichText with "
    [1]=>
    array(5) {
      ["type"]=>
      string(6) "strong"

It seems like the expected behavior is to get pure HTML.

How do you suggest to use RichText dynamically? Decoding this object should be implemented by gutenberg; even if I wanted to, I don't think I have the skills to create a <p> out of this object.

I don't have a good suggestion at this point. #6751 #5708 #2751 are also related.

Yes it would be great if render_callback would immediately turn convert RichText from object to html-string. Especially for uncluttering the html comment + database. Debugging / searching for attributes in the comment is crazy once RichText is involved. And I don't know any other way to inspect the attributes of a block under the hood.

But if that's too much at this or even any point, gutenberg must provide a function to get the pure HTML.

Anybody else from the team with suggestions?
I just can't imagine that rendering dynamically was not taken care of or atleast thought of when you build the <RichText /> component.

I mean: gutenberg clearly has the logic to turn this object into HTML already, since it is saved correctly in static context.

I've filed this against Merge Proposal: Plugins so we can come to a conclusion on it sooner rather than later.

Got an approach here: https://gist.github.com/AndreKelling/ac4d8be7111d27aaf236460dc5b1800e
So far it covers nesting of tags, a tags and br's.
All a RichText Component can come up with at the moment.

I had the exact same problem like katerlouis. I found a solution here: https://github.com/WordPress/gutenberg/issues/5708#issuecomment-401134065

Closing based on @urukai's note. Please re-open if the suggestion noted doesn't solve the problem for you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

afercia picture afercia  Â·  78Comments

ahmadawais picture ahmadawais  Â·  271Comments

melchoyce picture melchoyce  Â·  169Comments

mapk picture mapk  Â·  80Comments

melchoyce picture melchoyce  Â·  95Comments