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


Dear High Visibility,

I think tha your trying to treat the symptoms not the cause. I instantly agree, that Drupals Node Editing Form is far less usable compared to other CMS. But that should lead us to better Form Design and not to Workarounds.

Sure, there are things that could be done to improve the node editing UI, and in fact there is a large effort underway to make that happen for Drupal 7 , but in this specific case I'm talking about support for features that will probably never be a target of Drupal core functionality. (e.g., PHP syntax checking, debugging tools, built-in Drupal api libary - all of which Komodo, the IDE, I use has :-)

Hi Caleb,

[I don't have access to my dev environment to show actual code but..]
In my template.php file, I have a little piece of code that lets me use
node-nid.tpl.php files where they exist.
I think this is called suggested templates.

Basically, if the file node-9.tpl.php file exists, for example, it will be used instead of node.tpl.php, for the path node/9.

This is a slightly better method than yours in that
1. You can use it for ANY node, without having to put in the code into the node body.
2. You don't need the php input filter at all.

Hope this helps!

...but only if you don't care about end-users being able to edit things in the UI. That's the one thing the method in the article has the advantage of (e.g., create the node with the tools of your choice in a file, but in the end make it editable via the UI). I guess you could always take the stuff out of your tpl.file and put it in the node, but at that point it's kind of a potatoe/potato thing.

Please come back and share your code though - I'm sure there are people who would be interested to be able to use it.

Update: if this is the method you are talking about, and I think it probably is, it's better suited for different purposes than what I am describing in the article.

Yep, that's the piece of code there!
As you've said, horses for courses and all that.

Hi Caleb,

It looks like we might be doing a little work together on a project so I was checking out your site. My solution to this particular problem was creating a function in a custom module, and then calling that function to grab the file from the files/pages directory and output it. That way, it's only one line of code in the node body field, like:


The downside to this (and the advantage to your solution) is that you have to have a custom module available for the function call. I've been feeling like this is kind of a hack-ish solution for a while now, but it's serving it's purpose. Ideally, it would be nice to have a little toggle below the text area that will import the content of the file to the text area, and another to export it to a file, so non-technical users can just pull the content in when they need to use it, and we (as admins) can output it back when we want to use an editor.

I look forward to talking with you!


Glad you stop by - I've been using the utility module method you refer to for a lot of things myself. For this article however I was thinking that this was a good method for people who don't feel comfortable enough to make their own module or know what a function call is. ;-)

Actually, now that I think about it the other upside for using the include statement is that you won't have to wrap your html in php and/or escape characters - it will instead work the same way a tpl.php file works. Also if the code will eventually just end up back in the node so that the end user can edit - it might be overkill to create the function. Either way both are good things to know about.


I have searched for this extensively - there are many good reasons to want to more directly incorporate and manage php files within the Drupal framework. Many thanks for taking the time to share this pragmatic solution.

Agreed, same here. Many thanks too from me!

I use "It's all text!", a firefox plugin that lets you specify the editor you would like to edit textareas with.

Then, every text area has a little 'edit' button in the corner, and clicking it opens the contents of the text area in your favourite editor (mine is vi). Whenever you save in your editor, the textarea is updated.

This also allows you to open the editor, and leave it open when you submit the form. The next time you open it, the text in your editor is still active.


Thanks for the pointer to "It's all text!", now I can edit nodes with emacs, getting rid of just about the only thing I've found annoying about drupal (which I have just started using).