This is a request for a way to do externalized template files that could override the default template '.inc' files from the module. This would eliminate the dependency on Mail Editor module for templating, which has its own set of problems with HTML emails.

This would also make it possible for people to template their emails without having to modify any Subscription module default documents, keeping their hands clean from 'hacking the module.' If they could be stored in the Themes directory that would be best.

Thanks for your improvements!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

salvis’s picture

The default templates are in subscriptions_mail.templates.inc. How about allowing a subscriptions_mail.custom_templates.inc file to override the former, if it's found?

I'm not sure about putting it into the themes directory. The file contains executable code that runs in the module context. Running code from the themes directory in the module context seems to be a violation of Drupal's execution model. It might be a security breach.

Have you reported the problems with Mail Editor? Maybe I've forgotten, but I'd like to look into those now in #651304: subscriptions_mail_digest template??. I'd appreciate any insight you can offer there.

gregarios’s picture

FileSize
2.98 KB

How about allowing a subscriptions_mail.custom_templates.inc file to override the former, if it's found?

That would be ok for me, if it would still freeze the mail template compatibility for the future (even if there were changes?).

I'm not sure about putting it into the themes directory. The file contains executable code that runs in the module context. Running code from the themes directory in the module context seems to be a violation of Drupal's execution model. It might be a security breach.

Take a look at the attached file, used in the Author Pane module. It is a template of sorts to override its own template file in sortof the same situation, with some PHP code, although it's template was named with a '.php' instead of an '.inc' suffix. The attached file resides in my theme directory, and works like a charm. The module even includes this file as an example in the module, with the list of allowed variables handily defined in comments. (it normally doesn't have a '.txt' at the end, but to attach it...)

Have you reported the problems with Mail Editor?

I never reported my problems with Mail Editor due to the fact I wouldn't have been able to report anything except "it doesn't work," and I know how useful that is. I installed it correctly, and tried to use it, but I could never get it to actually function. No errors were given, nothing else was broken, but no templates affected the emails that were sent out no matter what I tried, so I gave up and handled it by hand. Plus, it was getting much too complicated for me when I needed 3 or more modules just to get HTML emails going out — do I decided to eliminate one.

salvis’s picture

How about allowing a subscriptions_mail.custom_templates.inc file to override the former, if it's found?

That would be ok for me, if it would still freeze the mail template compatibility for the future (even if there were changes?).

That is difficult. We could implement a version number for the template file, so that you'd at least get an alert when the default template file has changed and you need to update your customized version.

Your attached file is a theming file. It runs in the theming context: it has access to nothing but the documented "pre-cooked" variables, and it runs only after all of the processing is done. It has much less potential to mess up something and thus can be entrusted to themers. Module files should not be entrusted to themers. (This separation may change though.)

Thanks for the info about Mail Editor.

gregarios’s picture

That is difficult. We could implement a version number for the template file, so that you'd at least get an alert when the default template file has changed and you need to update your customized version.

That works for me.

It also works for me to have the theme file located in a sub-directory of the Subscriptions module directory. As long as there is some kind of alert notification of template changes, that would be just fine. :-)

gregarios’s picture

Since theme designers also have access to the modules directory anyway, I think I've come up with a fix for this on this thread.

gregarios’s picture

Priority: Normal » Major
Status: Active » Needs review
FileSize
3.32 KB

Attached is a patch allowing a site admin to copy the subscriptions_mail.templates.inc file to the themes directory to override the default found in the modules directory.

It is rolled on the current 6.x-1.x-dev version dated 2012-Feb-22.

salvis’s picture

Status: Needs review » Needs work
+++ subscriptions.module	2012-03-11 12:58:36.000000000 -0700
@@ -649,3 +649,16 @@
+ * Substitute a possible external template file instead of the default.

This is YOUR intention with the "then" branch (= your change). We want to describe the entire function, i.e. write something like "Load the default templates." (default as in not-customized-in-MailEdit)

Add something like "The Subscriptions default templates can be overridden with site-specific default templates by placing a file named '...' into the '...' directory."

