Sunday, February 22, 2009

Google Maps in Drupal

Below are some brief instructions on how to use it:

Step 1. Create a map with macro creator

Each site includes a macro to create a map that you can customize it as you see fit. You can see the GMap Macro on my site here. To create a map:

* Scroll to the bottom of the macro to define the Map Height and Map Width. This is in pixels (try "500px" by "500px" to start with) and make sure you include the "px" after the number. Scroll back to the top and you will see the template map has automatically resized.
* Move the map around with the mouse to the area you want to include. Also adjust the zoom control to achieve the level of magnification you want. You can choose between a basic map, a Satellite map or a hybrid version by switching between the buttons.
* You can add points, lines or circles by (single) clicking on the map. You can switch between these options with the pull down menu where it says Click Map. Experiment by clicking around a map to see what happens.
* Some of the line/polygon settings are a little temperamental, but the marker options work well allowing you to change the marker type. It is possible to link one marker to another.
* To remove a marker make sure the Click Marker option is set to "remove" and then click the marker to be removed on the map.
* Each map needs a different name or identifier (set to "map" by default). Type this in to the Map id attribute field. This can only contain numbers letters and the "-" symbol, and must be unique for each map that will appear on the same page.
* The Control feature (None|Large|Small) allows you to specify whether there is a zoom control on the map and what size it is.
* There are all sorts of advanced options available for customizing your map. The most important of these is the ability to have feeds that take information from a local RSS feed with geolocation data and dynamically plot this on the map. I'll blog about this separately when I have figured out how to do it!

Step 2. Put the map on a webpage (node) in your site

Scroll down to the bottom of the macro and copy the Macro Text that appears in the text box. You then need to paste this into the body field of a new page (node). When you do this make sure to disable rich text (just underneath the text box) and under Input Format select the GMap Filter option. You can add any other information to the page with HTML or the rich text editor.Just do not change the GMap Macro text. When you are done, click Submit and the new page with your Google Map and any plotted points, lines or polygons will appear.

Friday, February 6, 2009

AHAH Helper Module For Drupal

The ahah helper module provides a neat function ‘ahah_helper_path’ to let drupal know about new field elements. This way you can set the path “magically” and the helper module takes care of how the new fields will be rendered. You can use it like this:


$form[’slide_form’][‘new_field’] = array
// field properties …
The ahah property path will be:
‘#ahah’ = array (
‘path’ => ahah_helper_path(array(’slide_form’, ‘new_field’)),
// other properties
);


This helper module makes it very easy to update a form with your own ajax-driven field addtions. It helps memorizing the form so your new fields are known by drupal. At the form api reference link, we can see which fields can trigger this behavior: button, checkbox, image button, password, radio, select, submit, textarea, textfield.

An extra advantage of using the ahah helper module is that when javascript is disabled, the same function will be called with a normal post and this will work just fine. Sometimes it can be helpfull to check #first_time parameters in callbacks to see if the data was previously saved.

Sunday, February 1, 2009

Theming Drupal 6 from the module

You start with creating a DOT info file for your module. The name of which would be in our case special_page.info.

;$Id$
name = Special Page
description = Provides a template file for my content type CONTENT_TYPE.
core = 6.x

Then you go about creating your DOT module file, ie: special_page.module.

// $Id$
/**
* @file
* Module that provides a special template for pages that show
* ONE node of content type CONTENT_TYPE_NAME.
*/

You probably should implement HOOK_help() in your module, but I will skip that, and cut the chase to what we really need.

At this point, you need to tell the theme system to use your template, and to use it only in certain situations. Your 'situations' will differ from mine. In my case, I wanted the theme system to use my template on a node page, hence any page with path node/nid, yet only on pages that show the node in view 'mode', so I did not want the template to be used when the path is node/nid/edit for example. There are many ways to skin the cat here. I decided to use the fact that a $node object is passed to the page.tpl.php template only when the page is a node page, as you will see in the following code snippet. Here, I will make use of a preprocess function to pass on to the theme system a suggestion about a new module-supplied template.

function special_page_preprocess_page(&$variables) {
// If this is a node page (not a list of nodes page) and
// the node is shown in 'view' mode rather than 'edit' or whatever.
if (isset($variables['node']) && (arg(2) === NULL)) {
// If the content type of that one node is 'CONTENT_TYPE_NAME'.
if ($variables['node']->type == 'CONTENT_TYPE_NAME') {
$variables['template_file'] = 'page-CONTENT_TYPE_NAME';
}
}
}

Then feel free to create such template file. As a reminder, no need to add the tpl.php extension to the value you assign to $variables['template_file']. Also, that name can be anything, I am just following conventions here by prefixing with page, as in page-SOMETHING.tpl.php. Make sure that the name you provide here matches the name of your template file.

Then, you are faced with a small problem: the template file will need to be placed in the theme folder in order to be picked up by Drupal's theme system. But you don't want that. So what to do? Here comes a situation where we can use the module hook HOOK_theme_registry_alter(). For the theme hook you want to provide special theming for (hook in theme parlance here), you will have to tell Drupal Hey, Drupal, please look in my module folder over here, you may find a template file you will need.

function special_page_theme_registry_alter(&$theme_registry) {
$theme_hook = 'page'; // my hook name
// Get the path to this module
$modulepath = drupal_get_path('module', 'special_page');
// Add the module path on top in the array of paths
array_unshift($theme_registry[$theme_hook]['theme paths'], $modulepath);
// dsm($theme_registry[$theme_hook]['theme paths']);
}

And you are done.