Three little lines of code that you may learn to love

Assignment: Create a highly customized page/node in Drupal and:

  • Be able to develop the page/node using our text editor of choice and avoid the time-consuming and not-intended-to-be-developer friendly Drupal node-editing interface
  • Optionally be able to decide whether to have the page/node be editable by the via the Drupal UI or not.

Sure, we could do this by creating a page-node-xx.tpl.php template file (where 'xx' is the node id number), but that's just a major hassle later on when updating/theming things and it won't give us the flexibility of being able to let an end-user edit things.

In this case, the thing to do is to create a file called my-file.php (.php because we want our text-editor to still recognize it correctly), put it in our theme folder, check the PHP input filter on the node itself, and then place this code in a node:

// Include a file with the rendering of a node so that you can edit in text editor and just refresh without having to use Drupal UI
$filename = 'my-file.php';
$path_to_file = drupal_get_path('theme', 'your_themes_name') .'/'. $filename;

What this will do is look for the file you placed inside your theme folder called "my-file.php" and render it. Which means that you make changes directly to my-file.php without having to touch the node itself and thus simply refresh your node anytime you make changes to the file.

This method has the advantage of:

  • Saving a ton of time and headaches of dealing with the Drupal interface and doing without your favorite code editing tool.
  • After you're finished you'll have the option of choosing to replace the include-code with the final code inside your file so that an end-user can edit it within UI...
  • ...or you can just keep things like this and be able to do cool things like check the code into version control, which means that you basically get many of the benefits of a tpl.php file without the cost of one.

Enjoy and happy coding. :-)

UPDATE: if this all sounds like a good idea to you, head over to the project page for Save-to-File which was inspired by a conversation in the comment thread below.

22 March, 2008

Advanced Theme Construction Kit (ATCK) updated for Drupal 6

The Advanced Theme Construction Kit, which integrates a derivative of the Yahoo grids CSS library within a Drupal framework, was updated and released for Drupal 6 today.

Something new
After religiously following the changes to Drupal 6's theme-layer for many months now, as well as actively coding a Drupal 6 module for the past month and a half, I was thinking that upgrading ATCK was going to be a snap...

In some ways it was, but the Drupal 5 to Drupal 6 theme conversion was by far the most complicated theme update I've seen since being involved with Drupal (4.5 onward). As it turns out the Drupal 5 template.php file for ATCK was almost completely useless, and it was with great thanks that I (again) borrowed heavily from Hunchbaque's template.php in order to get the desired granularity for classes/id's.

One area I did depart from core and the Hunchbaque theme on is the handling of css files. Allowing for the possibility that there is something that I'm 'missing', I can't say that I'm currently very happy with Drupal's new method of adding stylesheets via .info files, nor with the functions that Hunchbaque currently uses in it's template.php to add IE-only css. The core method of stylesheet handling makes stylesheets a bit of a pain to refresh, and I found both methods to be unreliable with respect to conditionally adding stylesheets.

In the end 'old fashioned' conditional statements were implemented within page.tpl.php, and a custom function added to template.php. Along with being reliable, this combo provides the ability to tell Drupal to NOT import specific core css files (e.g., if you don't want the node.css file loaded - simply add a line to the function inside ATCK's template.php and node.css never gets loaded).

Something old
The most satisfying part of the upgrade was that the centerpiece of ATCK, the Yahoo Grids-inspired CSS library, did not require a single change. So this part has proven to indeed be modular and Drupal version independent. :-)

Future development plans for ATCK
* Possibly add the edit functionality that the Zen theme has introduced to make managing blocks and views easier.


HigherVisibility is looking for assistance!

15 March, 2008
Subscribe to RSS - theming