Make it more obvious that a Twig template is overridden

Created on 11 February 2024, 10 months ago
Updated 14 March 2024, 9 months ago

Problem/Motivation

To help new Drupal users get started with theming, it would be nice if it was advertised more prominently, when a template is actively overridden by the user in a sub-theme, since it can easily be missed.

It could also be used to do a search for a key word, for example "CUSTOM TEMPLATE" to quickly find the relevant position in the source.

Note: This indication should not be enabled for overrides in core or contrib themes, since with for example Bootstrap5, this would result in 50 "CUSTOM TEMPLATE" instances, defeating the purpose.

Steps to reproduce

Add a Twig template override and not immediately see that it was successful.

Proposed resolution

  1. Replace x and * with emojis
  2. Make it clearer that a Twig override attempt was successful, by inserting a single instance of the word "BEGIN CUSTOM TEMPLATE", where it happened.

    This is the current format:

    <!-- THEME DEBUG -->
    <!-- THEME HOOK: 'block' -->
    <!-- FILE NAME SUGGESTIONS:
       x block--b5subtheme-content.html.twig
       * block--system-main-block.html.twig
       * block--system.html.twig
       * block.html.twig
    -->
    <!-- BEGIN OUTPUT from 'themes/custom/b5subtheme/block--b5subtheme-content.html.twig' -->
    

    Maybe add some visual indications that this is using a custom template, as well as a key word like CUSTOM TEMPLATE?

    <!-- THEME DEBUG -->
    <!-- THEME HOOK: 'block' -->
    <!-- FILE NAME SUGGESTIONS:
       โœ… block--b5subtheme-content.html.twig
       โ–ช๏ธ block--system-main-block.html.twig
       โ–ช๏ธ block--system.html.twig
       โ–ช๏ธ block.html.twig
    -->
    <!-- ๐Ÿ’ก BEGIN CUSTOM TEMPLATE OUTPUT from 'themes/custom/b5subtheme/block--b5subtheme-content.html.twig' -->
    

Merge request link

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

โœจ Feature request
Status

Fixed

Version

10.3 โœจ

