Articles

Automatically Copying The Node Title To The Menu Title In Drupal 6

One new feature of Drupal 7 is that any title you give a node will be copied to the menu title field when you create a menu item. I wanted to replicate this functionality in Drupal 6 and so I created a function that did just that. I have used this function a few times in different projects so I'm posting it here.

This code uses the drupal_add_js() function to push a small amount of JavaScript code into the page when a node is added or edited. The hook hook_init() is used to add this content as it is run very early on in the Drupal boot cycle. I have tried putting this code into other hooks (like hook_preprocess_page()) but it isn't included, I would look into this further but this solution seems to work well.

PHP Question: Include And Require Files

Question

What's the difference between include() and require()?

PHP Question: Increment By One

Question

What will the following snippet print, and why?

$count = 0;
print $count++;

PHP Question: Printing A Boolean Value

Question

What will the following code print, and why?

echo TRUE;

PHP Question: Scope Resolution

Question

What is printed after the following code has been run?

function calc() {
    $a = 1000;
    return $a;
}

$a = 1;
calc();
print $a;

 

PHP Questions

As part of a new series of posts I will be creating a set of PHP questions suitable for use in interviews or just for fun. They will range from the very easy to very difficult but are all designed to test one or two specific parts of the PHP language. The idea is not to test for knowledge of the order of PHP function parameters (which I often have to look up), but more to test if the candidate knows how the language works. It is also quite a good mechanism to post about some of the fundamentals of the PHP language.

Revisiting filter_var() and FILTER_VALIDATE_URL

Quite a while ago I looked at using the filter_var() function to validate URL's using the FILTER_VALIDATE_URL flag and someone pointed out recently that this function has not only changed since the initial release, but that a number of flags can be added to change the way that this function works. Here are the flags available.

Overriding Drupal 6 Automatic Select Element Validation

Whilst creating a large and complex form in Drupal 6 recently I hit upon a problem that took me a couple of hours to solve so I am posting the solution here in case anyone else gets similarly stuck. I am also writing this down so that I can remember the strategy in case I have to do the same thing again.

What I was doing was creating an event booking form. The form allowed users to select from a number of times slots in a select list, but each time had a limited number of bookings. When the user submitted the form and all allocated slots for their selected time were fully booked they were shown a message asking them to select a different time slot. The original time was removed from the select list so that the user didn't try to select it again. This was fine and it all worked as expected but when this happened I also got the following error message from Drupal.

Drupal 6 Views Templates

Rewriting the output of a Drupal view isn't always necessary, but figuring out what to do with the available templates can very useful. It is, however, also a bit of a stumbling block from time to time, especially for newcommers. After you have created your view you will have a number of filters to restrict your data, and a set of fields which are printed by Views using a set of templates. How these templates are used depends on what sort of view data you use and style you select, but there will be three standard templates that are always used in views. These templates are display, style and row and can be treated like any other Drupal template in that you can override them with more specific filenames.

Ideas Of March

This week I was pointed, somewhat ironically through Twitter, to a blog post from Chris Shiflett about how we need a blogging revival. Which is something I quite agree with as although Twitter is great for a sense of community, it is impossible to impart good technical knowledge and/or experience through 120 characters.

Blogs are a fantastic resource. I have lost count of the times that I have been stuck on a problem and after a quick bit of searching I have found a blog post from someone who had exactly the same problem, but managed to find a solution to it. I have quite a few people who have done the same thing with #! code and have posted comments on how I have helped them out, which I find really rewarding.