for geocluster, i was looking for a way to add the cluster information to data provided by views_geojson.

the attached patch adds a views_geojson_render_fields_alter which i currently use in a working draft of my geocluster module.

also note, that i successfully use the bounding box strategy with leaflet.

i'd be happy to hear your thoughts on my approach to clustering and the related views_geojson integration.
also see #1547610: Plan server-side geo clustering for drupal 7

more general thoughts on integrating geocluster with views are posted here: #1791796: Allow to inject a custom aggregation implementation

Comments

clemens.tolboom’s picture

+++ b/views_geojson.module
@@ -221,6 +221,12 @@ function _views_geojson_render_fields($view, $row, $index) {
+  foreach (module_implements('views_geojson_render_fields_alter') as $module) {
+    $function = $module . '_views_geojson_render_fields_alter';

You should use module_invoke_all

+++ b/views_geojson.module
@@ -221,6 +221,12 @@ function _views_geojson_render_fields($view, $row, $index) {
+    $function($feature, $view, $row, $index);

Isn't there a more general hook_field_alter available in views context? Maybe I'm just garbling ;)

clemens.tolboom’s picture

Status: Needs review » Needs work

You could add some documentation just before the _alter call too.

dasjo’s picture

hi clemens,

ad #1: module_invoke_all doesn't help, because it doesn't accept arguments being passed by reference. alternatively drupal_alter would be an option, but it only allows 2 (+1 deprecated) argument to be passed. in the end, i think the current implementation is most straight forward.

ad #2: it says, "Let modules modify the data". i guess that's what it does.

pol’s picture

Issue summary: View changes
Status: Needs work » Active

@dasjo, Is this the only way to achieve this ? There are no cleaner way using views hook ?

basvredeling’s picture

I think the only applicable views hook here could be hook_views_post_render. But that will likely be slower in many cases because you're altering already rendered output and doing an extra iteration over the results to access individual rows.

Regarding the proposed solutions:

  • I don't really like the foreach() either. It's cleaner to adhere to generic drupal altering patterns.
  • The module_invoke_all passing by reference limitation is mainly a memory issue if implemented correctly I guess. You could just pass it and change the data once it returns.
  • Drupal alter would be cleaner. Even with the limitation of only 2 arguments. Geocluster currently only alters one argument anyway (the row).

Finally there is #2370471: Make patching Views GeoJSON obsolete. That takes a different approach that looks pretty solid. But it probably also requires a rewrite of non-leaflet js to catch clustering properties.

basvredeling’s picture

Status: Active » Needs review
StatusFileSize
new406 bytes

Here's a patch containing the drupal_alter implementation. I'm also submitting an issue at the geocluster issue queue to properly implement the alter hook.

As a side note: I think this hook should be named hook_views_geojson_feature_alter() instead, because we're altering a "$feature", not a "$render_fields", within the contexts $view and $row. That's why I renamed the hook. This'll break the already broken geocluster implementation. See: #2370471: Make patching Views GeoJSON obsolete and http://cgit.drupalcode.org/geocluster/tree/geocluster.module#n169

basvredeling’s picture

basvredeling’s picture

Title: Add hook views_geojson_render_fields_alter » Add hook views_geojson_feature_alter

changed title to match patch in #7