Component
Themeย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ressa
  • First commit to issue fork.
  • Merge request !6545Added overridden Keyword โ†’ (Closed) created by sumit-k
  • Status changed to Needs review 10 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sumit-k

    Certainly, it does make sense to use the "overridden" keyword instead of a symbol like a sign. This change can enhance clarity, especially for new Drupal users who may not be familiar with the significance of certain symbols.

    Raising a merge request (MR) and providing a screenshot for reference is a proactive approach to communicate the proposed change effectively. It helps stakeholders visualize the suggested modification and provides context for the decision to use the "overridden" keyword.

  • Pipeline finished with Failed
    10 months ago
    Total: 568s
    #92679
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Ruturaj Chaubey Pune, India

    Ruturaj Chaubey โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Success
    10 months ago
    Total: 521s
    #92720
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Ruturaj Chaubey Pune, India

    Added the OVERRIDDEN highlight on the custom theme templates that override the core template.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    What about using emoji instead of x and *

  • Pipeline finished with Failed
    10 months ago
    Total: 495s
    #92983
  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Thanks @sumit-k and @Ruturaj Chaubey, great initial MR and improvements! I agree @larowlan, emojis could help faster separate name options, and active templates, so I added that. Also, I updated the MR to exclude contrib themes, since that would defeat the purpose, by adding a truckload of OVERRIDDEN's everywhere, for example if you are using Bootstrap5 as base theme. Now, OVERRIDDEN is only shown for themes not in core/themes or themes/contrib.

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen
  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Maybe โ–ช๏ธ (https://emojipedia.org/black-small-square) would work better for the listed file names, since โŒ may be misinterpreted? I have updated the MR.

  • Pipeline finished with Failed
    10 months ago
    Total: 473s
    #93260
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    I'm not convinced the use of OVERRIDDEN adds anything but don't feel strongly about it

    What shows if the base template is used?

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Having OVERRIDDEN is a great tool, allowing to search for the word in the source, and immediately jump to the relevant position.

    Shouldn't we assume that core and contrib themes are in folders called something with "core" or "contrib", and likewise normally, and you put custom themes in for example themes/custom, or similarly named folders without "core" or "contrib"?

    The logic in the code is to not include themes in folders with "core" or "contrib" in the path.

    str_contains($template_file, '--') won't work, since a base such as Bootstrap5 will introduce ~50 OVERRIDDEN instances, like I already stated in #8.

    Or am I missing something?

  • First commit to issue fork.
  • Pipeline finished with Canceled
    10 months ago
    Total: 264s
    #93406
  • Pipeline finished with Failed
    10 months ago
    Total: 479s
    #93412
  • Pipeline finished with Success
    10 months ago
    Total: 480s
    #93416
  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Wow @gorkagr, that was fast. Thanks!

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    Test are green now :)

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Updating the Issue Summary, with some points about not adding "OVERRIDDEN" for core and contrib themes, but only for user created themes.

  • Status changed to RTBC 10 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Removing tests tag as it was addressed and can be seen here https://git.drupalcode.org/issue/drupal-3420709/-/jobs/800003

    I think this is a nice little improvement personally.

    I applied the MR locally and verified I'm seeing the green checkmark on templates

    LGTM

  • Status changed to Needs review 10 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    Left another comment on the MR about the instances when we want overridden to show.

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Basically, the goal is to help new users (and everyone else) easier get confirmation, and find an overridden instance in the source in a custom theme that they made.

    There will be other overrides by other themes or modules, but they are not relevant for this use case, and should not trigger the key word. We want to single out the one instance (or few instances) in the source, which is relevant to the user.

    Instead of checking if the theme is not in "core" or "contrib", we could simply check if the theme is in a folder with the string "custom" in its path. This is after all the recommended method:

    It is good practice to place the contributed themes in a sub folder named "contrib" and your own themes in a folder named "custom".

    From Drupal theme folder structure โ†’

    The words placed in the source could then be "CUSTOM THEME", since that would be precise. And yes, if a user does not put the theme in the themes/custom folder, this feature will not work, but I think that's acceptable.

  • Pipeline finished with Success
    10 months ago
    Total: 630s
    #95308
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    A template could also be placed in a module under the /custom folder, so the last commit I'd rather say "CUSTOM TEMPLATE" rather than custom theme as it might point to the wrong location

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    That's a good point @gorkagr, I have updated the MR.

  • Pipeline finished with Success
    10 months ago
    Total: 481s
    #95624
  • Status changed to RTBC 10 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Believe @larowlan question has been answered.

    I showed this to one of our junior devs and they said it helped them.

  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Nice to get confirmation from the target audience that this is an improvement, thanks @smustgrave.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    Thanks, the change to check for custom feels less controversial to me.

    Tagging for frontend framework manager review.

    +1 from me.

  • Status changed to Needs review 10 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Sorry I'm hung up on the custom thing like @larowlan was. I've added a comment on the MR with a suggestion to change this from detecting custom - because that has way too many ways of being wrong in both directions to checking whether the template comes from the active theme.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    Changed to see if the template file that is being checked is from the active directory, so in that case it should be an overridden template, shouldnt it?

    Best

  • Pipeline finished with Failed
    10 months ago
    Total: 314s
    #98450
  • Pipeline finished with Failed
    10 months ago
    Total: 584s
    #98455
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    ok, with my last commit, the test fails.
    But if understand correctly the test flow, the active theme ($variables['directory']) is 'core/modules/system/tests/themes/test_theme/', correct?
    and because of that, the template of check is provided by the theme too (not the node module itself), so the assert ($this->assertStringContainsString("BEGIN OUTPUT from '$template_filename'", $output, 'Full path to current template file found.');) should be changed too?

  • Status changed to Needs work 10 months ago
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    +1 on the idea.

    I'm not super sold on the green tick (at least that show up as a green check on my system) since the comments are already green by default on FF/chrome and the black square stands out better. But it's just cosmetic stuff and it is better than it was so i don't mind enough to change it.

    For "๐Ÿ’ก BEGIN CUSTOM TEMPLATE OUTPUT" it should also update the end output comment no: "END CUSTOM TEMPLATE OUTPUT", I don't think we need the emoji for this one.

  • Status changed to Needs review 10 months ago
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

    Implemented @nod_ suggestion and also updated the test as node__1 there uses a template on the theme and not in core/modules/node/templates, so that's why the test was failing. All should be good, unless we need a new test to cover the case where no template is overridden (maybe with the node_edit_form template?)

  • Pipeline finished with Success
    10 months ago
    Total: 546s
    #98566
  • ๐Ÿ‡ฉ๐Ÿ‡ฐDenmark ressa Copenhagen

    Thanks everyone for great suggestions and refining the code, I'll update the Issue Summary to match the current patch.

  • Status changed to RTBC 10 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Confirmed still seeing the change as mentioned.

    • nod_ โ†’ committed f2ba76f1 on 11.x
      Issue #3420709 by gorkagr, ressa, Ruturaj Chaubey, larowlan, alexpott:...
    • nod_ โ†’ committed e5cd97f1 on 10.3.x
      Issue #3420709 by gorkagr, ressa, Ruturaj Chaubey, larowlan, alexpott:...
  • Status changed to Fixed 10 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    Committed f2ba76f and pushed to 11.x. Thanks!

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

Production build 0.71.5 2024