Issue #1898448 by julien, drupalway, podarok, jenlampton, steveoliver, joelpittet, jwilson3, chrisjlee, Cottser | c4rl: Convert search module to Twig.

Task

Use Twig instead of PHPTemplate

Twig sandbox: http://drupal.org/sandbox/pixelmord/1750250

Remaining

* replace all tpl.php files with .html.twig equivalents
* replace all theme functions with .html.twig equivalent templates
* add new preprocess functions for the .html.twig equivalent templates
* update all hook_theme definitions

To test this code...

1) create some content
2) build your search index by running cron
3) search for some content to see that your search results are running through both search-results.html.twig and search-result.html.twig

#1757550: [Meta] Convert core theme functions to Twig templates

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

c4rl’s picture

Issue tags: +Twig

Tagging

bharat83’s picture

Assigned: Unassigned » bharat83
bharat83’s picture

Assigned: bharat83 » Unassigned
piers.warmers’s picture

Assigned: Unassigned » piers.warmers

Looking into: DrupalCon Sydney Sprint.

chrisjlee’s picture

Assigned: piers.warmers » chrisjlee
Status: Active » Needs review
FileSize
9.22 KB

patch attached

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

The last submitted patch, 1898448-search-twig-4.patch, failed testing.

chrisjlee’s picture

Status: Needs work » Needs review

#5: 1898448-search-twig-4.patch queued for re-testing.

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

The last submitted patch, 1898448-search-twig-4.patch, failed testing.

star-szr’s picture

FileSize
1.73 KB
10.12 KB

Thanks for this @chrisjlee!

Converting the theme() calls to render arrays (per http://drupal.org/node/1920746#render) should fix the exceptions, the 'pager' variable in preprocess would be NULL if no pager was to be rendered.

The remaining failures are legitimate and they are because the help text is missing (see search-results.html.twig). I added that as a temporary @todo but that will need to be fixed in this issue.

The only other changes I made were removing one extra newline, and removing spaces around the 't' filter.

star-szr’s picture

Status: Needs work » Needs review
star-szr’s picture

FileSize
1.42 KB
10.29 KB

Discussed briefly with @steveoliver, let's put the help text in the preprocess for now and revisit later. This should pass testing.

chrisjlee’s picture

No problem! Awesome it passed.

Thanks for your help. This helps me figure out the patches.

jenlampton’s picture

Status: Needs review » Needs work

Thanks guys, this is a great start! A few comments:

1) In the .html.twig template comment docs we need to remove any mention of variable type, since front-end devs won't need to know what an array or string is anymore. It might be worth reading our coding standards to get up to speed on all these little nuances :)

 * - title_prefix (array): An array containing additional output populated by
 *   modules, intended to be displayed in front of the main title tag that
 *   appears in the template.
 * - title_suffix (array): An array containing additional output populated by
 *   modules, intended to be displayed after the main title tag that appears in
 *   the template.

should become

 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.

and

 * - title_attributes: Array of html attributes for the title. It is
 *   flattened into a string within the variable title_attributes.
 * - content_attributes: Array of html attributes for the content. It is
 *   flattened into a string within the variable content_attributes.

should become

 * - title_attributes: HTML attributes for the title.
 * - content_attributes: HTML attributes for the content.

2) We decided to remove all instances of "is defined" from the checks, and will also only define variables as needed. That means that

  {% if (info_split.comment is defined) %}

should become

  {% if (info_split.comment) %}

We changed our mind about that a few times, so it's not uncommon to find lots of instances of "is defined" in the sandbox, but we need to remove them in the core patches.

3) All instances of render() and similar need to be removed from templates. This means that

  {{ render_var(title_prefix) }}

should become

{{ title_prefix }}

If we can make these small changes, I think this patch looks great. I'm going to create a follow-up issue about combining these two templates into one though, since we are splitting the OL and the LI tags by doing it this way, and that goes against our new principles for the Drupal 8 theme layer.

edit: Follow-up issue created at #1926344: Consolidate search-result.html.twig and search-results.html.twig

joelpittet’s picture

Status: Needs work » Needs review
FileSize
10.03 KB
2.68 KB

Re-rolled

jenlampton’s picture

Status: Needs review » Needs work

This is much better :)

