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.
Issue #1898050 by duellj, Cottser, meeli, dsdart, Fabianx | c4rl: book.module - Convert PHPTemplate templates to Twig.
(as of #42)
Task
Use Twig instead of PHPTemplate
Remaining
- Manual testing (see below)
Theme function name/template path | Conversion status |
---|---|
converted to #type table in #1968982: Convert book theme tables to table #type | |
core/modules/book/templates/book-all-books-block.tpl.php | converted |
core/modules/book/templates/book-export-html.tpl.php | converted |
core/modules/book/templates/book-navigation.tpl.php | converted |
core/modules/book/templates/book-node-export-html.tpl.php | converted |
To test this code...
- Enable the book module
- Create a few pieces of book content (and put them into a book)
- When viewing the content, confirm that
book-navigation.html.twig
is being used to render the links below the node content - Click the 'Printer-friendly version' link at the right of node links to confirm that
- the nodes are being rendered through book-node-export-html.html.twig
- the export wrapper is being rendered through
book-export-html.html.twig
- Place the 'book navigation' block into a region to confirm that book-all-books-block.html.twig is being used
API changes
n/a
Related
#1757550: [Meta] Convert core theme functions to Twig templates
#1987510: [meta] Convert all core *.tpl.php templates to Twig as singular patch
#1936700: Book module usability: Stop using vertical tabs
Comment | File | Size | Author |
---|---|---|---|
#38 | 1898050-38.patch | 15.39 KB | star-szr |
#38 | interdiff.txt | 1.03 KB | star-szr |
#30 | 1898050-30.patch | 15.48 KB | star-szr |
#30 | interdiff.txt | 982 bytes | star-szr |
#26 | 1898050-25-twig-book.patch | 15.46 KB | duellj |
Comments
Comment #1
c4rl CreditAttribution: c4rl commentedTagging
Comment #2
meeli CreditAttribution: meeli commentedi'll give this a go!
Comment #3
meeli CreditAttribution: meeli commentedalrighty!! i've added the twig template files and tested it and the markup and functionality both look pretty much 100%. have checked through the templates and A/B compared them to the tpl.phps and it's looking pretty much smooth.
the last thing that needs to be done here is converting theme_book_admin_table in book.admin.inc to be split into a preprocess function and a separate twig template file.
Comment #4
meeli CreditAttribution: meeli commentedsee corresponding issue in twig for the theme function conversion: http://drupal.org/node/1912660
Comment #5
star-szrThis looks like a great start, thanks @meeli. Sending to testbot.
Comment #7
star-szrJust some notes after reviewing the patch a bit closer, thanks again @meeli. The test failures just seem to be about missing previous/next links. I haven't had a chance to run the tests locally yet.
These will need to be filled out.
These @see references should end in parens (i.e.
@see template_preprocess()
per http://drupal.org/node/1354#see.Comment #8
star-szrUnassigning since it's been a while. @meeli - If you'd like to take this up again, feel free to re-assign to yourself.
Comment #9
duellj CreditAttribution: duellj commentedI'm going to give this one a go. It looks like book_admin_table can be replaced with a render array with a #table element, I might attempt to remove that theme function too.
Comment #10
duellj CreditAttribution: duellj commentedHere's the first pass at reworking this patch. Fixes failing tests (mostly whitespace issues) and updates doc blocks for preprocess functions to new standards.
I'm still working on converting book_admin_table to a #theme render array.
Comment #11
duellj CreditAttribution: duellj commentedComment #12
duellj CreditAttribution: duellj commentedConverted theme_book_admin_table to a #theme render array. Another theme function bites the dust :).
Note that there's an outstanding issue when reordering book pages: #1933190: Fatal error when reordering book pages in book admin . There's no test coverage for the error, so everything should pass.
Comment #13
jenlamptonThanks @duellj!
I'm working on re-rolling this now with a few changes.
- twig filters should not have a space before or after the pipe
- there was an extra greater-than symbol in one of the templates
- one of the preprocess functions needs a little tweaking
- docs need to be updated for all templates and preprocess functions (variable definitions added)
And would be nice:
- change the template names not to include "html" or "block" since those are redundant and less words = better :)
Comment #14
jenlamptonmeant to change status too
Comment #15
duellj CreditAttribution: duellj commentedI've updated so patch applies cleanly now, and applied all the changes from #13 (except for the template renames).
Comment #15.0
duellj CreditAttribution: duellj commentedhow to test, and API changes
Comment #15.1
star-szrAdd conversion summary table
Comment #16
star-szrTagging.
Comment #16.0
star-szrAdd pointer to test steps
Comment #17
duellj CreditAttribution: duellj commentedOn the advice of Cottser, moving conversion of theme_book_admin_table() to #1968982: Convert book theme tables to table #type
Comment #19
star-szrThanks @duellj, that's much easier to review :)
I haven't done manual testing yet but I found some documentation tweaks, I think as long as manual testing checks out we'll be good to go after the below are updated:
These should be .html.twig, not .twig.html.
The descriptions of the variables need to be a complete sentence (in this case, start with a capital letter) per http://drupal.org/node/1354#drupal. We're also trying to avoid using 'array', 'string', 'object' in Twig docblocks per Twig coding standards. I suggest "The top-level book links".
Similar to the above, here's an alternate description (needs to be wrapped at 80 characters per http://drupal.org/node/1354#drupal): "A flag indicating whether the current display language is a right to left language."
"URL to the home page." would be a bit friendlier here.
I'm not sure why these slashes are here, I think they can be removed.
(needs to be wrapped at 80 characters)
A flag indicating that the previous, parent or next data has a value.
Let's remove the word entity here per Twig coding standards.
Comment #20
duellj CreditAttribution: duellj commentedForgot to move tests over to #1968982: Convert book theme tables to table #type. Tests should pass now. Also implemented documentation tweaks from #19. Thanks for the excellent review Cottser!
Comment #21
duellj CreditAttribution: duellj commentedA patch would help :)
Comment #23
duellj CreditAttribution: duellj commentedFor some reason the table #type conversion snuck back in, which caused tests to fail. Also fixed comment wrapping from #19.5
Comment #24
star-szrCode and documentation wise this looks ready to go, just needs manual testing and markup comparison - testing steps are in the summary thanks to @duellj!
Comment #25
dsdart CreditAttribution: dsdart commented4. Click the 'Printer-friendly version' link at the right of node links to confirm that
-- has 2 lines with class starting with "section-"; the second one is missing an id - see section-error.png
5. Place the 'book navigation' block into a region to confirm that book-all-books-block.html.twig is being used
-- nav id is incorrect - see book-block-error.png
Comment #26
duellj CreditAttribution: duellj commentedThanks for the great review dsdardt! This is why we do manual testing.
Patch contains fixes for both errors. For the second fix, had to cast the variable to a string to print the 0 (see #1970960: Twig implementation does not print int(0)). The ids are actually wrong here, but I opened a new issue for that: #1970968: Book navigation block ids don't match book ids.
Comment #27
duellj CreditAttribution: duellj commentedComment #28
star-szrThanks @duellj! We just need someone to manually test this one again.
Comment #29
star-szrI'm going to test and review this tonight.
Comment #29.0
star-szrUpdate conversion summary table
Comment #29.1
star-szrRemoved API changes and moved (now empty) API changes section down, latest patch doesn't make any API changes.
Comment #30
star-szrI added two books with content of varying levels of depth, tested all the templates and compared the markup thoroughly. I only found two *tiny* tweaks so I'm rolling a new patch and marking RTBC.
The tweaks two:
Ship it! Fantastic work @duellj :)
Comment #32
star-szr#30: 1898050-30.patch queued for re-testing.
Edit: c'mon test bot, not a good time.
Comment #33
star-szrThat's better.
Comment #33.0
star-szrUpdate template names in testing steps
Comment #34
c4rl CreditAttribution: c4rl commentedPer #1757550-44: [Meta] Convert core theme functions to Twig templates, retitling to indicate this issue applies to .tpl.php templates rather than theme_ functions (though there are no theme_ functions in this module).
Comment #35
alexpottThe .tpl.php conversions here look good... and the patch still applies to head.
+1
Comment #36
alexpottHowever in line with everything that's being said... we need to profile the patch.
Comment #37
star-szrThis is next on my list for profiling.
Comment #37.0
star-szrUpdated issue summary.
Comment #38
star-szrFirst of all, rolling in two small changes here:
book-export-html.html.twig:
The extra space was in the .tpl.php version but I see no reason to keep it.
book-all-books-block.html.twig:
The printing 0 issue has been fixed so the concatenation with an empty string is no longer needed.
Now, on to the profiling results.
Profiling done with Stark theme and APC class loader enabled.
Created 10 book nodes and placed them in a book with multiple levels of nesting.
book-all-books-block and book-navigation conversions
Tested on a node page with both templates in use - book navigation block placed in sidebar and set to always display.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518eee9367b5d&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518eee9367b5d&...
The numbers for this were coming back slightly faster on a consistent basis. Nice!
book-export-html and book-node-export-html (printer friendly view)
Displaying the printer friendly view for 10 book nodes.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518fb7370421e&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518fb7370421e&...
Twig is slower in this case, the numbers fluctuated a bit in the 1.1% to 1.5% range but landed on 1.4% several times so I went with that.
Comment #39
alexpottI think we need to understand what's going on in book-export-html and book-node-export-html (printer friendly view) and why it is slower.
Comment #40
Fabianx CreditAttribution: Fabianx commentedI can explain that in parts:
We call isset() on the EntityBC DEcorator twice, which seems to add around 0.9 ms already using 11 nodes.
This is perfectly logical (as twig needs to check access to properties) but a problem of EntityBC Decorator, which should einer not call __get on isset() or at least cache the result using pass-thru caching.
The rest is the TWIG subszstem being loaded for the first time (0.9ms) and class loading (0.9ms), which is one time only and legitimate (in other benchmarks this 2ms static overhead is already factored out.)
So I would say despite that EntityBCDecorator bites us, this is fine ...
To make things more even and reduce the static overhead, I propose to do the theme('node') within the template php or like old hook init or somewhere else this is always called even if book_export_html does not go through the normal layers.
This would make things comparable as the twig overhead is factored out. It can be easily seen in http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?symbol=twig_render_... that the overhead of the Twig Init is much more than normally.
Putting back to RTBC for alex to do a call on this.
Thanks,
Fabian
Comment #41
Fabianx CreditAttribution: Fabianx commentedDiscussed with alexpott and he is fine with that now.
I have opened: #1998684: EntityBCDecorator::__isset is very slow as follow-up to fix EntityBCDecorator.
The rest is just the one-time twig overhead, so we can go ahead here!
Comment #42
alexpott+1. Ready for #1987510: [meta] Convert all core *.tpl.php templates to Twig as singular patch
Comment #42.0
alexpottUpdate conversion table
Comment #43
alexpottCommitted f2aa485 and pushed to 8.x. Thanks!
Comment #44.0
(not verified) CreditAttribution: commentedAdded suggested commit message