Problem/Motivation
$current_theme
from D7 has no equivalent in Drupal 8.
Templates knowing what theme is active is useful when paired with the functionality we see with modules such as Theme Key (https://www.drupal.org/project/themekey), where complex theme switching rules are used. There are also use cases like the front end theme loading a piece of content using the admin theme and vice versa. In these cases markup changes may be desired, but the template needs context to understand how it is being presented.
Proposed resolution
Add an active_theme twig function.
Remaining tasks
User interface changes
API changes
The new twig function, active_theme.
Beta phase evaluation
Issue category | Task because we are adding a function to twig that is missing as compared to D7. |
---|---|
Issue priority | Major. The parent is Major because this and some related functions that existed in D7 are missing from the theme system in D8. Lack of this particular functionality prevents D7 modules which use it from being ported to D8. |
Disruption | This should produce little disruption, except to patches on similar issues that are adding twig functions. |
Comment | File | Size | Author |
---|---|---|---|
#65 | add_an_active_theme-2416831-65.patch | 4.33 KB | cilefen |
#65 | interdiff.txt | 2.78 KB | cilefen |
#61 | add_an_active_theme-2416831-61.patch | 5.04 KB | cilefen |
#61 | interdiff.txt | 2.86 KB | cilefen |
#60 | add_an_active_theme-2416831-60.patch | 5.09 KB | cilefen |
Comments
Comment #1
cilefen CreditAttribution: cilefen commentedComment #2
cilefen CreditAttribution: cilefen commentedComment #3
davidhernandezComment #4
cilefen CreditAttribution: cilefen commentedComment #5
cilefen CreditAttribution: cilefen commentedComment #6
star-szrThanks for getting this going @cilefen!
Should we inject the theme manager?
I think the tests could be novice.
Comment #7
cilefen CreditAttribution: cilefen commented@Cottser: Do you why there is both a setGenerators() and setLinkGenrator()? It seems that now we will inject three services, all of them should be in the same method.
Or we could extend ContainerAware and set the entire container as it is likely we will need more services in the future.
Comment #8
star-szrYeah, I noticed that @cilefen. Might be worth doing a
git blame
to try and find out why they were injected that way.Comment #9
cilefen CreditAttribution: cilefen commented@Cottser Having looked over the issues that inserted these methods, the redundancy was simply overlooked in review.
Comment #11
cilefen CreditAttribution: cilefen commentedComment #12
cilefen CreditAttribution: cilefen commentedTests: the way you find out the patch doesn't work. Ha.
Comment #13
cilefen CreditAttribution: cilefen commentedThe theme name for the assert should be found from the theme manager service.
Comment #14
cilefen CreditAttribution: cilefen commentedComment #15
tim.plunkettThis is odd to me. Everywhere else we have a single method per service. Why the change?
Comment #16
cilefen CreditAttribution: cilefen commentedIt looked like a mistake to me. There is a method called setGenerators() that has a function comment making it look like a constructor. It sets only one generator. I don't care either way but I will change its name when I put this back to three methods.
Comment #17
cilefen CreditAttribution: cilefen commented@tim.plunkett I forgot to thank you for reviewing.
Comment #18
cilefen CreditAttribution: cilefen commentedOops
Comment #20
akalata CreditAttribution: akalata commentedRerolling #18.
Comment #22
JeroenTCreated reroll.
Patch attached.
Comment #23
star-szrThese need newlines at the end of the file per https://www.drupal.org/coding-standards#indenting.
Comment #24
tadityar CreditAttribution: tadityar commentedAdded those newlines.
Comment #25
tadityar CreditAttribution: tadityar commentedAdded too many spaces
Comment #26
cilefen CreditAttribution: cilefen commentedComment #27
cilefen CreditAttribution: cilefen commentedComment #28
Noe_ CreditAttribution: Noe_ at Devhouse Spindle commentedRerolled the patch.
Comment #29
cilefen CreditAttribution: cilefen commented@Noe_ That is nice work for a first reroll! Normally, you should avoid changing the order of statements in a reroll. In this case, it doesn't matter much. So, I have some tiny requests below. Please include an interdiff with the next patch.
There should be a comma after the last element of a multiline array.
An extra space crept in here. I think it was in a prior patch.
Comment #30
cilefen CreditAttribution: cilefen commentedThe next steps are accessible to a novice.
Comment #31
vasi CreditAttribution: vasi at Evolving Web commentedComment #32
fengtanWe are working on this at DrupalCon Los Angeles
Comment #33
HakS CreditAttribution: HakS at Rootstack commentedComment #34
vasi CreditAttribution: vasi at Evolving Web commentedAdded trailing comma, removed extra space.
Comment #35
m4oliveiHey now, at DC LA, we're gonna go ahead and review this guy.
Comment #36
fengtanComment #37
vgriffin CreditAttribution: vgriffin as a volunteer commentedReviewing the patch as well, also at DC LA
Comment #38
vgriffin CreditAttribution: vgriffin as a volunteer commentedReviewing the patch as well, also at DC LA
Comment #39
vgriffin CreditAttribution: vgriffin as a volunteer commentedReviewed with others at DC Los Angeles. Ready for commit.
Comment #40
lauriiiGood work on the patch & review folks! I'm sorry but I Just wanted to point few nitpicks I found:
These could be in alphabetical order (t before u)
Could we use the new array syntax here.
Comment #41
vasi CreditAttribution: vasi at Evolving Web commentedChanged to new array syntax, re-ordered namespace imports to be in alphabetical order.
Comment #42
Todd Zebert CreditAttribution: Todd Zebert at Miles commentedI've reviewed the interdiff and the only differences are the use ordering, and the array syntax, as expected.
Comment #43
star-szrThanks everyone for working on this!
I don't think we need the link generator anymore because of #2335661: Outbound path & route processors must specify cacheability metadata.
Comment #44
lauriiiJust wanted to leave a comment here to tell that you are making good progress here and this seems to be really close to be ready to be commited! Keep up the good work folks!
Comment #45
vasi CreditAttribution: vasi at Evolving Web commentedI've removed the link generator.
Comment #46
star-szrI agree with @lauriii this looks very close! Thanks all.
If we are changing this line to add the trailing comma I think we should also change the array to short array syntax. But only on this line :) The motivation is that the next line uses short array syntax here, and we are already changing the line.
This method should probably be called getActiveTheme, especially because the docblock starts with "Gets" :)
Comment #47
lauriiiComment #48
star-szrThis looks great to me and I tested it as well. I can't find any faults. Tests look good. Thanks @all!
Comment #49
Noe_ CreditAttribution: Noe_ at Devhouse Spindle commentedThis part is inconsistent. I think we should only do new array syntax or old array syntax. But not both intermingled.
So make the first three also new syntax.
Comment #50
lauriii@noe_ thanks for the review! We have agreed at least on the theme system to start using the new syntax. However we don't want to change existing lines to use the new array syntax at once because it would make us write lots of rerolls for patches because they would contain changes for lines that they are actually not changing. We have agreed to be a little inconsistent on this matter since it makes things lots of easier and this is the way this has worked also on other issues. I'm setting this to Needs review, maybe you want to set this back to RTBC if you agree with me?
Comment #51
Noe_ CreditAttribution: Noe_ at Devhouse Spindle commented@lauriii If you guys decided on that I am fine with it, and I only said I wanted to change this in the function that was changed. There are lots of places the other array format is used and I didn't specify those.
It was just that I thought it might be a good idea.
just my 0.02c
Comment #52
alexpottThe issue summary is missing a justification of why we need this? It seems strange that a twig template needs to know the current theme.
Comment #53
davidhernandezComment #55
Noe_ CreditAttribution: Noe_ at Devhouse Spindle commentedWeird, the bot failed testing.
I thought that there must be a conflict with the 8.0.x branch so I started rerolling the patch.
But after applying the patch successfully and rebasing it with 8.0.x it did the 3-way merge and it worked like a charm.
So I added the new patch as an attachment to this comment, but I couldn't figure out how to make an interdiff because the rebase succeeded.
Comment #58
davidhernandez#55 looks right, compared to #47.
Comment #59
alexpottThis is now unrelated and unnecessary API change.
Also rather than adding the additional web test method this looks eminently unit testable. We could add a test method to
\Drupal\Tests\Core\Template\TwigExtensionTest
to replace the added web test.Comment #60
cilefen CreditAttribution: cilefen commentedThe test needs work.
Comment #61
cilefen CreditAttribution: cilefen commentedThis is a crude, but correct, test. We could remove some repetition by setting some class properties in a setUp method. And, the mock builder setup chaining could be improved.
Comment #62
cilefen CreditAttribution: cilefen commentedIf you are new to phpunit tests, you run this test like:
phpunit -c core/ --filter testActiveTheme
Comment #63
cilefen CreditAttribution: cilefen commented@alexpott via IRC mentioned that we cannot change setGenerators as it is in HEAD. It is preferable to re-add a setThemeManager method to do what is needed.
Comment #64
star-szrThanks @cilefen, cannot in general or cannot in this issue because it's out of scope?
Comment #65
cilefen CreditAttribution: cilefen commented@Cottser I think I should have written "ought not in this issue as such", not "cannot".
Comment #66
star-szrYeah that makes a lot more sense, thanks @cilefen. I can file a separate issue for that change.
Comment #67
lauriiiLooks good!
Comment #68
alexpottCommitted ffe9803 and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation to the issue summary.
Comment #70
davidhernandezThis should have had a change record. I added one.
Comment #71
star-szrActually I'm not seeing anything else in core.services.yml that has a setter injecting multiple services, so renaming setGenerators seems to make more sense given that but I'm no injection pro. Feel free to disagree over here: #2496801: Change setGenerators to setUrlGenerator on TwigExtension :)
Thanks all!