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

Converting 7.x Drupal module to 8.x


drupal8_release_date_confusion

This is PART – I of creating module in Drupal8.

As we all know Drupal community are working very hard in development of Drupal8. So, I thought of writing about converting Drupal 7.x module to Drupal 8.x. Obviously porting to D7 module to D8 module will start when D8 core will get stable. But I was just curious to understand the changes made by community in D8 so I thought to take a dive in developing modules in D8 and along with that learn the changes with some coding , which probably will help at the time of module contribution for D8.

If you are not familiar with setting up D8 instance in your local machine then probably you should go through my earlier blog on Drupal8.

here’s the link :- https://beautiful7mind.wordpress.com/2013/02/11/whats-new-in-drupal-8-and-how-you-can-contribute

So, we will create one custom module in D8. Our module name is “alien”.

Steps to follow:-

  1. Create directory in modules . Note that now the directory structure of D8 core is changed . So , we will create all custom module in “drupal/modules/custom/”. Under custom directory create new directory that is your module directory name it “alien”. It will look like “drupal/modules/custom/alien”.
  2. Create some files in your alien directory  :-
  • alien.info.yml – [earlier .info file extention is change to .info.yml. If you give .info extension to your file your module will not be understood by Drupal8 core.]
  • alien.module – [contain code for doing some thing in site]
  • alien.install – [contain code for doing some thing in database of site ]

Now we will open file “alien.info.yml” in some editor and add below details into it.

name : alien
description : This module is to understand module development for D8.
package : core
version : VERSION
core : 8.x

That’s it. Save your alien.info.yml file. Now go to your site and Go to > Menu > Extend  here you are able to see your module “alien” in module list.

Hooorraaah it worked 🙂

Building maps using Openlayers in Drupal 7


Implementing maps in Drupal7 is not easy as it was in Drupal6 using Gmap but Gmap module is not developed for Drupal7. I was working with maps in Drupal7 and personally I felt that there are less documentation on maps in Drupal7. So, I thought to write this blog which can probably help my readers in implementation of maps. Related to implementation luckily we have Openlayers module in Drupal7.

Openlayers with maps, what sounds like the small task of showing some nodes on a map with the following collection of modules and dependencies:

  • Geofield : which hold the geo coordinates in our content
  • Address field: to hold postal address in our content
  • Geocoder: to convert address data into geographical data
  • geoPHP: Geocoder dependency
  • Openlayers

With some additional dependencies

  • Ctools
  • Views 3

Step 1: First you have to install above module and enable them included with Openlayers and its sub modules

  • openlayers_ui
  • openlayers_views

Step 2: Create content and add following fields

  • Addressfield : Postal address , zipcode , city
  • Location : field_type – Geofield , Widget – Geocode from another field

Selection_012

Address field will store your postal address of your business and geofield will take those address in location field and fetch geo coordinates of all businesses.

Selection_013

Step 3: Creating a Map

Openlayers come with many configurable option. I will describe basic options which you need for creating map. We need a base layer which is our map with some additional behaviors and styles on it. Secondly, Openlayer overlay which we will setup using views later.

Getting started with creating map

Go to structures > Openlayers > maps. Click add map and fill all details. Your map details should look like below.

Selection_014

Configure layer & style for your map.

Selection_015

Configure some behavior for map.

Selection_017

Selection_019

Now we are done with creating map which we will use for displaying data on it later.

Step 4: Creating Openlayer Overlay or you can say map view.

We need two things

  • Openlayer overlay to display data on map
  • Page or block to display map

Go to structures > Views > add new view .

Values for view

  • Show : Content
  • of type : Business information
  • create a page : Checked

Click on continue & edit. Here we will change some things in our view.

Add these values to your view

  • Format : Map > business map > Apply(all display)

Selection_020

  • Add Fields : 1) Title 2) Location . You should also set path of you page.

Now we will change some settings of our Openlayers overlay. Click on settings side by Format Openlayers Data Overlay | Settings.

  • Map data sources > WKT
  • WKT field > content : Location
  • Title Field > content : Title

Selection_021

