How to make custom behavior in Openlayers module with Drupal 7

Drupal with Openlayers

Couple of days back I was working with Openlayers in one of my project in D7. I got stuck with some functionality which forced me to make custom behavior which should work with Openlayer module in Drupal 7. I found Openlayers has very less documentation so I thought to write this blog. I will make some introduction about how to start with creating custom behavior which work with Openlayers. I am not focusing on some particular functionality but on giving base to your functionality.

I am assuming you know how to configure Openlayers and display map in Drupal 7.  So , here’s the situation you faced that you need to achieve some functionality which Openlayers don’t provide and then you have to create some module which should work with Openlayers.

Next step is to make a custom module . Go to sites/all/modules/ and create your module directory containing files

  • module_name.info
  • module_name.module

Also create some more directory in your module directory like

  • include — [It will contain all .inc files]
  • js — [javascript files ]
  • css — [css files ]

Now we will write some things in your modules .info file.

name = Module name
description = Allow you to draw polygon on map and it will search locations on map based on polygon draw.
core = 7.x
package = “Openlayers”
dependencies[] = openlayers

files[] = module_name.module
files[] = include/openlayers_behavior.inc

Step 2: We will add some code in modulename.module file.

<?php

// implementing hook_init()

function modulename_init(){
        

// here we are adding our css and javascript file
         drupal_add_js(drupal_get_path(‘module’, ‘open_draw’) .
      ‘/js/openlayers_behavior_drawfeatures.js’);
    drupal_add_css(drupal_get_path(‘module’, ‘open_draw’)  .
      ‘/css/openlayers_behavior_drawfeatures.css’);
 }
        

// implementing hook_openlayers_behaviors()
function modulename_openlayers_behaviors() {
  return array(
         ‘openlayers_behavior_region_click’ => array(   
    ‘title’ => t(‘Polygon search’),
    ‘description’ => t(‘It will allow search on maps when user will draw polygon on map’),
    ‘type’ => ‘layer’,
    ‘path’ => drupal_get_path(‘module’, ‘open_draw’) . ‘/include’,
    ‘behavior’ => array(
        ‘file’ => ‘openlayers_behavior.inc’,
      ‘class’ => ‘openlayers_behavior_drawfeatures’,
      ‘parent’ => ‘openlayers_behavior’,
    ),
   ),
  );
}

Step 3: We will make openlayers_behavior.inc in our include directory and add some behavior in it.

<?php

/**
 * behavior
 */
class openlayers_behavior extends openlayers_behavior {
  /**
   * Provide initial values for options.
   */
  function options_init() {
    return array(
      ‘element_id’ => ”,
      ‘feature_types’ => array(),
      ‘feature_limit’ => 0,
    );
  }

  function options_form($defaults = array()) {
    $features = array(
      ‘polygon’ => t(‘Polygon’),
    );

    return array(
      ‘feature_types’ => array(
        ‘#title’ => t(‘Available Features’),
        ‘#type’ => ‘checkboxes’,
        ‘#options’ => $features,
        ‘#description’ => t(‘Select what features are available to draw.’),
        ‘#default_value’ => isset($defaults[‘feature_types’]) ? $defaults[‘feature_types’] : array(),
      ),
    );
     }
}

That’s it now just enable your module and you can see your behavior in behavior section of Openlayers map settings.

Of course now our behavior are not doing anything we just able to see our behavior’s settings in Openlayers map settings. In order to make behavior functional now you have to just add javascript or Jquery in you /js directory and css in /css directory.

Advertisements

12 thoughts on “How to make custom behavior in Openlayers module with Drupal 7

  1. Hi, i follow your steps, but i can not see option fields in my behavior-list.
    maybe u can explain what shut happen.
    thanks for the articel

  2. Hi there! Do you use Twitter? I’d like to follow you if that would be okay. I’m absolutely enjoying your blog and look forward to new updates.

  3. Howdy just wanted to give you a brief heads up and let you know a few of the pictures
    aren’t loading correctly. I’m not sure why but I think its a linking issue.

    I’ve tried it in two different internet browsers and both show the same results.

  4. I’m now not sure where you are getting your info, however great topic. I needs to spend some time studying much more or figuring out more. Thanks for magnificent information I used to be searching for this information for my mission.

  5. Hi there, just became aware of your blog through Google, and
    found that it’s truly informative. I’m gonna watch out for brussels.
    I will appreciate if you continue this in
    future. A lot of people will be benefited from your writing.

    Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s