Issue #1898060 by steveoliver, taslett, jenlampton | c4rl: Convert dblog 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) Go to the db log page (admin/reports/dblog) to see links, rendered via dblog-message.html.twig
2) Click on any of the links on the on the dblog page to see the full message, rendered via dblog-message.html.twig

#1757550: [Meta] Convert core theme functions to Twig templates
#1956806: Fix DbLogController::overview() and topLogMessages() logic and provide a formatMessage() method

Comments

c4rl’s picture

Issue tags: +Twig

Tagging

taslett’s picture

Assigned: Unassigned » taslett
taslett’s picture

Status: Active » Needs review
StatusFileSize
new2.41 KB
fabianx’s picture

+++ b/core/modules/dblog/dblog.admin.incundefined
@@ -298,11 +298,11 @@ function theme_dblog_message($variables) {
+      $link_text = truncate_utf8(filter_xss($output, array()), 56, TRUE, TRUE);
+      $output = l($link_text, 'admin/reports/event/' . $event->wid, array('html' => TRUE));
     }
 

This one is tricky to see, but here you can use:

url() instead and use

<a href="{{ link_url }}">{{ link_text }}</a>

in the template.

Then at least the template would be useful.

Probably it could be deprecated and be a derivative of theme_link later, so lets add a comment with @todo as well for that.

Thanks for your work!

taslett’s picture

Hi Fabianx,
The dblog-message.html.twig template is used when the full message is displayed, after clicking the link in the log. So we will need a conditional statement either in the preprocess function or the template. I think that will make the @todo irrelevant.

This is what I am looking at currently.

{% if link_text and link_url %}
  <a href="{{ link_url }}">{{ link_text }}</a>
{% else %}
  {{ message }}
{% endif %} 
taslett’s picture

StatusFileSize
new2.66 KB

Using url() in the preprocess function and the conditional in the html.twig file.

star-szr’s picture

Thanks @taslett! Minor nitpicks ahead :)

+++ b/core/modules/dblog/dblog.admin.incundefined
@@ -274,16 +274,16 @@ function dblog_filters() {
- * @param array $variables
+ * @param $variables

This line shouldn't be changed, the 'array' should be kept.

Also, dblog-message.html.twig needs a newline at the end of the file.

taslett’s picture

StatusFileSize
new2.51 KB

Thanks Cottser,
Not sure why I removed the array.
Didn't realize the html.twig file needed a newline at the end of the file should we add that to the twig coding standards page #1823416 ?

star-szr’s picture

Thanks @taslett! I should have mentioned this but interdiffs are helpful too :)

As for the newline, that's covered by http://drupal.org/coding-standards#indenting, no need for a Twig-specific standard there.

taslett’s picture

StatusFileSize
new1.14 KB

Adding interdiff for comment 8 above.

jenlampton’s picture

This one looks great! Thank you both for working on it :)

This patch includes a few very minor changes.
1) Simpler variable names: text and url instead of link_text and link_url.
2) Also included these variables under "Available variables" in the template docblock.

chx’s picture

Status: Needs review » Reviewed & tested by the community

I think this is the first Twig template conversion patch I happen to RTBC and it's hard to describe the joy i feel over seeing

{% if url and text %}
  <a href="{{ url }}">{{ text }}</a>

things like this.

joelpittet’s picture

Looks great, nice work taslett

fabianx’s picture

+1 for RTBC

fabianx’s picture

Issue summary: View changes

add how to test this code

star-szr’s picture

StatusFileSize
new680 bytes
new2.51 KB

Minor comment nitpick in line with the variable changes in #11, and added commit message per git blame, couldn't find any issues in the sandbox.

One thing to note before committing is that we haven't yet reached consensus on the standards for preprocess documentation: #1913208: [policy] Standardize template preprocess function documentation

jenlampton’s picture

We're going to need to update the rest of core to match the new coding standards for preprocess, so I don't think that should block a commit here.

Otherwise, looks great! :)

star-szr’s picture

#16/@jenlampton - Agreed, patch looks good!

dries’s picture

- Wouldn't be cleaner if we always had 'message' instead of having 'message' and 'text'.

- When do we have log messages that don't provide a URL? I don't think that actually happens.

webchick’s picture

Yeah, I looked at Drupal.org's admin/reports/dblog (D6), as well as webchick.net's (D7).

While I didn't go through every single page, I went through quite a few of them, and never found an instance of a dblog message without a URL linking to the more specific info. So it seems like we could remove support for that feature without making anyone cry.

However, if we did that, then the Twig template just becomes:

+  <a href="{{ url }}">{{ text }}</a>

Which I would think is exactly what theme_link() (or its ilk) would be doing.

So my question would be, do we actually need this template file at all, if we just made the wlid part of the watchdog API?

webchick’s picture

Status: Reviewed & tested by the community » Needs review
jenlampton’s picture

On admin/reports/dblog the link is always printed.
When you click that link, you are taken to admin/reports/event/N where the {{ message }} is printed instead.
If we delete message then we'll never see the longer version.

I will give this a re-roll and try to eliminate the theme function entirely.

jenlampton’s picture

Title: Convert dblog module to Twig » Remove all (useless) theme functions from dblog module, so we don't have to convert to Twig
StatusFileSize
new4.87 KB

Okay, here's a more sane approach with no theme functions at all. Let's see if there were tests that now fail :)

Status: Needs review » Needs work

The last submitted patch, core-remove_theme_funcs_from_dblog-1898060-22.patch, failed testing.

jenlampton’s picture

Status: Needs work » Needs review
StatusFileSize
new784 bytes
new4.9 KB

This one prevents the double escaping, should pass tests.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

Tested this manually, and it works as before, both for messages with variables and without.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Yeah!

While this represents an API change, I'm thinking it's going to be way too annoying to track each of these individually, so we should just talk about it in the "meta" change notice about converting things to Twig.

webchick’s picture

Issue tags: +SprintWeekend2013

Status: Fixed » Closed (fixed)

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

sun’s picture

Status: Closed (fixed) » Needs work
+++ b/core/modules/dblog/dblog.admin.inc
@@ -58,13 +58,29 @@ function dblog_overview() {
   foreach ($result as $dblog) {
...
+    if (isset($dblog->message) && isset($dblog->variables)) {
...
+        $message,

If a query result row does not have a ->message and ->variables for any reason, then $message contains the $message of the last row.

+      // Messages without variables or user specified text.
+      if ($dblog->variables === 'N;') {
+        $message = $dblog->message;
+      }
+      // Message to translate with injected variables.
+      else {
+        $message = t($dblog->message, unserialize($dblog->variables));
+      }

Could we have added a dblog_format_message() helper function instead of duplicating the same code in multiple places?

star-szr’s picture

Assigned: taslett » Unassigned
Status: Needs work » Closed (fixed)
star-szr’s picture

Issue summary: View changes

Add commit message.

sun’s picture

Status: Closed (fixed) » Fixed

I re-opened this issue in #29, because I already predicted that no one would care if it would be moved to a new issue. That's what happened now.

@jenlampton, @taslett, @Cottser: Any chance you can follow up with necessary fixes on #1956806: Fix DbLogController::overview() and topLogMessages() logic and provide a formatMessage() method?

star-szr’s picture

@sun - I don't think it's that we don't care, it's that we've been a bit busy working on the 80+ other Twig conversion issues. I'd be happy to work on the follow-up and I've just assigned it to myself.

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

Anonymous’s picture

Issue summary: View changes

Add follow-up issue to summary