I am using the OpenLayers widget to add/edit geofield content. When the content is displayed the Geofield Formatter zooms to the extent of the drawn feature as expected since the Zoom to Layer behaviour is enabled with the geofield_formatter layer selected.

However, it would be helpful if the edit form also zoomed to the extent of the drawn feature(s). Am I missing something or is this functionality not currently possible?

Comments

tky’s picture

I found the same issue here. Though I enabled the "Zoom to layer" function for my input map, it won't do the job it supposed to do when get geodata from addressfield.

It would be better that the map can zoom in with data input while user click "find using XXX field" function in edit form. Would improve the accuracy if user can navigate the location and check it.

FrancoNogarin’s picture

+1 on this

Sinovchi’s picture

It would be really helpful if the edit form also zoomed to the drawn feature.

flaw’s picture

+1

maijs’s picture

In case you want "Zoom to Layer" behaviour to work in geofield widget (on the edit node page), you can use a hook which injects "openlayers_behavior_geofield" layer to possible "Zoom to layer" behavior options:

/**
 * Implements hook_form_alter().
 *
 * Add geofield feature layer to "Zoom to layer" behaviour
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  // 'ctools_export_ui_edit_item_form' = openlayers map edit form (via ctools)
  if ($form_id == 'ctools_export_ui_edit_item_form') {
    // Check for geofield module presence
    if (module_exists('geofield')) {
      // Check if geofield behaviour is enabled (this form element will most likely be only in openlayers map edit form)
      if (drupal_array_get_nested_value($form, array('behaviors', 'openlayers_behavior_geofield', 'enabled', '#default_value'))) {
        // Check form has options that need to be altered
        if ($options = &drupal_array_get_nested_value($form, array('behaviors', 'openlayers_behavior_zoomtolayer', 'options_set', 'options', 'zoomtolayer', '#options'))) {
          // Append 'openlayers_behavior_geofield' layer to existing layers
          $options = array_merge($options, array('openlayers_behavior_geofield' => 'openlayers_behavior_geofield'));
        }
      }
    }
  }
}

After you add this hook, do the following:
1. Go to Structure > OpenLayers > Maps > (your widget map) > Edit > Behaviors > Geofield and make sure Geofield checkbox is checked.
2. Save the form by clicking "Save and edit"
3. When map edit form reappears, go to Behaviors > Zoom to layer, enable "Zoom to layer" option and choose "openlayers_behavior_geofield" layer in layers list.
4. Now when you edit an entity which has a geofield field attached (with OpenLayers map widget), "Zoom to Layer" will zoom to the features layer.

fonant’s picture

Perfect! #5 works just how it should, thanks maijs.

vistree’s picture

Hi, is #5 expected to work with actual openlayers and Drupal-core 7.26?
I can't get it to work ;-(

UPDATE: Sorry, works great!!! I tried to implement the hook in template.php (which was not working).
After putting the code in a module it works - thank you!!!

drupalok’s picture

Issue summary: View changes

can't get it to work...

added your code to a new module, enabled it. checked geofield...
but the new layer is not showing in "zoom to layer"
can you upload your working module please?

m.stenta’s picture

Category: Support request » Feature request

I'd like to know if there's a way this can be added to the Geofield module itself. It seems like a common requirement.

I don't quite understand why the code in #5 works, though. Is there a way to do it in the Geofield module without a hook_form_alter()?

m.stenta’s picture

Here's another way to accomplish the same thing: hook_openlayers_map_alter().

You can either enable it on a per-map basis:

/**
 * Implements hook_openlayers_map_alter().
 */
function geofield_openlayers_map_alter(&$map = array()) {

  // If this is my map, add the Geofield behavior as a zoom-to layer.
  if ($map['map_name'] == 'mymap') {
    $map['behaviors']['openlayers_behavior_zoomtolayer']['zoomtolayer']['openlayers_behavior_geofield'] = 'openlayers_behavior_geofield';
  }
}

or for all maps that have both a Geofield and Zoom-To-Layer behavior:

/**
 * Implements hook_openlayers_map_alter().
 */
function geofield_openlayers_map_alter(&$map = array()) {

  // If the geofield and zoomtolayer behaviors exist, add the Geofield behavior as a zoom-to layer.
  if (!empty($map['behaviors']['openlayers_behavior_geofield']) && !empty($map['behaviors']['openlayers_behavior_zoomtolayer'])) {
    $map['behaviors']['openlayers_behavior_zoomtolayer']['zoomtolayer']['openlayers_behavior_geofield'] = 'openlayers_behavior_geofield';
  }
}

I don't think either of these will work in the Geofield module, because both require pretty specific assumptions.

What I still don't understand is WHY this works. The "oppenlayers_behavior_geofield" is a BEHAVIOR. Not a LAYER. So why can we add it to the list of LAYERS that are zoomed to??

japo32’s picture

Thanks for this. #10 worked for our use case. We used the second solution to apply it to out custom maps.

ngunner’s picture

#10, 2nd option worked like a charm! Just make sure to clear caches after implementing it. My users will be so happy!