Task

Use Twig instead of PHPTemplate

Remaining

  • Replace all theme functions and templates with .html.twig equivalent templates
  • Add new preprocess functions for the .html.twig equivalent templates
  • Update all hook_theme definitions

Testing steps

View source and look at meta tags, script tags, and link tags.

Related

#1885560: [meta] Convert everything in theme.inc to Twig
#1825090: Remove theme_html_tag() from the theme system

Comments

artofeclipse’s picture

Assigned: Unassigned » artofeclipse
Status: Active » Needs review
StatusFileSize
new4.33 KB

patch for core.

Status: Needs review » Needs work
Issue tags: -Twig

The last submitted patch, convert_theme_html_tag_to_twig-1939104-1.patch, failed testing.

artofeclipse’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Issue tags: +Twig

The last submitted patch, convert_theme_html_tag_to_twig-1939104-1.patch, failed testing.

artofeclipse’s picture

Status: Needs work » Needs review
StatusFileSize
new3.81 KB

apply for a new patch

Status: Needs review » Needs work

The last submitted patch, drupal-1939104-5.patch, failed testing.

hefox’s picture

edit Issue fixed via #1942490: Make Twig engine available during install

html_tag is used to create a head tag on install (maintenance) page.

Twig is registered via CoreBundle build(), registerTwig(); it is not called during install.php by the time that error is produced.

It works if add

use Symfony\Component\DependencyInjection\Definition;

and copy and paste contents of registerTwig() to install.core.inc a bit after line 334

    $container->register('twig.loader.filesystem', 'Twig_Loader_Filesystem')
      ->addArgument(DRUPAL_ROOT);
    $container->setAlias('twig.loader', 'twig.loader.filesystem');

    $container->register('twig', 'Drupal\Core\Template\TwigEnvironment')
      ->addArgument(new Reference('twig.loader'))
      ->addArgument(array(
        // This is saved / loaded via drupal_php_storage().
        // All files can be refreshed by clearing caches.
        // @todo ensure garbage collection of expired files.
        'cache' => settings()->get('twig_cache', TRUE),
        'base_template_class' => 'Drupal\Core\Template\TwigTemplate',
        // @todo Remove in followup issue
        // @see http://drupal.org/node/1712444.
        'autoescape' => FALSE,
        // @todo Remove in followup issue
        // @see http://drupal.org/node/1806538.
        'strict_variables' => FALSE,
        'debug' => settings()->get('twig_debug', FALSE),
        'auto_reload' => settings()->get('twig_auto_reload', NULL),
      ))
      ->addMethodCall('addExtension', array(new Definition('Drupal\Core\Template\TwigExtension')))
      // @todo Figure out what to do about debugging functions.
      // @see http://drupal.org/node/1804998
      ->addMethodCall('addExtension', array(new Definition('Twig_Extension_Debug')));

it works.. but that's a lot of copy and pasted code and seems quite unideal.

Saw you posted in #1825788: Symfony fatal when changing maintenance theme about this also, going to link to this comment there.

hefox’s picture

Status: Needs work » Needs review
StatusFileSize
new4.41 KB

Just to see that tests will past, adding patch with the copy and pasted items

Status: Needs review » Needs work

The last submitted patch, 1939104-8.patch, failed testing.

hefox’s picture

That's... odd; just manually went through site install and worked fine.

artofeclipse’s picture

@hefox, thanks for explaining this issue, your patch worked on my local machine, but there was some issues like the css/js not loaded.

hefox’s picture

Status: Needs work » Needs review
StatusFileSize
new5.84 KB

Last patch was missing the .html.twig *cross fingers*

Status: Needs review » Needs work

The last submitted patch, 1939104-12.patch, failed testing.

hefox’s picture

This is failing (at least for first test) because the tests are looking for <link rel="canonical" but now it looks like <link class="" rel="canonical"

From what I recall, Jen etc. want that class=X in there to make it easier for newer people to understand where to add new classes. Personally, I dislike an attribute appearing when empty like that.

So, either class="" needs to not appear or the tests need to be updated/changed or a third+ possibility I haven't thought of.

joelpittet’s picture

Assigned: artofeclipse » joelpittet
Status: Needs work » Needs review
StatusFileSize
new3.93 KB

This passes, locally. Here's hoping. I cheated a bit by letting the {{ attributes }} render the class as we are doing it now so there will be no empty class in the markup. Also, didn't include that registerTwig duplication above.

This whole thing may even just go away due to #1825090: Remove theme_html_tag() from the theme system

Also nice work @hefox and @artofeclipse for getting this started!

joelpittet’s picture

Oh also didn't include an interdiff because the patch in #12 didn't apply, I manually merged.

Status: Needs review » Needs work

The last submitted patch, twig-html-tag-1939104-15.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new1.02 KB
new3.98 KB

hmm, turns out was running tests on wrong site... this should be closer, but still may fail. And the whitespace modifiers were added strategically to pass the tests for ending newline.

Status: Needs review » Needs work

The last submitted patch, twig-html-tag-1939104-18.patch, failed testing.

joelpittet’s picture

Oh come on, give me a break, one fail "Fatal Error" on HtmlTagUnitTest.php Line 27

The whitespace modifiers I added purposefully leave the ending control's whitespace for a new line. I am going to re-run the tests but if it fails again, maybe someone else can have a look?

joelpittet’s picture

Status: Needs work » Needs review
Issue tags: -Twig

#18: twig-html-tag-1939104-18.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Twig

