0

What we want: Users should be able to choose an image, add call to action links, and a few other options when setting up a hero for the specific page they are creating. We have three different content types we allow this on currently, basic pages and two custom content types we've created.

Current solution: We have a paragraph with the fields they can change such as layout (how they want the image, call to action links, etc. to be styled), which image they want to use, the links (if any) to be rendered on top of the image, heading, caption, and a few others. This paragraph is referenced on the content types by an entity reference field and then when they create whichever content type, they change all the fields as usual. We have different twig templates and css files to render the images, links, etc. different based on the layout they choose. Since we want the hero image to render in a different part of the page, not within the content of the node, we are using node.field_name within the page.html.twig template to render it in the section of the page we want.

Hero Paragraph settings

Hero Paragraph Rendered

Current problem: The hero is dynamic per page and is rendering as expected but the changes are not showing in the node revisions. The published version of the page shows the correct hero, the /latest version of the node is showing the updated/correct hero when a draft has been created, but when we go to the node revisions and click the latest draft there (links such as node/31366/revisions/1623918/view), it is showing the same hero settings as the published revision.

Related modules we are using: Content moderation, entity_reference_revisions, paragraphs

I added an image of the editing experience, I couldn't get the entire paragraph but that is most of it. I also added an image of what the paragraph looks like rendered, the full image was too large to upload here so it is a smaller version than what is actually shown on the site but too large to attach here.

I've researched current issue with paragraph revisions and node revisions but can't seem to find a patch or any fixes to the issues we're having. If someone could help me with a better solution or sees something that we're doing wrong, we'd really appreciate help/advice. Thanks!

6
  • After reading this I think the question is "why aren't paragraphs changes seen in revisions?". Am I correct? What version of entity_reference_revisions is on the site? drupal.org/project/entity_reference_revisions Is the reference field configured to be revisioned?
    – mona lisa
    Sep 18, 2023 at 20:11
  • That is my main question, yes. We have version 1.10.0 installed on the site. The field on the node that references the paragraph is an entity_reference_revisions field and the image field itself within the paragraph is an entity_reference_revisions field that allows either an image or a video. The change is reflected on /latest but not within the revisions themselves which seems strange.
    – Jordan
    Sep 18, 2023 at 20:34
  • The $node variable in in page.html.twig always is the latest revision. If you want different versions of the node object, you have to do your own preprocessing.
    – Hudri
    Sep 18, 2023 at 20:45
  • Ahh okay, thank you. I feel like there should be a simpler solution to what we are wanting to do. Do you have any ideas for having a different image per page and rendering it in a different part of the page rather than in the node content? I would just create the fields on each of the content types we want to do this on but then they will just be duplicates. I thought about adding fields via BaseFieldDefinition::create but haven't done this before and am not sure where to put it. We are on drupal 9.5.x.
    – Jordan
    Sep 18, 2023 at 21:00
  • Yeah, as @4uk4 says: New custom block plugin, place the block, get the current node context, get the hero image from node, get the node title (<h1>) or what else you need from the node, render as you like, style as you need. 🚀
    – leymannx
    Sep 19, 2023 at 8:36

1 Answer 1

2

You are asking for a better solution, this would be a hero block instead of hard-coding the hero image in the page template. If your block is using the node route context it automatically loads the node revision.

If you want to get the current solution working, try this code in a preprocess hook:

mytheme.theme

function mytheme_preprocess_page(&$variables) {
  $node = \Drupal::routeMatch()->getParameter('node_revision');
  if ($node) {
    $variables['node'] = $node;
  }
}
3
  • Okay so I was actually on the right track before switching to just rendering the paragraph within the page template. I have a custom block already written up that uses the node context definition and pulls the data from the paragraph reference which is on the node. A similar problem occurred though where if I had a published version, made a draft with a new image, the new image would actually show on the published version. Should I make a new question showing my block code or just update this question with it?
    – Jordan
    Sep 19, 2023 at 14:22
  • I would start a follow-up question for the block code and reference this question for the background info,
    – 4uk4
    Sep 19, 2023 at 14:47
  • Going to accept your answer and I've created a follow up question and linked this question. See here please drupal.stackexchange.com/questions/317060/…
    – Jordan
    Sep 19, 2023 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.