templates

Rendering Twig templates programmatically in Drupal 8

From time to time, I have the need to take a Twig template and a set of variables, render the template, replacing all the variables within, and then get the output as a string. For example, if I want to have a really simple email template in a custom module which has a variable for first_name, so I can customize the email before sending it via Drupal or PHP, I could do the following in Drupal 7:

Overriding a template file (.tpl.php) from a module

There are many times when a custom module provides functionality that requires a tweaked or radically altered template file, either for a node, a field, a view, or something else.

While it's often a better idea to use a preprocess or alter function to accomplish what you're doing, there are many times where you need to change the markup/structure of the HTML, and modifying a template directly is the only way to do it. In these cases, if you're writing a generic custom module that needs to be shared among different sites with different themes, you can't just throw the modified template into each theme, because you'd have to make sure each of the sites' themes has the same file, and updating it would be a tough proposition.

I like to keep module-based functionality inside modules themselves, so I put all templates that do specific things relating to that module into a 'templates' subdirectory.

Adding Images to Search Results (Drupal Search)

For a while (earlier in my Drupal career), I was avoiding adding imagefield-attached images to nodes that appeared in my search results, because I remember the first time I tried doing so, I was still quite confused by the way drupal handled search results theming.

Well, after another crack at it, I finally have a nice, performant way of displaying images from nodes added via imagefields (or in drupal 7, image fields) inline with search results, and it didn't require much work at all!

Images in Search Results

The image above shows inline images for LOLSaints.com, a site which uses Midwestern Mac's Hosted Apache Solr search service to return results quickly and allows faceting, etc. using the excellent Apache Solr Search Integration module. But the technique I'm using works equally well with built-in (but slower) Drupal core search module's results.

The Storm Before the Calm - Help Drupal Designers!

With less than two weeks remaining until Drupal 7's 'code freeze,' when new features and major patches will no longer be added to Drupal 7 (instead, everyone will work to optimize and clean up all the approved patches/features for an official Drupal 7 release), it's crunch time for those wishing to make Drupal a dream for designers and themers.

Initially, I had a lot of time to help along with a bunch of other great Drupal users (new and veteran alike) to help nail down a better set of core template files (.tpl.php files), along with some CSS definitions, in order to allow people to code new (and hopefully better) themes straight from Drupal core, using nothing but CSS and a .info file. (See the Stark theme (built into Drupal 7) for an example of how simple a Drupal theme can truly be!).

We've made it through: