After listening to Seth Cohn’s very inspirational speech about the Context module at the DrupalCamp NYC 8, I got inspired to play around with that module a bit more. Primarily, Context serves as alternative way for the block administration. However, Seth focused more on Context’s ability to keep variables during a page load available, without the need to use global variables or other workarounds.
One of my recent projects required very specialized SEO optimiziation. Initially, I started using the Nodewords module, which is brilliant and very easy to use. In certain areas however, I felt that Nodewords makes it harder for me as a developer to achieve my goals, so I wanted to come up with a different approach. By no means is this module supposed to be a replacement for Nodewords, it’s more of an exploration of the Context module.
Search Engine Optimization for drupal module developers
In this example, I’m primarily focusing on the head title (the title that appears in the browser for any open window) and the meta description (which will show up as a teaser in search results).
I’ve identified three different “types” or areas that often require specialized SEO optimization. These are:
- Nodes (per content type)
- Pages (created by other custom Drupal modules)
- Views Pages (created by the Views module)
SEO Head: A centralized module that handles head title and meta description
If you’re developing in a similar way as I am, you like to create smaller modules for each distinct functionality. In my approach for this alternative module, I am using a central module called SEO Head, which works as the central module to set the head title and meta description. This module is called at end of the page execution process by using a relatively high system weight of ‘200’ (the default being 0). By calling this module at the end, I am trying to make sure that no other module sets the head title. This module overrides the existing head title and sets the meta description.
Looking at the above list, only one of the areas is defined within the module: Nodes, per content type. By the time the SEO Head module gets called, the full node object is available, so anything (including CCK fields) can be used for the page title or meta description. See lines 17 to 35 in the source code.
In this example implementation of the SEO Head module, I am using a special text CCK field for the page title of a node of the type ‘page’ (see the file attachments below, you can install this field easily by downloading the node_page_title module, which is a Features module.). For the regular “story” node type, the body of the story is used for the meta description. These two instances are just examples of what could be done here.
Using Context: Define head title and meta description in your custom module
Instead of defining all page titles and meta descriptions in one central place, I thought it would be much ‘nicer’ to have each custom module you create for your site define their page titles and meta descriptions, if needed. In order to make this happen, I am using the Context module as a vehicle to transport any head titles and meta descriptions to the SEO Head module. This allows any module to set these values ‘remotely’, meaning directly in the module. To demonstrate this, I created a separate example module called ‘example’, see download list below. The Example module provides a page and a simple view.
For any page defined in a module, just use the context_set function (line 13 and 14 in the example module) to set head title and/or meta description, and the SEO Head title will take care of the rest.
To define head title and meta description for Views (which is currently not possible with Nodewords), you can make use of the hook_views_pre_execute hook (line 23 following).
Again, these modules are just some examples for using Context in a creative way, and aren’t meant to replace the Nodewords example in any way. I’m looking forward to some interesting discussions.