Issue #1898426 by Floydm, Cottser, jenlampton, chrisjlee, kattekrab, jerdavis, derheap, hussainweb: link.module - Convert theme_ functions to Twig.
Task
Use Twig instead of PHPTemplate
Remaining
- Revised patch needed based on feedback in #28
- Manual testing (see below)
| Theme function name/template path |
Conversion status |
| theme_link_formatter_link_separate |
converted |
To test this code...
1) enable the link module (admin/modules)
2) add a link field to a content type (admin/structure/types)
3) change the display format to 'Separate title and URL'
3) add a piece of content (node/add)
4) view the piece of content to confirm the link is being rendered through link-formatter-link-separate.html.twig
#1757550: [Meta] Convert core theme functions to Twig templates
Comments
Comment #1
c4rl commentedTagging
Comment #2
rcaracaus commentedComment #3
rcaracaus commentedComment #4
floydm commentedI've been trying to learn a bit about D8 and Twig this evening, so I checked out the Twig sandbox and tried following the conversion instructions for the simplest module needing to be ported I could find, this one. My patch is attached. We'll see if this is at all on the right track.
Comment #5
floydm commentedYay, I may not have totally completed the job, but at least I didn't blow anything up.
BTW I punted on dealing with l() since it looks like there are some big discussions happening around it.
#1812562: Create best practices about use of the l() function and the url() function in templates
#1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig
Comment #6
jenlamptonHi Floydm, and thanks for jumping right in! :)
A few notes..
- In the Twig sandbox all the templates lived in the stark theme, but as part of the move to core we're putting them "back where they belong" so in this case there needs to be templates directory added into link module, and your new template goes there.
- We also need to remove the theme function, since we are replacing it with the preprocess + template file. (We did not remove the theme function in the sandbox, for testing purposes)
- We're going to remove the process layer, so we need to remove any reference to template_process
* @see template_process()- We changed our minds about checking variables a few times, but in the end we decided not to use 'is defined' anymore in
{% if title is defined %}- When using a filter, we don't want any space around the pipe character
{{ title | striptags }}- But, rather than use a striptags filter, let's preprocess that baby so we can just print title.
- We need to add a 'template' in our hook_theme implementation.
Here's a re-roll of this patch, with the above changed. Let's see if it works!
Comment #8
floydm commentedThanks @jenlampton, for the explanation.
I updated my twig sandbox and applied this patch in the hope of helping take it farther, but I think I'm in over my head. It looks like this is failing in the unit tests, so I figured out how to run them on my box today, which is *something*, but ... I'm not quite sure how/where to take it from here. More reading up to do, I guess. :)
Comment #9
jenlampton@Floydm,
The next step would be to take this patch and apply it to a local install of Drupal 8 HEAD (not the sandbox), and then run only the test(s) that failed. I'd start with LinkFieldTest. Find out what that code is testing, and see if those conditions actually exist after this patch.
From the test result it looks like the first test is looking for the exact string
<div class="link-item"><div class="link-url"><a href="http://www.example.com/content/articles/archive?author=John&year=2012#com">http://www.example.com/content/articles/archive?author=John&year=2012#com</a></div></div>.If we have whitespace in there, or are missing a class or something, those are reasons we could be causing this test to fail.
That might be a simple fix, do you want to give it a shot?
Comment #10
floydm commentedHi @jenlampton. Thanks for the follow up.
I don't quite get what is going on, but I get errors like "Fatal error: Call to a member function entityType() on a non-object in /www/d8/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php on line 475" and "FATAL Drupal\link\Tests\LinkFieldTest: test runner returned a non-zero error code (255)." when I try to run the test on my box with "php ./run-tests.sh --module --verbose link". Then I get all sorts of failures in the tests, like userLogin fails. Is that not the correct way to run the test?
So I haven't tested this locally, but I *think* it is just a whitespace issue in the tests. The new template introduces whitespace where previously there was none, so the attached patch attempts to copy the whitespace changes from the template into the tests.
Comment #12
star-szr@Floydm - Thanks for your work on this! I think it will probably be more instructive to run the relevant tests through the UI after enabling the Testing module.
Comment #13
floydm commentedThank you, @ jenlampton and @Cottser, for your help.
This now passes all tests on my box. There was one legitimate error in the template (two closing a tags) and the rest of the issues were new white space and carriage returns. I fixed the closing tag and added the white space to the tests.
Comment #14
aspilicious commentedWhy write all this logic in one line. I would place the actual printing of the div between the if statement
You need to end a file with a newline
Comment #15
floydm commentedComment #16
floydm commentedComment #17
floydm commentedhang on. fixing newline too.
Comment #18
floydm commentedAdded newline to template.
Comment #20
floydm commentedThis patch passes all tests on my machine.
Comment #21
star-szrLooking at sandbox issues and commits, I couldn't find anyone else to credit. So no commit message added to the summary for this issue.
Comment #22
jenlamptonThis is looking good, but I'm not sure about the way new lines are added in the tests. I mean, it passed, so that's obviously good :) But can we check in the other core tests and see how the \n is usually added? Maybe we can just stick it on the end of the previous line, like:
That would look a little better, anyway. But I'm curious to see how it's done elsewhere.
Comment #23
star-szrThe block rendering tests use
implode()to add the newlines, not sure if that's the rule or the exception though. See the interdiff in #1898034-16: block.module - Convert PHPTemplate templates to Twig.Comment #24
floydm commentedAgreed, @jenlampton, I'm not fond of the newlines either.
Thank you, @Cottser, for pointing out the block example. That seems like a better approach to me but we'll see what others think.
Rerolled the patch with the approach to newlines found in the blocks interdiff referenced in #23.
Comment #26
star-szr@Floydm - You're probably missing some blank lines now. I found it helpful to use
debug()while working on the test to compare the expected output vs. the actual output.Comment #27
floydm commentedNo I just made a dumb mistake. This time after fixing it I ran it to verify it passes all the tests, then built the patch and applied it to the 8.x branch to verify it applied cleanly, then ran the tests against the 8.x branch to verify it passed there too. So, really, this time it should work.
Thank you both, @Cottser and @jenlampton, for your patience and coaching.
Comment #28
steveoliver commentedHere's a thorough review from my end.
On the test:
I think we should /not/ change the test and instead use the
{% spaceless %}tag around the entire codeblock of the template, as previously the markup has had no whitespace.For the preprocess docblock:
As per #1913208: [policy] Standardize template preprocess function documentation:
I think we should edit to this (may or may not want to add the @param $variables in):
For the preprocess function:
l()/theme('link') should be done soon.
As per Best practices - preprocess functions and templates:
Let's make this l() into a render array, like this:
And for the template:
Since, once l()/theme('link') works as expected, we'll have all these variables, lets document them such:
Then wrap {% spaceless %} ... {% endspaceless %} around all 6 lines.
NOTE: Until '#type' 'link' returns an active class for links, this won't pass. So we can either:
a. Keep it failing and dependent upon #1888424: Make Drupal's URL generation logic available to HttpKernel, and minimize code repetition/divergence and/or #1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig, or
b. Insert a hack with @todo remove once above issues are fixed.
Comment #29
star-szrAny takers for rolling a new patch based on the feedback in #28? If you have questions you can always stop by #drupal-twig on IRC :)
Comment #30
nikkubhai commentedI will try.
Comment #30.0
nikkubhai commentedadd how to test
Comment #31
nikkubhai commentedComment #32
star-szrTagging.
Comment #33
star-szrRolling a new patch based on @steveoliver's feedback.
Comment #34
star-szrLeaving the l() in for now. It's not perfect but at this point I'm not sure we have the luxury of waiting for #1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig.
The automated testing for this output is quite thorough as we've discovered, so I'm removing the manual testing tag I added last night.
Comment #36
star-szrOops, two interdiffs.
Comment #37
star-szrFixed indent level within {% spaceless %} tag.
Comment #38
chrisjlee commentedSeems to work nicely!
Comment #39
chrisjlee commentedFollowup docs tweaks as per twig docs
Comment #40
star-szrThanks @chrisjlee! I agree this is good to go.
Comment #42
star-szr#39: 1898426-39.patch queued for re-testing.
Comment #43
star-szrComment #43.0
star-szrAdd conversion summary table
Comment #44
star-szr#1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig is resolved :)
What do we want to do with this now? #type link?
Comment #45
c4rl commentedRelated #1985470: Remove theme_link()
Comment #46
c4rl commentedPer #1757550-44: [Meta] Convert core theme functions to Twig templates, retitling to indicate this issue applies to theme_ functions, which are lower in priority than PHPTemplate conversion issues.
Comment #47
alexpottComment #48
jstoller#39: 1898426-39.patch queued for re-testing.
Comment #49
jerdavisProfiling
Comment #50
jerdavisResults, tested as described in summary
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519fbb3f5c2a0&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519fbb3f5c2a0&...
Comment #51
kattekrab commentedManually testing this now.
Comment #52
kattekrab commentedHTML output identical.
See attached diff.
Comment #54
jerdavisBack to RTBC as testbot wasn't actually supposed to test that. See patch in #39
Comment #54.0
jerdavisRemove sandbox link
Comment #55
kattekrab commentedthanks @jerdavis
Comment #56
star-szr#39 doesn't apply, needs a reroll.
Comment #57
star-szrUnassigning, anyone can work on the reroll.
Comment #58
scor commentedregarding #39:
This issue was fixed: #1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig.
Comment #59
derheap commentedComment #60
derheap commentedRerolled the patch.
Removed the following comment.
As far I have understood l(), the call is ok by now.
Comment #61
derheap commentedMade an error with the patchfile.
Removed comment is now really removed.
Comment #62
joelpittet@derheap seems like something went a bit sideways with the re-roll. There is an extra bit at the end with "NodeTestStorageController.php" and "Prepares variables for separated link field templates." rewrite is missing.
Would you mind trying again from #39?
http://drupal.org/patch/reroll
I have a feeling maybe your repo wasn't rebased to origin/8.x, just a guess though...
Comment #63
joelpittetretagging
Comment #64
hussainwebAttaching a reroll of patch from #39 and removed the comment as per #60.
Comment #65
derheap commented@joelpittet: I had some problems with merge conflict. I am just a novice …
@hussainweb: thanks for fixing my reroll.
Comment #66
star-szrOkay so this has been profiled, would like one more round of manual testing please.
Thanks everyone for moving this forward :)
Comment #67
joelpittet@derheap no worries, thanks for the patch:) Merge conflicts can sometimes suck
Comment #67.0
joelpittetUpdated issue summary.
Comment #67.1
star-szrUpdate commit message
Comment #68
hussainwebI just ran a quick test.
After above steps, I changed the twig file with arbitrary text.
Finally, cleared the cache and refreshed the content page and verified the arbitrary text:
Comment #69
hussainwebTest result in comment #68.
Comment #70
star-szrSorry, you can't RTBC your own patch @hussainweb. Thanks for testing! We should also ensure the markup is the same before and after applying the patch :)
Comment #71
hussainwebGot it. I thought it would be fine here because I just rerolled the patch. I will try and compare the markup shortly.
Comment #72
hussainwebThis is the default markup:
This is the markup after applying the patch and clearing the cache. I also added random text and removed it from the twig file to ensure it was being used.
They match exactly as far as I can see.
Comment #73
eromero1 commentedThe patch was implemented and tested and ran perfectly. Everything seems to be working.
Comment #74
alexpottCommitted 6ff8caa and pushed to 8.x. Thanks!
Comment #75
kattekrab commentedwhoohoo! Thanks @alexpott - and thanks everyone for writing, testing, re-rolling, re-testing!
+ Snacks for the testbots :)
Comment #76.0
(not verified) commentedCapitalization in commit message