Hi - I've been trying to figure out a solution to having the cck map widget zoom to a feature in node/edit for the past few hours and just can't see a way through. It works fine in node/view.

Have tried with hook_openlayers_map_alter to assign the zoomtolayer behaviour to the right layer but it strikes me that the problem is that that layer information is simply not available in edit mode.

While when in node/view we get an openlayers_cck_vector_layer in the $map array under behaviors with all the relevant WKT info in edit mode that is simply not there.

Also in the OL code I see this for the theming function in edit mode:

function theme_openlayers_cck_map($field = array(), $map = array()) {
  // TODO: fix
  return $map;
}

and I am guessing that TODO is a rather telling sign.

Would be grateful is you could:

a) Let me know if I am just being silly and it can easily be done or simply can't be done for some reason
b) Let me know if that TODO above points to just this type of work and a couple of pointers and will be happy to try to do the patch myself
c) Anything else you think might help!

Thanks

ps: have it as a bug report because I thought it should be there but it isn't but it may well be a feature request :-)

CommentFileSizeAuthor
#16 view.png66.82 KBfigs
#16 edit.png72.63 KBfigs
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sanjo’s picture

+1

Have the same problem. Maybe I will look into it tomorrow.

I think that's a feature of the OpenLayers Geocoder. (http://drupal.org/cvs?commit=359388)
So maybe we can port this feature to OpenLayers and add an option for this.

steinmb’s picture

+1
Is this a "hidden" feature or is it still pending? I would love to see my node zoom to layer when edition the node.

zzolo’s picture

Category: bug » feature

This is not currently possible without a map_alter at the moment.

This "issue" is that there is no support in the interface for setting behaviors for the input layer on the widget. This could be done in a similar fashion as the widget behavior settings for the formatter map. The idea is the same; basically the preset interface doesn't know about the layers used for input and display on the map.

Also, note that the Geocoder behavior there is for zooming to a geocoded value.

Also, you could easily do this with a hook_openlayers_map_alter() if you wanted.

ronald_istos’s picture

zzolo tried to do it with hook_openlayers_map_alter but simply can't get it work. alters work fine in any other place... is there something I am missing?

zzolo’s picture

Hey @ronald_istos. Have you tested to make sure the hook is firing? (suggestion would be to do a drupal_set_message())

From there, you just want to manually add the zoom_to_layer behavior with the CCK layer name in it. You'll have to look at the code and output the map array to see this all in action, but it should only be a few lines of code. Something like the following (though do not quote me as I am just going off memory here):

$map['behaviors']['zoom_to_layer'] = array(
  'layers' => array(
    'openlayers_cck_input_layer',
  ),
  'zoom' => 5,
  'other options I am forgetting about' => 1,
),
ronald_istos’s picture

zzolo the hook fires and you can adjust the zoom - but the information for where to zoom at is not available in the array. The centerpoint zoom is the one of the preset.

While, when viewing a node you have the cck_vector_layer to get the info from you don't have that info when editing a node.

I guess you would need to load the node - get the info if there is any to get at and then adjust the zoom and center point based on that.

zzolo’s picture

So, what you have to do is look at the Draw Features behavior, as it creates the layer for the editing, which is named: "openlayers_drawfeatures_layer".

Then, look at the zoomtolayer behavior, and what the parameters are.

Again, untested, but should be able to:

$map['behaviors']['zoomtolayer'] = array(
  'layers' => array(
    'openlayers_drawfeatures_layer',
  ),
  'point_zoom_level' => 5,
),
Adam S’s picture

Version: 6.x-2.0-alpha8 » 6.x-2.x-dev

Does OpenLayers not zoom to feature (point) in CCK node edit as is? Or, has this been fixed? I created a view with NID as argument and a single field of WKT. This enables the preset to zoom but there is a big problem with it -- the point does not move.

zzolo’s picture

@Adam, this is still not in there at the moment.

You could do your method, but hide the feature layer you are zooming to with a blank style. The problem is that you are displaying the point you have created and t will not disappear because it is not on the editing layer.

mansspams’s picture

Some other approach is needed, zzolo. There is no info of points in $map array and http://drupalcontrib.org/api/function/hook_openlayers_map_alter/6 says 'Adding behaviors, pre-defined layers here will not work. This is good for minor tweaks after the map has been processed.' Is there another hook?

zzolo’s picture

Hi @LavaMeTender ak.... That docs site might not be up to date, you should refer to the documentation that comes with the module. See:
http://drupalcode.org/viewvc/drupal/contributions/modules/openlayers/doc...

Specially see hook_openlayers_map_preprocess_alter.

Agileware’s picture

For anyone that needs it, here is the code that works for me to get the zoom to layer on the cck vector layer.

Keep in mind that this is not an entire solution as in this state the code will affect all openlayers maps.
So you need to add checks to make sure you only modify the maps you want.

There doesn't seem to be any great way to work out which maps you are you are but you could potentially use the url (if you don't have other maps on the same page you don't want to affect) or the $map['preset_name'] or something.

<?php
/**
 * Implementation of hook_openlayers_map_preprocess_alter().
 *
 * Map array alter. Fired before processing the array, and
 * before checking for errors. The whole array is passed
 * along and will allow you to alter it in any way. This
 * is a good place to alter the map, if the other hooks
 * do not provide the functionality you need.
 *
 * @param $map
 * Map array
 */