What just we did ?

We created our map and added overlay for our data to display on map using views.

Last Step

We will add this Overlay to our map which we created earlier . Go to Structures > Openlayers > Maps > business map > layers & style . You probably see your view their in list . Just enable them so this map will use overlay for displaying content.

Selection_023

That’s it at this point we had a page available containing map with overlays displaying data when you hover them.

Selection_024

Thanks to Sonali , Anirudha , Chetan for reading draft of this.

Step by Step : How to implement Facet Search on data in Drupal 7


In this blog I will help you in setting up Facet search functionality on your  data in Drupal 7. However , If you are thinking about creating website where you can show your Big Data and with the help of facet search your website user can actually find specific data related to them. This blog will probably help you.

The Faceted Search module provides a search API and a search interface for allowing users to browse content in such a way that they can rapidly get acquainted with the scope and nature of the content, and never feel lost in the data. More than a search interface, this is an information navigation and discovery tool.

In order to make such functionality you need to install couple of modules in your site. We will first make a Indexing engine which will index all your data and then we make blocks of specific data which will allow you to filter  all related data to itself.

Modules to install for implementing Facet Search:

  1. Facet API
  2. Search API
  3. Database search
  4. Search Facets
  5. Search Views
  6. Entity API

We are done with installing and enabling above modules.

Steps to follow:

  1. Create content type and add fields in respective content type.
  2. Configure the search server
  3. configure the search index
  4. Add fields on in Facet API
  5. Create a complete view  of your data

Step 1. Create content type and add fields in that:

Let’s take an example we created content type

  • Title : Company
  • Fields : Address
  • Fields : County

Now just create content of above content type and add data into it.

Right now we have content with data into it and we have modules. Now we need to configure above modules as per our functionality on data.

Step 2. Configure the Search server:-

Go to configuration admin page (configuration/admin) in your Drupal 7 site and click on search API in Search and metadata section . you should be now on Search configuration setting. We will create a search engine for our data. You should see page like below :

Search configuration

Now click on Add server link .Give name to your server I gave (MYSQL) , Choose database service for your service class and click create server. Your server should be  like below.

Selection_002

Step 3. Configure the search Index:

Now we will make our search index which will index all data from using search server and database server. It will do indexing as user search for data on site and give fast result. Go back to search API settings and click on Add Index link. You should be on below page now.

Selection_004

Values for above form:

  • Index name : Business Information
  • Item type: Node
  • Server: MYSQL which you create earlier
  • Index items immediately : Checked

Click on create Index. Now we will select which field to be indexed from our data so we will select fields we want . In our case we will select

  • content type
  • Address
  • Title
  • County

and click save configuration .

What just happened ?

we created our database server , Index server and added fields which need to be index. As you added fields for indexing Facet API actually make Facet blocks of those fields . don’t focus on them right now we will enable them later.

Now run Cron in your site which populate the Index.

Step 4. Create a view of your data which need to be Indexed and Displayed to your site user :

Install Views and enable it . Go to Structure > Views. click add new view. Select below values for each field:

  • Show content : Business Information “This will be name of your Search server”
  • Create a page: checked
  • Path: node
  • Display format: Table

Your view information look like below:

Selection_005

Click on continue  & edit button . Now in the Fields section click on add fields and Filter Index node . Add Index fields you want to show to your user. Your selection will looks like below.

Selection_007

Complete all configuration of specific field and click Apply Display . You are done with creating view . You are now able to see this view on your front page(/node).

Next step is to enabling all blocks of your facet search . Earlier we added fields which need to be Indexed . Actually when you add fields for indexing Facet API it create Blocks of those fields respectively. You have to just enable them from Search settings.

Step 5. Enable Facet block from Search settings :

Go to configuration and click on Search API from search and metadata . Now just click on edit in Index . You are able to see various configuration options like below.

Selection_008

Click on Facets and enable facets you want . When you enable and save configuration. It will create Blocks of respective fields .

Step 6. Enable your Facet block from Blocks:

Now go to structures and blocks . Here you are able to see your facet block in Disable section just enable them in any regions of your theme.

