Online Book Reader

Home Category

Mapping With Drupal - Alan Palazzolo [22]

By Root 304 0
the bottom of the page to create this field.

On the screen that comes up there are no further field settings, so click the “Save field settings” button.

The final configuration page for this field is where the field is configured for the Event content type at admin/structure/types/manage/event/fields/field_event_location. Set it to be a required field by checking the “Required field” checkbox.

Under the “Available countries” field, to enable all countries leave it at the default with nothing selected, or else select a few countries.

The “Format handlers” checkboxes allow different fields to be shown in addition to the default country-specific address form. For example, if you are using the address for shipping a product to customers, you might want to enable the Name and Organization fields. In this example just leave it as the default “Address form.”

It is possible to set a default value for the address or to allow multiple addresses (for example, for different invoicing and shipping addresses), but in this example just leave all the values unchanged.

Click the “Save settings” button.

We have now created a content type for an event with a title, a description and an address.

How does this differ from the address field provided by the Location module in Adding geographic data to a node with the Location module? If you are building a website that will be used outside the United States, the differences are significant.

Create some “Event” content at node/add/event. Enter the name and description of an event. Try changing the country in the drop-down and see how the address form changes. For the United States, there is a drop-down field labeled “State” and a field labeled “ZIP Code.” For Canada, the state field becomes “Province” and the ZIP Code becomes “Postal Code.” The United Kingdom has the same “Postal Code” field but the “State” field is a text field rather than a drop-down. The United States and Canada are the only countries with a list of states or provinces included with the Addressfield module.

For comparison, create some “Drupal company” content at node/add/drupal-company. With the United States as the country, start typing the name of a state and notice how it autocompletes. Most countries have states or provinces that will autocomplete like this. However notice how the labels of the fields do not change; it is always “State/Province” and “Postal Code.”

This comparison shows an important difference between the two approaches (Location and GMap versus Geofield and Addressfield). Location and GMap give you more functionality just by installing the modules. OpenLayers, Geofield, and Addressfield give you less ready-made functionality, but they try to be more semantically correct with the data, and they provide a pluggable API to add the data that you need. For example, if there is a country for which you need a list of states, you can look at the addressfield_example module that is included inside the Addressfield module to see how to do this (the example shows how to add states for Switzerland).

The Event content type that we have created has an address, but this is not yet being geocoded. The Geocoder module will integrate with the Geofield and Address modules to handle this.

Download the latest release of the Geocoder module to your site’s modules directory. At the time of writing there is no stable release, so download the latest development release.

Enable the Geocoder API module. There are no configuration or permissions changes needed for this module.

On the “Manage fields” page for the Event content type at admin/structure/types/manage/event/fields go to the “Add new field” row. Enter Event geo as the Label and event_geo as the Field name. In the “Select a field type” drop-down, choose “Geofield.” In the Widget drop-down, choose “Geocode from another field.”

Click the “Save button” at the bottom of the page to create this field.

On the next screen that comes up, there are no further field settings, so click the “Save field settings” button.

This field

Return Main Page Previous Page Next Page

®Online Book Reader