PHP Logo

Getting Started With PHP DocBlox

20th March 2012 - 6 minutes read time

DocBlox is the new defacto PHP class documentation generator. It was developed as an alternative to PHPDocumentor, but it looks as though it will replace it and become the new PHPDocumentor2. As a result I thought I would put together a quick tutorial on getting started.

This post assumes that you know what PHPDoc comments are and that you have a project that you want to generate API documentation for. To get started on PHPDoc take a look at the Wikipedia page on PHPDoc.

Whereas PHPDocumentor would scan and generate all of the code in one go, DocBlox will scan the source code and generate a series of XML files based on the code it finds. It will then use these files to generate the API documentation in a number of different formats, although HTML is probably the most common.

JavaScript Logo

Local And Global Variables In JavaScript

18th March 2012 - 4 minutes read time

I have seen a lot of this sort of thing, so though I would put together a quick lesson in JavaScript variable scope. There are some important differences between local and global variables in JavaScript that will cause grey hairs if you don't know what's going on.

Scope is something that dictates what variables can be seen by code. If a variable is created inside a function then it can be said to be local as only the locally running code can see it. A variable that can be accessed by any part of the code is said to be global. This is special in JavaScript, which I will come onto later.

This scope concept is important as if a variable is outside the current scope then the code can't see it. Everything in JavaScript is a variable so understanding how and when variables can be accessed is important.

To create a local variable in JavaScript do this:

Drupal Logo

Adding The autocomplete Attribute To Forms And Password Fields In Drupal 6

22nd February 2012 - 3 minutes read time

Many modern browsers now come with auto-complete functionality so that users can fill in their details quickly without having to type in their username and password every time they want to log on. This can be turned off by adding the attribute "autocomplete" to the form and password elements and setting its value to 'off'.

Setting the autocomplete attribute to off on password fields (and forms containing password fields) can add an added level of security to your Drupal site. This is especially important as if the computer is stolen it is more likely to contain saved passwords that will allow access to sensitive systems. So turning this feature off might be beneficial for certain systems, especially those with very sensitive information.

Drupal Logo

How To Change No Search Results Text In Drupal 6

2nd February 2012 - 2 minutes read time

Every Drupal project I finish will usually have the same request at some point. This usually happens when the client tries to do a search that produces no results and sees the search hinting text about blue smurfs.

The text about blue smurfs that is printed when there are no search results is called directly in the search module so it isn't possible to edit or override it. The solution is to use the theme_box() theme hook and override the text just before it is sent to the page. Just drop the following code into your theme.php file, rename the function to fit your theme and clear your caches.

Apache HTTP Logo

IE8 Downloading docx Files As zip On Apache

23rd January 2012 - 2 minutes read time

I recently built a Drupal site on an Apache server setup and everything seemed fine until someone running IE8 tried to download a .docx file. For some reason IE8 insisted that this file was a .zip file and would open it as such, causing a bit of confusion as to what the problem was. I assumed that this was due to the mime type of the file not being set properly, but I decided to do a quick search to see if I could find anything to corroborate this. I thought it might be something odd that IE8 was doing as everything else was downloading the file perfectly.

After wading through multiple support forums and finding very little of any help I eventually gave up and went with my first hunch and set the mime type for the .docx file extension in the sites .htaccess file. Here is the rule I used:

Apache HTTP Logo

Redirecting From One Domain To Another Using Mod Rewrite

6th January 2012 - 1 minute read time

Use the following rules in your Apache configuration (or your .htaccess file) to redirect all traffic from one domain to another. This also keeps the query string in place so that the user isn't just dumped to the homepage.

  1. RewriteEngine on
  2. RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
  3. RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [L,R=301]

These rules are mod_rewrite dependent, so you'll need that module enabled in your Apache install.

PHP Logo

Testing 301 Redirects Using PHP

3rd January 2012 - 2 minutes read time

I'm always writing bits of script to test things out and I thought that I would start making a record of them in case I need them in the future. This is a little script that will loop through the contents of a text file and validate that a bunch of 301 redirects point to the place they are meant to point to. This script assumes that the redirects are already in place on the server, but this is what I was testing. Here the format of the redirects text file.

Drupal Logo

Adding Reset Password Support To Drupal 6 Password Recovery Email

3rd January 2012 - 3 minutes read time

Drupal is capable of sending out a few different emails to users depending on different actions. The emails can be customised quite nicely with usernames, passwords, email addresses and other things by using a set of tokens. The password recovery email states that you can use the "!password" token to send the user their new password, but after a few tests I found that this token doesn't get replaced when the email is sent out.

The simplest solution here is just to remove this token from the description for this email. However, if you do want to allow user's to reset (and receive) their passwords by using this form then there are a couple of simple things you can do.

The first thing to be done is alter the user_pass form so that it uses a custom submit function that we will write. Add the following form hook to a module (or create your own for this purpose).

Drupal Logo

Adding WISIWYG Support To Drupal 7 Node Summaries

31st December 2011 - 6 minutes read time

I often get asked a simple request during a project, and the solution to the problem is sometimes more complex than I originally thought. One of these problems was adding a WYSIWYG editor to the summary field on the node edit form. There isn't an easy way to do this, but it is possible to get a good solution working.

Using the hook_form_alter() hook we can intercept and change the node edit form to change the type of the summary element from a textarea to a text_format element. In order to get the WYSIWYG component of the form working we will need to also add a format to the form element. For the purposes of this example I have created a module called wysiwyg_summary, so the hook is called wysiwyg_summary_form_alter().

Drupal Logo

Drupal 7 Expanded Menu Control On Nodes

1st December 2011 - 5 minutes read time

I recently noticed a strange little issue with Drupal 7 that seemed like either an oversight or a decision I don't agree with. Essentially, when a node is created with a menu item in place the extended flag on the menu will not be set, but the control is also not available on the menu admin page. This means that when you are trying to print out a hierarchical menu structure you need to create the page, go into the menu admin area, access the menu, click on edit to access the menu item and change the setting there.

To get around this I set about creating a little module that would add a form control to the menu options section on the node edit form. This single checkbox is used to override any settings that the menu module creates with regards to the extended menu parameter.

The first thing to do is create a simple info file for the module. I include this here for completeness.