Score:1

Fetch and combine values from nested fields: present all copyright credits in one block

jp flag

I have a content type using media fields, and using Paragraphs which also have media fields.

A typical page might look like:

  • node
    • field_media1 (one value here)
      • field_copyright
      • field_img
    • field_paragraphs (several values here)
      • field_media2
        • field_copyright
        • field_img

I'd like to pull out all the rendered field_copyright content in hook_node_preprocess and stick it in a variable to be rendered at the bottom of the page.

I can imagine that I can do a recursive loop down through the $node entity in hook_node_preprocess, but then I need to generate render arrays for all that raw data; it feels like what I want to do is pluck out the rendered versions of these fields, so they're not included when the media fields get rendered.

Is there a way I can do this?

4uk4 avatar
cn flag
I would use a Views block with a contextual filter for the current node.
leymannx avatar
ne flag
What about another view mode for both the node and the paragraphs. And then render the current node (maybe in a custom block plugin or a Views block) in that new view mode which itself renders the paragraphs in their new view mode?
4uk4 avatar
cn flag
@leymannx, yes, this is a great idea if you don't mind to write a block plugin.
leymannx avatar
ne flag
@4k4 – Or in a Views block, of course. You were faster.
jp flag
The views block sounds hard. Is it going to be able to reach in to those paras and pluck fields from the appropriate paragraph types and aggregate them with the other fields? I thought about @leymannx solution too, but I think I'd rather just render the raw data myself than set up a whole other cumbersome set of view modes. I did wonder if a module `mymodule_process_node` could do the hoiking, but I haven't achieved that yet either.
leymannx avatar
ne flag
Views block, show content, rendered entity, in view mode XY. Configure contextual filter to get current node ID from URL. It's not that difficult. It can all be done from the UI. No need to code. Up to you.
jp flag
Oh I see, just using Views to present the rendered entity. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.