congratulation your site is ready. Visit your home page and it should  look like below . As you select options from blocks your main view show you data as per your selection .

Note:-

Facet API have many configuration options . You should take a look to them like showing selection in Blocks in Checkbox etc. However , facet API not able to give you Multiple Facet search functionality . So , I am now working on developing custom module of widget and behavior which will give functionality of Multiple facet selection search . Facet API have OR and AND but only AND works not OR among fields.

Please free feel to ask your queries related to Facet search and If you need some more blog’s on Facet search for specific functionality or configuration or custom modification in Facet API then do comment probably I will help you by writing blog on respective.

Thanks to Chetan , Anirudha , Sonali for reading Draft of this .

Internet / Blogging VS You


This blog is describe some of my thoughts which I got from my working experience with different people . You can say its about the bad habits of people in their professional life which leads to their degradation . My focus here is that  you should think about these aspects and try to follow them. I will stress on certain topics like Social media post languages , why people fear from writing .

Your post shows your attitude on social networking sites

Many times I see people are posting or writing really bad way . I am not talking about fancy words or words by which they really didn’t mean anything. People really fail to structure their questions and answers in a way to easily understand by others .

And then they say “come on don’t take it seriously , It’s just internet. “

But actually their post making Internet worst place to store their data which is not understandable. What they fail in this is that they never reach to the highs which their masters achieved who follow some standards in their thoughts.

I feel Post or anything you share on social networking sites actually shows your thoughts , attitude towards your work you do and effort you put in your work. It’s basically a untold responsibility which everyone should understand that every character you write on Internet use some memory of database , It use hardware’s to keep your data store. It means you are misusing the technology with your bad data. I am not talking here about quality of data. I am talking here about not give correct shape to your data. Which you can give easily.

When you are asking any question you are not only asking question but you are expressing your self in front of other person . The way you ask question shows how deeply you understand that particular problem. So always before posting anything please keep your facts correct.

Think about it!

How do I get my fellow programmers to blog without blogging, to write without writing?

I understand saying some one that “You should write blog!” is really easy but it never works.  I know the pain behind writing  first blog is really tough. Blogging isn’t for every one. I am not sure what experience you have but Facebook, stack overflow  taught me how to write things effectively. You learn from good and bad’s of people. I always test my writing skills on Stackoverflow , Facebook because these are the place where I get quick reply on my questions. I understood how to write quality of questions , quality of answer , well structured , it also helped me in changing attitude when you ask question or answering to other person .

I learned writing from submitting proposal in conferences , making notes while working , writing comments in my programs . I understand many programmer have habit of making grammar mistake while writing. It’s not because you can’t write but it’s because you evolve a lot in programming syntax’s that you forgot English grammar .

In order to give push to yourself towards blogging I suggest you to checkout this site writers.stackexchange.com . Becoming expert writer is long way you can not become expert in one day.Process of writing is a journey of discovery which never ends .

Mostly you should write a lot.  I thought Jon Skeet well summed up here :-

Everyone should write a lot – whether it’s a blog, a book, Stack Overflow answers, emails or whatever. Write, and take some care over it. Clarifying your communication helps you to clarify your own internal thought processes, in my experience. It’s amazing how much you find you don’t know when you try to explain something in detail to someone else. It can start a whole new process of discovery.

I am not a good writer . Give me a push towards blogging!

Below I am including some thought’s of people and sources they use for improving their writing skills.

  1. Writing skills improve you memory check out  http://en.wikipedia.org/wiki/Working_memory  .
  2. Article on working memory http://cogprints.org/3246/1/Olive_Euro_Psy.pdf . Must read.
  3. Better communication http://apprenticecoder.wordpress.com
  4. Gerald Weinberg observed in his 1971 classic, The Psychology of Computer Programming, that the most important language for a programmer to know isn’t a programming language at all, but English, as programming is primarily a social activity

Thoughts:-

http://www.codinghorror.com/blog/2011/02/how-to-write-without-writing.html

http://firesofmay.blogspot.in/2012/09/question-and-answer.html