I think we still need to get rid of the docs that mention that info_split is a keyed array.

 * - info_split: Contains same data as info, split into a keyed array.


Long term, we should aim to get rid of info_split entirely. We can nest each one of the sub-items within info directly to make the info variable drillable, so printing {{ info }} will work exactly as it does now, but printing {{ info.type ]] should print only the type, as we do currently by printing {{ info_split.type }}. But I think that is too much for this first patch :)

For now, how about we just move the mention of info_split lower in the docs right before we explain what it contains? Something more like...

 * - info_split: Contains the same data as info, but split into separate parts.
 *   - info_split.type: Node type (or item type supplied by module).
 *   - info_split.user: Author of the node (can be linked to users profile
 *     depending on permission).
 *   - info_split.date: Last update of the node. Short formatted.
 *   - info_split.comment: Number of comments output as "% comments", %
 *     being the count (depends on comment.module).
 * @todo: the info variable needs to be made drillable and each of these sub
 *   items should instead be within info and renamed info.foo, info.bar, etc.

The @todo in here will remind us to clean this up later as we get to #7, Cleanup of previous preprocess functions.

Also, I missed this in the last review, but the docs still say that these templates contain definition lists which is not the case anymore, so we should correct that as well.

in search-results.html.twig:

 * This template collects each invocation of theme_search_result(). This and
 * the child template are dependent to one another sharing the markup for
 * definition lists.

should become

 * This template collects each invocation of theme_search_result(). This and
 * the child template are dependent to one another sharing the markup for
 * ordered lists.


And in search-result.html.twig:

 * This template renders a single search result and is collected into
 * search-results.twig. This and the parent template are
 * dependent to one another sharing the markup for definition lists.

should become

 * This template renders a single search result and is collected into
 * search-results.twig. This and the parent template are
 * dependent to one another sharing the markup for ordered lists.
chrisjlee’s picture

FileSize
4.89 KB
12.76 KB

Jennifer:

Thanks for the helpful comments and detailed changes.

star-szr’s picture

Status: Needs work » Needs review
FileSize
10.16 KB

Thanks @chrisjlee, the latest patch included changes to UpdateCoreUnitTest.php, quickly rerolled (edited) to remove those hunks.

Minor nitpick (not changed in this patch): the @todo for the info variable is not formatted correctly, see http://drupal.org/node/1354#todo. No colon, and the first word after the @todo should be capitalized.

chrisjlee’s picture

FileSize
744 bytes

Cottser:

Good catch! made those changes

chrisjlee’s picture

FileSize
10.16 KB

Whoops sorry forgot to hit attach.

jenlampton’s picture

Status: Needs review » Needs work

More teeny tiny nits :)

In search-result.html.twig
We changed the names of our templates from .twig to .html.twig, so we need to update the docs to reflect that.

 * This template renders a single search result and is collected into
 * search-results.twig. This and the parent template are
...

In search-results.html.twig
We print {{ pager }} in the template, but it's not listed in the Available variables. We should add it.

in search.pages.inc we need to clean up the preprocess function docs, now that we have standards :)

/**
 * Prepares variables for search results templates.
 *
 * Default template: search-results.html.twig.
 *
 * @param (array) $variables
 *   An array with the following elements:
 *   - results: Search results array.
 *   - module: Module the search results came from (module implementing
 *     hook_search_info()).
 */

and it also looks like the list of elements within $variables was incorrect for template_preprocess_search_result :( let's see if this is a sufficient improvement.

/**
 * Prepares variables for individual search result templates.
 *
 * Default template: search-result.html.twig
 *
 * @param (array) $variables
 *   An array with the following elements:
 *   - result: Individual search result.
 *   - module: Module the search results came from (module implementing
 *     hook_search_info()).
 *   - title_prefix: Additional output populated by modules, intended to be
 *     displayed in front of the main title tag that appears in the template.
 *   - title_suffix: Additional output populated by modules, intended to be
 *     displayed after the main title tag that appears in the template.
 *   - title_attributes: HTML attributes for the title.
 *   - content_attributes: HTML attributes for the content.
 */

So close guys! :)

joelpittet’s picture

Status: Needs work » Needs review
FileSize
3.32 KB
11.85 KB

re-rolled.

star-szr’s picture

Thanks @joelpittet!

