Hi,

I'm trying to programmatically create a Geofield and associate it with a custom content type during a modules installation.

The content type is 'storelocation' and is created using the following code (parts of the code have been removed for brevity):

$fields = array(
        'storelocation_address' => array(
            'field_name' => 'storelocation_address',
            'type' => 'addressfield',
        ),
        'storelocation_geofield' => array(
            'field_name' => 'storelocation_geofield',
            'type' => 'geofield',
        ),
    );

    foreach ($fields as $field) {
        field_create_field($field);
    }

...

$instances = array(
    'storelocation_address' => array(
        'field_name' => 'storelocation_address',
        'label' => _t('Address'),
        'description' => _t("A store's physical address."),
        'widget' => array(
            'type' => 'addressfield',
            'settings' => array(
                'default_country' => 'CA',
                'available_countries' => array('CA'=> 'Canada'),
            ),
        ),
        'required' => TRUE,
    ),
    'storelocation_geofield' => array(
        'field_name' => 'storelocation_geofield',
        'label' => _t('Store Coordinates'),
        'description' => _t("A store's latitude and longitude."),
        'widget' => array(
            'type' => 'geocoder',
            'active' => 1,
            'settings' => array(
                'geocoder_field' => 'storelocation_address',
                'geocoder_handler ' => 'google',
                'delta_handling' => 'default',
                'handler_settings' => array(
                    'google '=> array(
                        'geometry_type' => 'point',
	                    'all_results' => 0,
	                    'reject_results' => array(
                            'APPROXIMATE' => 0,
                            'GEOMETRIC_CENTER' => 0,
                            'RANGE_INTERPOLATED' => 0,
                            'ROOFTOP' => 0,
                        ),
                    ),
                ),
            ),
        ),
        'required' => 0,
        'display' => array(
            'default' => array (
                'type' => 'hidden',
            ),
        ),
    ),
);

foreach ($instances as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = 'storelocation';
    field_create_instance($instance);
}

The above code will successfully create the Geofield and associate it with the 'storelocation_address' field during the installation of the module, however when content is added using the custom content type and saved the geo data is not populated.

Inspecting the database and the $field and $instance variables for the custom Geofield and comparing it to a Geofield that was created through the user interface shows that there are some discrepancies in the settings of the widget and field. I tried to get around this by manually populating the settings during the installation of the custom module but even when the settings matched the geo data still wasn't populated.

One way that I found to fix this was to open up the content type through Structure -> Content types -> Store Location (manage fields) -> Geofield (edit) and clicking the 'Save settings'. This appears to trigger something in the backend (I thought it was through field_update_field() but I tried calling that in the field creation stage to no avail). Now when I go to create new content (or save an existing content node of type 'Store Location') the geo data is populated.

The only place that I can really pinpoint it down to is that when _field_invoke() is called in field.attach.inc for geofield_field_presave() and it tries to load the items (around line 208 in field.attach.inc) by calling $entity->{$field_name}[$langcode]) nothing is found for the geofield when it is created as above.

Am I missing something completely obvious or does Geofield not support being created programatically?

Any help would be greatly appreciated.