+++ subscriptions.module	2012-03-11 12:58:36.000000000 -0700
@@ -649,3 +649,16 @@
+function subscriptions_template_chooser() {

Move this to the subscriptions_mail.module and call it for what it does, e.g. subscriptions_mail_load_default_templates(). Maybe you can even move it into a subscriptions_mail*.inc file — that would be even better...

+++ subscriptions.module	2012-03-11 12:58:36.000000000 -0700
@@ -649,3 +649,16 @@
+   global $theme_path;

Drupal convention is to indent two spaces per level, not three.

+++ subscriptions.module	2012-03-11 12:58:36.000000000 -0700
@@ -649,3 +649,16 @@
+   if (is_file($theme_path .'/subscriptions_mail.templates.inc')) {
+      include_once $theme_path .'/subscriptions_mail.templates.inc';
+   }
+   else {
+      include_once drupal_get_path('module', 'subscriptions_mail') .'/subscriptions_mail.templates.inc';
+   }

Try to avoid duplication. Something like

+  $file = 'subscriptions_mail.templates.inc';
+  if (is_file($path = $theme_path .'/'. $file)) {
+    include_once $path;
+  }
+  else {
+    include_once drupal_get_path('module', 'subscriptions_mail') .'/'. $file;
+  }

 

+++ subscriptions_content.module	2012-03-11 13:01:18.000000000 -0700
@@ -606,7 +606,7 @@
 function _subscriptions_content_node_mailvars(&$mailvars, $node, $field, $s) {
   global $language;
-  include_once drupal_get_path('module', 'subscriptions_mail') .'/subscriptions_mail.templates.inc';
+  subscriptions_template_chooser();

Is it really necessary to load the templates here in subscriptions_content.module? Could we avoid it? Maybe by pre-loading them where we call this function from.

salvis’s picture

Priority: Major » Normal

D7 needs to go first here...

gregarios’s picture

D7 needs to go first here...

No! Please! You're breaking my arse on this enough, and I don't even have or understand D7 yet. I do have a day job here. Ripping me for all this semantic stuff seems a little ridiculous to me... I've never even had any formal training. So all my work on D6 on this will be totally moot until there is a D7 version of my work? :-(

gregarios’s picture

Priority: Normal » Major

Salvis, regarding comment #7:
How come you seem to write more in a comment about my patch than it would take to just write the actual code you want used and do it? Wouldn't that save us both a bunch of time?

salvis’s picture

Priority: Major » Normal

Yes, that's how it goes all over d.o. We cannot have fixes/additions in old versions (such as D6) that disappear when you upgrade (to D7 in this case). Upgrading is hard enough — we must not put ourselves in a position where we willingly cause old issues to come back to life for upgraders. I'm sure you'd hate that if you were in that position.

Ripping me for all this semantic stuff seems a little ridiculous to me... I've never even had any formal training.

I'm not ripping you. I'm trying patiently to show you how to do it right. This does not replace formal training, but since you're learning on the job, you may as well learn to do it the right way.

So all my work on D6 on this will be totally moot until there is a D7 version of my work?

No. It's a good starting point, and when D7 is done you'll already have the D6 version, so that step will be quick. I don't think D7 is that much different for this issue, and it shouldn't matter much which way you port.

gregarios’s picture

No. It's a good starting point, and when D7 is done you'll already have the D6 version, so that step will be quick.

From what I'm reading in other issues you're having with the D7 version, Mail Edit will be a required dependency now. So, will this effect my code at all? My code for D6 is meant to specifically remove the need for Mail Edit. (and it does, for me) How does this factor in the D7 version? Would the user still need Mail Edit, but leave it disabled somehow?

Is it really necessary to load the templates here in subscriptions_content.module? Could we avoid it? Maybe by pre-loading them where we call this function from.

I have no idea... I didn't write this code, I'm just substituting my function where it was previously coded. Couldn't change it if I wanted, as I don't even understand why it is called in these various places.

salvis’s picture

#10 and #11 crossed. In reply to your #10:

I'm not here to do your work for you, but I'll sacrifice some of my precious spare time to help you do it, and I hope that you'll get better at it over time. I'm making an investment in you.

with the D7 version, Mail Edit will be a required dependency now. So, will this effect my code at all?

No, I don't think so. You will need to enable Mail Editor because it's where the template processing is now done, but you just won't define any Mail Edit custom templates if you don't want to.

gregarios’s picture

I'm not here to do your work for you, but I'll sacrifice some of my precious spare time to help you do it, and I hope that you'll get better at it over time. I'm making an investment in you.

That's funny... I was going to say the exact same thing to you.

The only reason I make any contributions to Drupal modules at all is so I can get better functionality out of them than they have normally. I'm primarily and foremost a web designer and web host, with a bunch of IT and prepress design thrown in, as well as a fulltime+20 work-from-home dad. ;-)

I have this module working fully for my own purposes, but I'm just trying to manage to get the module to a point where I don't have to apply all my updates to it every time a new release comes out.

salvis’s picture

Yes, funny indeed.

I'm sure you've heard the expression "scratch your own itch". I have no itch anymore to change anything in Subscriptions for D6.

I have this module working fully for my own purposes, but I'm just trying to manage to get the module to a point where I don't have to apply all my updates to it every time a new release comes out.

That's your itch, so you have to scratch it. If you can convince me that a change is a net improvement (benefit - resource_usage - additional_UI_complexity - additional_support - additional_maintenance) then I'll commit it, but you'll have to provide it pretty much ready-to-be-committed.

If you provide a patch that I can't commit, that creates work for me. My itch is not to get your patch committed but to get you to provide better patches. So that's where I'm scratching. :-)

For every line of code that I commit, I'm already taking on the chore to port it to D8, D9, etc. because it's not practical to put that on you, but I refuse to do the D7 porting. That's part of your cost for getting something committed to the legacy D6 version.

gregarios’s picture

Understood. Thanks.

gregarios’s picture

Status: Needs work » Needs review
FileSize
3.37 KB

Ok, attached is a new patch. It is rolled against the new 6.x-1.x-dev (2012-Mar-11) version.

I think I got your recommendations in there. However, I couldn't figure out the right method for including the function as an external ".inc" file, so I just moved it to the subscriptions_mail.module as you requested.

I tested it on my live site, and it's showing no errors and functioning correctly.

If you approve this, I'll download the D7 dev version (which I won't be able to test) and make the exact same changes to it, rolling a new patch for the D7 for ya. I can't work on the D7 first for obvious reasons, but I'll work you up a D7 patch when this is good to go.

Let me know if it is acceptable. Thanks Salvis.

salvis’s picture

Sorry for the delay, I've been sick in bed. This looks good but I need some more time to test it.

gregarios’s picture

No problem. It's that time of year here, too.

salvis’s picture

Where exactly is "the 'themes' directory"?

sites/all/themes does not seem to work...

Assuming that I run a site on Garland and with Bluemarine as my administration theme, then where do I put the custom default template file?

gregarios’s picture

Oops... the readme should say "in your custom theme directory"...

so, in sites/all/themes/<your-theme>/

salvis’s picture

Well, and if I have no custom theme?

Or if I have one but I'm using a built-in administration theme?

This may not be as easy as we thought it would be...

gregarios’s picture

I'm not sure I understand... everyone has a theme. Drupal comes preloaded with themes, even.

Hold on... I'm testing something...

gregarios’s picture

Ok attached is a new patch (based on 6.x-1.x-dev 2012-Mar-16) that I think addresses the issue of which theme directory to use. I researched and found this method after seeing other module issues with $theme_path variable. It looks for the "active" theme directory. I tested it on my site and it's working nicely. I also clarified the README text better in this patch.
Let me know what you think.

salvis’s picture

Exactly, Garland for example. But Garland is under /themes — we cannot put a file there, because that's part of Drupal.

gregarios’s picture

You can put it in Garland and it works just fine. Garland is just another theme. It just happens to be one Drupal comes with. Many, many other modules have template files that have to be installed in the sites/all/themes/<theme>/ folder. I deleted Garland about 3 years ago and never have had it installed since.

gregarios’s picture

Also, Garland is downloadable outside of Drupal, and is actually getting removed from Drupal 8.
See this link:
http://drupal.org/project/garland

salvis’s picture

You're missing several points. Let's try to nail down the first one.

Garland normally lives under themes, not under sites/all/themes. That is a fundamental difference.

It may well work if you put the file in themes/garland, but you're not supposed to put things there, and we are good citizens and we do not promote nasty habits.

The same applies to all themes that come packaged with Drupal (and thus live under themes).

Does that make sense?

gregarios’s picture

That makes sense to a degree, but I don't see it working in the real world.

Lets just state that it can only be used with custom-added themes then. I mean, it's only looking for the file "if it exists" where it looks. If it isn't there nothing will be broken. All we can do is qualify the usage.

Also, there are a lot of modules that allow customization by putting external files in the owner's custom theme folder. I just can't see it being a problem. Themes are made to be customized, and I'm sure if someone wanted to customize their default Garland theme they wouldn't be just starting with subscriptions module.

In fact, I can see people complaining: "How come when I add the custom template to my default Garland theme folder it doesn't work!?"

We have two choices: Make it work with the default Drupal themes (if someone tries it), or don't. You choose.
I think we should leave it working no matter which theme folder the template is in, but tell people they should only use the template in their "custom theme" folder...

salvis’s picture

That makes sense to a degree, but I don't see it working in the real world.

Lets just state that it can only be used with custom-added themes then.

No way! That'd be a support nightmare. The environment that I can support without killing myself is plain-vanilla Drupal. The "real world" is way too diverse to make any promises. If there's a problem in a "real-world" installation and it's working fine in plain-vanilla Drupal, then I'm mostly out of the loop, because I don't need to debug the "real-world" installation. Now, to advertize a feature that "does not work on plain-vanilla Drupal, but only in the real world, but only if you have a custom theme" would be nuts!

In fact, I can see people complaining: "How come when I add the custom template to my default Garland theme folder it doesn't work!?"

Yes, I see that, too.

We have two choices: Make it work with the default Drupal themes (if someone tries it), or don't. You choose.

It needs to work with the default Drupal themes, but I wish there was a solution that did not involve putting the files under the themes directory. That directory branch is off-limits.

I think we should leave it working no matter which theme folder the template is in, but tell people they should only use the template in their "custom theme" folder...

I don't want to actively block themes, of course.

Being vague about where to put the file is not a solution. It will only cause people to come back and ask for support, as I did. The "custom theme" folder is the folder where custom themes go: sites/all/themes.

We have to be more specific, like

Put it into sites/all/themes/&lt;my_theme>; on an out-of-the-box Drupal installation put it into themes/garland for testing purposes, but be aware that putting files under Drupal directories other than sites is not recommended practice.

... if that works... Is that your understanding of how it should work? Does it?

There I go again letting myself be dragged into discussing partial solutions before we've established the full extent of the problem... That is a very inefficient way of working. Can you afford to work that way?

gregarios’s picture

It needs to work with the default Drupal themes, but I wish there was a solution that did not involve putting the files under the themes directory. That directory branch is off-limits.

The custom theme directory is the only accepted place to put files like this, unless you want to sign on to putting it in a new sites/all/libraries/subscriptions/ directory. (some other modules are placing 3rd party scripts and such in this directory)

As far as what to tell people, we can put something like this, but we're getting too complicated than just leaving it the way I put it in the patch. This might actually get you more support tickets than just letting it be ambiguous:


Mail Templates (Hard Coded Theme File)
--------------------------------------

The Subscriptions default templates can be overridden with site-specific custom
templates by placing a copy of the file named 'subscriptions_mail.templates.inc'
into your active custom theme directory, and customizing it to suit your needs.
This directory can be found here: sites/all/themes/<your-theme-name>/

To use the custom subscriptions template in a default Drupal theme from the main
Drupal <document root>/themes directory, please copy the theme to your sites/all/themes
directory first, then activate the copied theme. We prefer you do not install the file
into your default Drupal theme directly.

Being vague about where to put the file is not a solution.

I don't think my README text is vague at all -- it specifically stated where to place the file to make it work. What it didn't do is give a solution for the few people who don't have any custom theme installed.

There I go again letting myself be dragged into discussing partial solutions before we've established the full extent of the problem... That is a very inefficient way of working. Can you afford to work that way?

There you go again -- no need to get condescending. I'm not liking that you rip me for the way I work. This "problem" you are seeing is not MY problem until you make it my problem -- it's your problem to begin with, then you make it my problem, see? That aside, I'm still willing to follow your suggestions, if you give them to me. I would just use my latest patch as-is, but if you have a problem with it, then we can work on it. I do not have a problem with it.

Nobody can "established the full extent of the problem" without a discussion of some kind. Do you always work to an incomplete finish without consulting anyone about potential problems? (See how that sounds?)

Anyway, please... if you have a suggestion that will work, or if one of mine will work, let me know and I'll code it in and submit a patch. I can't keep reading your mind to find out what you want this text to say. If you don't like mine, then supply your own, please.

salvis’s picture

There you go again -- no need to get condescending. I'm not liking that you rip me for the way I work.

That wasn't my intention. I'm sorry if it came through like that.

In #28 I tried to start laying out the problem. I did say there was more to it, but when in response to my question "Does that make sense" you came back with "That makes sense to a degree" and started proposing solutions, I made the mistake of letting myself be dragged into discussing solutions.

I'm not asking you to read my mind but to allow me to fully explain the problem before you cover me with solution proposals.

I did try to let you have it all in #20 and #22, but this didn't work. Then I tried a fresh start in #28, but it only made sense "to a degree". There's no use in talking about solutions as long as we don't have a full common understanding of what the problem is.

You're eager to work on this all day and night, but I only have a few minutes on some days.

You want to get it in quickly, but I'll be stuck with it for years — I'm afraid you'll have to slow down to my pace.

Is there anything in #28 that does not make perfect sense?

No solutions, please!

gregarios’s picture

#28 makes perfect sense.

salvis’s picture

Great!

There's a feature called admin theme. Do you know that feature?

gregarios’s picture

Yes.

salvis’s picture

Admin theme is a core feature and we need to support that.

This means we can have two different themes active at the same time.

Do you have an idea whether the user theme or the admin theme is active during cron, "active" as in $theme_path?
Since you're sending HTML mail, I assume you're getting the correct $theme_path in cron, right? But does it still work if you enable an admin theme that's different from the user theme?

Does it make a difference how cron is run (cron.php, from the status page link)?
(In D7 there's a built-in poormanscron which may behave in yet another way.)

In Mail Editor (where we'd like to have the custom default templates available, too) it'll surely be the admin theme that's active, because Mail Editor is part of the admin interface.

Does this make sense and do you agree that we need to support the admin theme feature (including the potential issues above and maybe others)? Am I missing something?

No solutions, please!

gregarios’s picture

I believe the "active" theme (according to my coding) is always the theme picked as "default" in the admin/build/themes panel. So, whatever you have going for an administrative theme would be irrelevant to the module. Thus, no matter how you run the module, cron or otherwise, the theme is picked by whatever is listed as the default theme (of which there can be only one) in the settings.

I could be wrong about the $GLOBALS['theme_key'] having just the default theme set in it, though. I couldn't find any definition for it other than "It holds the active theme" in it.

I know nothing about the Mail Editor module other than it never works for me.

Also, my latest patch (#24) does not use $theme_path.

salvis’s picture

Yes, $GLOBALS['theme_key'], sorry.

drupal_get_path('theme', $GLOBALS['theme_key']) is a nifty way to get around having to know whether the theme is custom or built-in, but if we were to recommend copying the built-in theme to sites/all/themes, can we be sure that Drupal will then use the copy? (NOT looking for solutions yet, just noting down potential problems that we must not forget.) Can you post pointers to documentation where this is defined/explained?

I could be wrong about the $GLOBALS['theme_key'] having just the default theme set in it, though. I couldn't find any definition for it other than "It holds the active theme" in it.

A quick check shows that you are indeed wrong. On my development installation I have Devel module enabled and its Execute PHP block in the footer of every page. Running
dpm($GLOBALS['theme_key']);
on user pages gives me "garland", and running it on the admin/build page gives me "bluemarine". This corresponds to "the active theme" for the current page, and can certainly be different from the "default theme".

Please find out what the situation is in cron.

I know nothing about the Mail Editor module other than it never works for me.

Ouch! That means we live in a different universe. If you had said it worked fine for you, it wouldn't necessarily mean that we're closer together, but saying it never worked for you ascertains a non-negligible minimal distance. My target audience are those for whom ME works (the ME+ types). I have no way of knowing how large the ME- group is, but I sure hope we're working on a feature that will work for the ME+ group as well!

This realization reduces the significance of your testing. The ME+ and ME- environments are obviously different, and what works in one doesn't necesserily work in the other and vice versa! Why does ME not work for you? If we could understand what the problem is, we could gain some insight (and maybe even fix it).

One thing is clear: $GLOBALS['theme_key'] returns the name of the admin theme on the admin/build/mail-edit/subscriptions_content_node-nid/en page, and not the name of the user theme, so when Mail Editor requests the default templates (through Subscriptions), it will not get the custom ones, based on $GLOBALS['theme_key']. Do you agree that this is a problem?

More food for thought: you probably saw it coming by now — what do we do if the admin enables more than one theme? Then every user will have a different $GLOBALS['theme_key'] value... We could try to say that we just don't support that, but having a partial feature is dissatisfying. Kind of like being "98% pregnant". There should be no no-go zones between core and Subscriptions. Do you agree with that?

sites/all/libraries/subscriptions could be a valid alternative, but is there a supported way of finding that directory? A way that will also work if the site does not reside in sites/all but in sites/mysite or in sites/mysite/special?

Let's continue investigating the problem space before jumping to solutions. Does all this make sense to you? Do you see other points that we need to keep in mind?

Take your time with your reply, I probably won't be able to reply in the next two days.

gregarios’s picture

I'll save ya the trouble: My solution was meant to be an alternative instead of having to use (the unusable) Mail Edit module, so I guess we're done. I didn't even know that Mail Edit used your template file. It'll be simpler to just customize the Subscription module for my exact usage and call this quits. I'm sorry, but this entire 2-year spanning thread was just to attempt to get a solution I could use without having to customize your module for my exact needs, and possibly to help someone else in my situation. Thanks for helping me understand what needed to be done.

salvis’s picture

Can we not try to look into why you're hating Mail Editor so much?

gregarios’s picture

Nothing personal about it. I don't hate it. It's a perfectly fine virtual paperweight. (-; It just doesn't always work with my setup -- sometimes it would send out emails without the MailEdit templates, and that was completely unfortunate in a corporate email. I'd rather keep my job than trust the Mail Edit module I guess. I know I can trust a hard-coded file though. :-)

salvis’s picture

Status: Needs review » Needs work

ME takes the default templates as starting point for fine-grained customizations, e.g. per content type. This would go well with custom default templates...

Oh, well, at least we've had a lively discusssion...

gregarios’s picture

ME takes the default templates as starting point for fine-grained customizations, e.g. per content type. This would go well with custom default templates...

I agree. Except, it doesn't work with my setup for some reason. I tried to use it. I wanted to use it. And, I tried to troubleshoot it, but I could never get any PHP errors or log notices or anything -- just a lack of template getting through to the emails.

salvis’s picture

That's in the context of efforts to send HTML mail, right, not normal text mail?

gregarios’s picture

I honestly don't remember much else about it... it was over 2 years ago. Done a lot since then. ;-)

salvis’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)