The standards aren't quite official yet, but this won't change: the array data type in the @param should be formatted like @param array $variables. No surrounding parentheses for the array per http://drupal.org/node/1354#param.

chrisjlee’s picture

FileSize
894 bytes
11.84 KB

like this?

star-szr’s picture

Yup, thanks @chrisjlee!

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

The last submitted patch, 1898448-23.patch, failed testing.

star-szr’s picture

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

#23: 1898448-23.patch queued for re-testing.

jenlampton’s picture

FileSize
11.84 KB

Thanks Cottser, that one got me twice!
Re-uploading patch from #23 since test bot seems to not want to re-test patches? (it's not actually in the queue)

star-szr’s picture

@jenlampton - #23 had some random test failures but it did come back green after I re-queued this morning. No biggie :)

jenlampton’s picture

Status: Needs review » Reviewed & tested by the community

Tests passing, and... looks fabulous! :) :)

star-szr’s picture

This is looking good! Found some more minor docs tweaks, minor enough that this can probably be left as RTBC.

+++ b/core/modules/search/templates/search-result.html.twigundefined
@@ -0,0 +1,77 @@
+ * - title_prefix: Additional output populated by modules, intended to be
+ *   displayed in front of the main title tag that
+ *   appears in the template.

This comment should be re-wrapped at 80 characters, the second line ends too early.

+++ b/core/modules/search/templates/search-result.html.twigundefined
@@ -0,0 +1,77 @@
+ * @see template_process()

Should this be removed?

+++ b/core/modules/search/templates/search-results.html.twigundefined
@@ -0,0 +1,34 @@
+ * - search_results: All results as it is rendered through search-result.tpl.php
+ * - module: The machine-readable name of the module (tab) being searched, such
+ *   as 'node' or 'user'

These comments needs to end in periods, and in one case (search_results) be wrapped at 80 characters as a result.

chrisjlee’s picture

FileSize
1.52 KB
11.84 KB

@Cottser Like that? did those changes except this one:

+++ b/core/modules/search/templates/search-result.html.twigundefined
@@ -0,0 +1,77 @@
+ * @see template_process()

Because i don't really know the answer to that question.

chrisjlee’s picture

FileSize
11.85 KB
611 bytes

Sorry realized i just made a small typo.

try this patch instead.

star-szr’s picture

Thanks @chrisjlee! Everything looks good, except one thing…

+++ b/core/modules/search/templates/search-results.html.twigundefined
@@ -11,7 +11,8 @@
  * - module: The machine-readable name of the module (tab) being searched, such
  *   as 'node' or 'user'

This comment still needs a period at the end, after 'user'.

jenlampton’s picture

Status: Reviewed & tested by the community » Needs work

Oh man, so close I can taste it.

Hmm... Let's leave this for now:

 * @see template_process()

I have been removing lines like these since we will *eventually* be removing the whole process layer, but perhaps it's premature to remove them in these initial patches, since this function still exists. We can roll the removal of all these @see lines into the patch that removes that function. That should be fun :) Dibbs!

@chrisjlee do you want to do the honors of adding the final period as per #33?

chrisjlee’s picture

Yes I would love to. Will do that tomorrow!

chrisjlee’s picture

FileSize
11.85 KB
652 bytes

Added a period. Hooray! I've never been so excited about adding a period!

chrisjlee’s picture

Status: Needs work » Needs review

Changed status

jenlampton’s picture

Status: Needs review » Reviewed & tested by the community

Aw yeah :)

jenlampton’s picture

Issue summary: View changes

added to test section

star-szr’s picture

Added a commit message to the issue summary based on git blame and sandbox issues. Please edit if I missed anyone :)

xjm’s picture

Issue tags: -Twig

#36: 1898448-36.patch queued for re-testing.

alexpott’s picture

Issue tags: +Twig

#36: 1898448-36.patch queued for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

chrisjlee’s picture

Assigned: chrisjlee » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

c4rl’s picture

Title: Convert search module to Twig » search.module - Convert PHPTemplate templates to Twig

Per #1757550-44: [Meta] Convert core theme functions to Twig templates, retitling to indicate this issue applies to templates rather than theme_ functions (though there are no theme_ functions in this module).

c4rl’s picture

Even though this is committed, do we care about profiling it further?

c4rl’s picture

Issue summary: View changes

Add commit message