Problem/Motivation

Per #3027653-51: Allow block and layout plugins to determine if they are being previewed, block and layout templates should be provided with a variable, "in_preview", to inform the templates whether they're being rendered in preview mode (TRUE) or regularly (FALSE).

Proposed resolution

Modify template_preprocess_HOOK in where necessary to add the "in_preview" template variable.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork drupal-3273317

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

clayfreeman created an issue. See original summary.

tim.plunkett made their first commit to this issue’s fork.

larowlan made their first commit to this issue’s fork.

clayfreeman’s picture

Status: Active » Reviewed & tested by the community

Aside from the small bit of key reordering I did, this looks RTBC to me!

tim.plunkett’s picture

+1, thanks @clayfreeman and @larowlan. Glad we didn't need a getter after all

larowlan’s picture

Added a change record

clayfreeman’s picture

Assigned: Unassigned » clayfreeman
Status: Reviewed & tested by the community » Needs work

I actually didn't realize that it would be easy to implement this for block templates also; setting back to Needs Work, as I think it will be needed by #3137995: Can't manage Status Messages block in Layout Builder and it's better scoped to happen in this issue.

clayfreeman’s picture

Title: Add 'in_preview' variable to layout templates » Add 'in_preview' variable to block and layout templates
Issue summary: View changes
Status: Needs work » Needs review

I will need to follow-up on this commit with a test case, but I primarily just need to know that existing tests pass for now. Also adjusting issue title and description to match.

clayfreeman’s picture

clayfreeman’s picture

Assigned: clayfreeman » Unassigned

I believe this should be good for additional review on the block stuff that I added. I also updated the change record with the new information.

I noticed there may be an opportunity to combine the layout template test with the one that was added in #3027653: Allow block and layout plugins to determine if they are being previewed, but I don't know if it's worth the effort since it's already written and working as intended.

clayfreeman’s picture

Most recent commit adds a bunch of lines documenting the availability of the new in_preview variable. Not sure if this is required or desireable by maintainers, but should be easy enough to revert if not. (I've never done this before, so I'm not sure what the general preference is for this type of change.)

danflanagan8’s picture

Not sure if this is required or desireable by maintainers

I'm not a maintainer by any means, but I think this is a new property worth advertising! Of course I was the one who requested the change. :)

I also manually tested the code and it works great! I used it to clean up a contrib module of mine that used a horrible hack to change the layout template during preview. I was able to change

{% if content.background.layout_builder_add_block %}

to

{% if in_preview %}

I swear I couldn't figure out a better way before. This new property is incredible. Can't wait til I can actually commit this change.

I also used in_preview in a block template to do a POC fix #3027653: Allow block and layout plugins to determine if they are being previewed, which was @clayfreeman's idea. Worked great for me there.

I think the test coverage is appropriate. I'm ready to RTBC when the latest tests pass. I'm a little worried my requests to update the template docs is going to cause tests related to template hashes to fail. We'll see!

clayfreeman’s picture

The test did fail on some sort of hashing mechanism which I'm not too familiar with. The additional template documentation changes, at least as I perceive them, have a pretty wide scope. I'm not sure if the changes are worth making, especially since the functionality is documented in the block template base hook.

Perhaps a maintainer could weigh in as to whether I should revert my last commit or seek a fix for the hash changes.

larowlan’s picture

I think the docs changes are worthwhile

danflanagan8’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for gutting out these changes to the docs and hashes, @clayfreeman. I think it's worth it to make it easier to stumble upon this new variable.

That fail was a random quickedit thing, so let's consider this green.

Looks awesome. I tested manually as described in #13. RTBC.

Can't wait to start using this!

quietone’s picture

Version: 9.4.x-dev » 10.0.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Sorry folks, this will need to be on 10.0.x now and the MRs is no longer mergeable so tests are not running. It would be good to add an MR for 9.5.x.

clayfreeman’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll

Assuming RTBC can stick on this since it was just a reroll, though feel free to revert if that's not the case.

ETA: Noting for the record that the diff from MR !2053 applies cleanly to 9.5.x, so I'm not sure there's any value in creating a separate MR.

alexpott’s picture

Version: 10.0.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 6483b2997a to 10.1.x and 1b3ec547f2 to 10.0.x. Thanks!
Committed 00243d3 and pushed to 9.5.x. Thanks!

I rerolled the patch for 10.x - removing all the changes to files that no longer exist. I backported this to 9.5.x because it is an addition will no BC implications and this has been RTBC since May 2022.

  • alexpott committed 6483b29 on 10.1.x
    Issue #3273317 by clayfreeman, larowlan, tim.plunkett, danflanagan8: Add...

  • alexpott committed 1b3ec54 on 10.0.x
    Issue #3273317 by clayfreeman, larowlan, tim.plunkett, danflanagan8: Add...

  • alexpott committed 00243d3 on 9.5.x
    Issue #3273317 by clayfreeman, larowlan, tim.plunkett, danflanagan8: Add...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.