function MYMODULE_openlayers_map_preprocess_alter(&$map) {
  $map['behaviors']['openlayers_behavior_zoomtolayer'] = array(
    'zoomtolayer' => 'openlayers_cck_vector_layer',
    'point_zoom_level' => 5,
  );
}
?>
figs’s picture

Could someone please explain for a complete noob how to use this code? Would love to get this working...

Agileware’s picture

You need to create a custom module, for this example called mymodule, by doing the following (this is just a basic example):

* Create a directory called mymodule in your sites/all/modules directory (or wherever you want to put it).
* Create a file called mymodule.info in the mymodule directory and in it have the following text (put whatever you want for the description):

name = My module
description = A brief description of what my module does.
core = 6.x

* Create a file called mymodule.module in the mymodule directory and in it have the following text (just copy and paste this exactly into the empty file):

<?php
/**
* Implementation of hook_openlayers_map_preprocess_alter().
*
* Map array alter. Fired before processing the array, and
* before checking for errors. The whole array is passed
* along and will allow you to alter it in any way. This
* is a good place to alter the map, if the other hooks
* do not provide the functionality you need.
*
* @param $map
* Map array
*/
function mymodule_openlayers_map_preprocess_alter(&$map) {
  $map['behaviors']['openlayers_behavior_zoomtolayer'] = array(
    'zoomtolayer' => 'openlayers_cck_vector_layer',
    'point_zoom_level' => 5,
  );
}

Then enable the "My module" module on the modules page (admin/build/modules).

You can change the name of the module to something more relevant by changing all occurrences of mymodule to something else (including directory names/file names/function names).

figs’s picture

Thanks for that.

figs’s picture

FileSize
72.63 KB
66.82 KB

...

figs’s picture

That works fine for me, IF I choose to zoom to the openlayers_cck_vector_layer layer. Can I use this same method to zoom to the Feature Layer?

I've tried swapping out openlayers_cck_vector_layer for openlayers_drawfeatures_layer and openlayers_cck_input_layer, but neither are working. What is the correct name to address the "Feature Layer"?

Thanks.

figs’s picture

Does the Feature Layer info exist in the array at all? Zzolo you seem to imply that it does, but I can't see where anyone has zoomed to the Feature Layer when editting a node. So is it possible to use the map_preprocess_alter method at all for this?

zzolo’s picture

Category: feature » support

@figs. It seems like people are able to make this happen. Output your map array in the map_alter to understand it better and to ensure that the pieces are there.

figs’s picture

Zzolo, if I look at the $maps array when viewing a node, in one array entry I get a point..

...
[openlayers_cck_vector_layer] => Array
    (
        [features] => Array
             (
                  [0] => Array
                       (
                             [wkt] => GEOMETRYCOLLECTION(POINT(128.8037109375 -26.391869671769))
                             [projection] => 4326
...

..which I can zoom to with a hook (using the same method Agileware used), but there is no similar layer available in the $maps array when editing the node. I'm not sure if anyone else got this working this way? I'm in above my head here and really appreciate the help.

Agileware’s picture

For me it works on the node view and edit pages.

This is what I have:

<?php
/**
 * OpenLayers Map Preprocess Alter
 *
 * Map array alter. Fired before processing the array, and
 * before checking for errors. The whole array is passed
 * along and will allow you to alter it in any way. This
 * is a good place to alter the map, if the other hooks
 * do not provide the functionality you need.
 *
 * @param $map
 * Map array
 */
function mymodule_openlayers_map_preprocess_alter(&$map) {
  // Override the zoom to layer behaviour so that it zooms to 
  // the CCK vector layer.
  // Try to single out only the maps we want to change so we
  // don't mess up zoom to layer on views and such.
  // Only if we are editing or viewing a node and the map is
  // using the default preset, not a views preset.
  if ($map['preset_name'] == 'mypresetname' &&
      arg(0) == 'node' && is_numeric(arg(1)) &&
      (arg(2) == 'edit' || !arg(2))) {
    $map['behaviors']['openlayers_behavior_zoomtolayer'] = array(
      'zoomtolayer' => 'openlayers_cck_vector_layer',
      'point_zoom_level' => 5,
    );
  }
}
?>

You would just need to change mymodule to your module name and mypresetname to the machine name of the preset that is being used on your cck field map.

figs’s picture

Thanks Agileware, but unfortunately this still doesn't work for me. This was essentially the same as what I was doing before when it worked for viewing a node but not editing a node.

When viewing it zooms to the correct data points (see http://drupal.org/files/issues/view_16.png).

But when editing the zoom falls back to the default zoom to location (see http://drupal.org/files/issues/edit_13.png).

As far as I can tell this happens is because openlayers_cck_vector_layer doesn't exist in $maps when editing, which is strange because the data I want to zoom to is visible on the map when editing! Perhaps I'm doing something else different. I only have the one preset defined; do I need more?

figs’s picture

No ideas?

Dave-B’s picture

+1

mikejw’s picture

I had a similar problem and in my case it was because I had Default Style set to "Invisible" in my map preset for the overlay layer.

zzolo’s picture

Status: Active » Closed (works as designed)

Closing. Sounds like you all figured it pretty well. Patches are welcome to put this in the module.

naught101’s picture

Status: Closed (works as designed) » Closed (duplicate)