The last submitted patch, twig-html-tag-1939104-18.patch, failed testing.

star-szr’s picture

Status: Needs work » Needs review
StatusFileSize
new3.69 KB
new7.16 KB

After talking to @msonnabaum and @dawehner on IRC about this test fail, @msonnabaum noted that since the unit test is specifically for theme_html_tag(), it should be removed and replaced with a test for template_preprocess_html_tag() to try and test the same logic. We can't test the markup output in this way, but we can still test the logic in template_preprocess_html_tag(), that is used to set up variables for the Twig template.

Also tweaked formatting on a @todo.

star-szr’s picture

Issue tags: +Needs manual testing

Tagging.

star-szr’s picture

Assigned: joelpittet » Unassigned
Issue tags: -Needs manual testing
StatusFileSize
new3.4 KB
new7.63 KB

Tested manually, no changes in markup and the patch works as expected.

This updates the Twig template to use {% spaceless %} instead of a bucket of whitespace operators and tweaks some documentation.

In my opinion this is ready to go.

Status: Needs review » Needs work

The last submitted patch, 1939104-25.patch, failed testing.

star-szr’s picture

Not quite, maybe the whitespace modifiers need to stay after all.

star-szr’s picture

Assigned: Unassigned » star-szr

Going to take a look at the tests and see what we should do here.

star-szr’s picture

Assigned: star-szr » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.15 KB
new7.47 KB

I'm going to go with @joelpittet's strategic whitespace modifiers, we are hoping to get rid of this template during consolidation anyway.

star-szr’s picture

StatusFileSize
new1.56 KB
new7.56 KB

Got rid of a couple of the 'is defined' in the template file and went through a crazy debugging session about why the first one is needed, so I added a comment to the .html.twig file to explain :)

Also tweaked preprocess, don't need to check if #tag is set because #tag is mandatory.

star-szr’s picture

StatusFileSize
new1.48 KB
new7.57 KB

Comment tweak for consistency.

star-szr’s picture

Assigned: Unassigned » steveoliver

Assigning to @steveoliver for review.

star-szr’s picture

Issue summary: View changes

Add testing steps

thedavidmeister’s picture

stealing this for review.

thedavidmeister’s picture

Assigned: thedavidmeister » Unassigned
Status: Needs review » Needs work

At first I couldn't figure out why there were tests for this theme function - I mean, why don't we have tests for the preprocess function of every other theme function? what makes this special?

- // Test auto-closure meta tag generation

I think that's the clue.

I believe the point of testThemeHtmlTag() is not to check that the tag coming into the theme function matches the tag coming out but to check that if you provide a "value" you end up with rendered markup that is "regular" HTML tag like <footag>value</footag> and if you provide no value you end up with a "self closing" HTML tag like <footag attribute="bar" />.

The new testPreprocessHtmlTag() intended to replace testThemeHtmlTag() is a much weaker test, it simply checks a few implementation details of the preprocess that have no real bearing on the final rendered HTML tag being closed correctly.

I'm going to say this needs work until the modifications to the tests are "comparing apples to apples".

star-szr’s picture

#34 - That makes sense, if we want that it can't be a unit test though (as far as I know and based on my work on this issue).

star-szr’s picture

Assigned: Unassigned » star-szr

I can work on a functional test over the next few days.

star-szr’s picture

Assigned: star-szr » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.19 KB
new7.14 KB

Okay, here's an apples to apples test of the theme output :)

thedavidmeister’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me :) Thanks for working on that test, it looks a lot more useful to me now.

tim.plunkett’s picture

+++ b/core/includes/theme.incundefined
@@ -2434,41 +2434,37 @@ function theme_feed_icon($variables) {
+  $variables['attributes'] = isset($element['#attributes']) ? new Attribute($element['#attributes']) : new Attribute(array());
+  $variables['tag'] = $element['#tag'];
+  if (isset($element['#value'])) {
+    $variables['value'] = $element['#value'];

This looks like another preprocess that could benefit from a switch from 'render element' to 'variables'.

+++ b/core/modules/system/lib/Drupal/system/Tests/Theme/HtmlTagTest.phpundefined
@@ -0,0 +1,36 @@
+class HtmlTagTest extends WebTestBase {

+++ /dev/nullundefined
@@ -1,36 +0,0 @@
-class HtmlTagUnitTest extends UnitTestBase {

Why the switch? This is :(

star-szr’s picture

@tim.plunkett - I like the idea of getting rid of another preprocess function but I'm not sure if we can lose the preprocess entirely at this point. For now, I think we'd need something there to get rid of the default theme hook class - see #1938430: Don't add a default theme hook class in template_preprocess(). I'll do some tinkering and see if changing to 'variables' allows us to simplify the preprocess function at all.

As for the unit test to functional test switch: unless I'm mistaken, by definition we cannot unit test the output of a preprocess function + template.

star-szr’s picture

To be more specific, theme_html_tag() is a single function and is unit testable. theme('html_tag', …) or drupal_render(array('#theme' => 'html_tag', …)) invoke hooks and do many other things whether 'html_tag' is implemented as a theme function or as a template.

Edited to clarify wording.

star-szr’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new3.76 KB
new7.68 KB

Here's a version using 'variables' instead of 'render element'. If #1938430: Don't add a default theme hook class in template_preprocess() gets in we should be able to drop the preprocess function completely.

star-szr’s picture

Status: Needs review » Closed (won't fix)
star-szr’s picture

Issue summary: View changes

related