Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Updated: Comment #N
Problem/Motivation
Twig has this concept of template include and extends. Currently we have to specify the complete path to a template file to include it. This fact, makes this feature of twig unusable for contrib module and themes.
Proposed resolution
Add a twig namespace for each module and theme (somewhere in CoreServiceProvider::registerTwig).
Syntax in templates without namespaces.
{% extends "core/themes/bartik/templates/node.html.twig" %}
Syntax in templates namespaces.
{% extends "@bartik/node.html.twig" %}
Remaining tasks
Implementations, Tests, Docs
User interface changes
None.
API changes
None. We do not use include or extends in our templates atm.
Comment | File | Size | Author |
---|---|---|---|
#14 | interdiff.txt | 3.81 KB | star-szr |
#14 | 2143557-14.patch | 8.66 KB | star-szr |
#13 | interdiff.txt | 1.59 KB | star-szr |
#13 | 2143557-13.patch | 8.41 KB | star-szr |
#11 | drupal-modules_and_themes_as_twig_namespaces-2143557-11.patch | 8.41 KB | webflo |
Comments
Comment #1
chr.fritschHere is a initial patch. I don't know how to write a test for this, so maybe someone could help
Comment #2
webflo CreditAttribution: webflo commentedComment #3
star-szr@dmouse and @jmolivas have done some (as far as I know) related work around this, maybe find them on IRC to share knowledge :)
https://github.com/hechoendrupal/slang/blob/master/lib/Drupal/slang/Plug...
Comment #4
webflo CreditAttribution: webflo commented#2109287: Replace list_themes() with a service. is in. Related change notice. https://drupal.org/node/2150863
Comment #5
star-szrPatch still applies.
Comment #6
chr.fritschComment #7
joelpittetI'm curious what testbot thinks here. That patch is small, but if it works wooo!
Comment #8
dawehnerI love this as it would make the life much easier for people which try to integrate some bundles into drupal.
Comment #9
webflo CreditAttribution: webflo commentedNow with tests.
Comment #10
dawehnerIt would be cool to inject these new dependencies.
What do you think about allowing people to use templates outside of the "templates" directory
It would be also cool to have a direct test which just checks which template is used, not passive like this.
Comment #11
webflo CreditAttribution: webflo commented1. Ok, done :)
2. Sure its possible but, we already enforce the templates folder for themes already. Take a look at drupal_find_theme_templates().
3. I am not sure what you mean. The first part of the tests does this already. We could remove the seconds part completely. Because this tests the twig language/syntax and not the actual namespaces.
Comment #12
dawehnerInteresting, I was not aware of that.
I agree this is enough test coverage.
Comment #13
star-szrUpdated to fix two very minor nitpicks (below). I'd argue that this could be changed to a task, it will especially be useful for subthemes. Thanks for this @webflo and @chr.fritsch!
Should be .html.twig instead of .tpl.php.
Missing period per https://drupal.org/node/1354#drupal.
Comment #14
star-szrMore docs updates and minor changes. The only material change is getting rid of a call to theme() in the tests, see #2173655: Refactor theme() to _theme(); make it a private API to discourage module developers from circumventing the renderable build system. I probably should have just made this a patch review, hope you don't mind @webflo :)
Edit: Interdiff is from #11.
Comment #16
dawehnerFine!
Comment #17
webflo CreditAttribution: webflo commented@Cottser thanks for moving this forward!
Comment #18
webchickWow! That syntax is definitely much nicer.
While this is sorta feature-y, it's also required to finalize the Twig API, so changing to a task.
Committed and pushed to 8.x. Thanks!
Comment #19
webflo CreditAttribution: webflo commentedThanks!
Comment #20
webflo CreditAttribution: webflo commentedComment #22
markhalliwellThis is awesome. When we get #1308152: Add stream wrappers to access extension files in, we should probably change this format to use the stream wrapper syntax and just pass the path to be resolved by those.
Comment #23
Jeff Burnz CreditAttribution: Jeff Burnz commentedI can't seem to get this working:
{% extends "@at_core/block.html.twig" %}
This just prints that out where the content should be.
Trying to change the branding block to extend my block.html.twig, both templates are in the base theme /templates directory, but of course my default active theme is a sub theme.
Comment #24
Jeff Burnz CreditAttribution: Jeff Burnz commentedOK, the problem is clear - you can't have templates in sub directories:
adaptivetheme/at_core/templates/block/block.html.twig will not work.
Probably something to look at in #2291449: Add Twig template inheritance based on the theme registry, enable adding Twig loaders ?
Leaving active for now, however this is a regression from D7.
Comment #25
star-szrWith the theme registry loader you could just do
{% extends "block.html.twig" %}
, currently you'd have to add the 'block' directory{% extends "@at_core/block/block.html.twig" %}
This is because the namespace maps directly to the templates directory and nothing more.
I think your use case will be fixed by the theme registry loader so re-closing. Thanks!
Edited to expand code samples and add code tags.