Working with nested Rich Text fields

Hello Rouven, does your code example also apply to rich-text fields which are references within a content type?

Moderation note: this post was split off from Add a target=“_blank” to hyperlink within Rich Text Content Type.

Hi @uk1,

I’m not sure I fully understand. Do you mean links that are referencing another entry and not a URL?

If so, the code would have to be a bit more complex. The data would not include the uri, instead it’d contain a link to the entry. You’d have to resolve that entry ID to a URL based on your routing logic. Then you can use similar code to mine to add the target property.

I am sorry, I realize my question was rather cryptic. I have a content type A with a richt-text field b. The method you describe works well for <a> elements in this rich-text type field. But I have another rich-text field c which is pulled into A via a “References, many” field. I am having difficulties targeting <a> elements in c.

I still don’t follow.

How do you pull c into A? References, many would be a separate field in a content type and not directly be part of a rich text field. Rich text only support block embeds, inline embeds and links for entries.

Via References I am linking to a content type “Section”, which has a rich-text field. In the Gridsome template “text” is the referenced rich-text field:

<div v-html="richTextToHTML(section.text)" />

I am trying to add targets for the hyperlinks in this rich-text field.

We’re getting far off the topic of this thread here. If I understand it correctly you have a content type A with a rich text field b. You then have another content type B with a rich text field C. You’re using an entry block embed (?) to embed B into b.

If so, you’ll need to call richTextToHTML twice. In the outer call you override BLOCKS. EMBEDDED_ENTRY to render the inner rich text field (b). In both you gotta do the transform for the links to have the correct rel attribute.

I hope this helped :slight_smile:

Thanks, Rouven, I’ll try that.