1 | |
---|
2 | Current for 6.x-2.0-alpha6 |
---|
3 | |
---|
4 | |
---|
5 | # Creating a new OpenLayers Behavior from Scratch |
---|
6 | |
---|
7 | First, you'll need to create a module. Of course, skip through this step if |
---|
8 | there's already a module that exists to which this behavior will be added. But |
---|
9 | if not, create a file called `modulename.info` with the contents |
---|
10 | |
---|
11 | core = "6.x" |
---|
12 | dependencies[] = "openlayers" |
---|
13 | name = "modulename" |
---|
14 | package = "OpenLayers" |
---|
15 | project = "modulename" |
---|
16 | |
---|
17 | In this case, you're creating a module just for this feature. So you'll need to |
---|
18 | implement a hook to notify OpenLayers that your module can do something for it. |
---|
19 | There's a hook called `hook_openlayers_behaviors` for this, and since your |
---|
20 | module is called modulename, its implementation should be |
---|
21 | `modulename_openlayers_behaviors`. A basic implementation would be |
---|
22 | |
---|
23 | function modulename_openlayers_behaviors() { |
---|
24 | return array( |
---|
25 | 'openlayers_behavior_mybehavior' => array( |
---|
26 | 'title' => t('My Behavior'), |
---|
27 | 'description' => t('Does something'), |
---|
28 | 'type' => 'layer', |
---|
29 | 'path' => drupal_get_path('module', 'modulename') |
---|
30 | .'/includes/behaviors', |
---|
31 | 'file' => 'openlayers_behavior_mybehavior.inc', |
---|
32 | 'behavior' => array( |
---|
33 | 'class' => 'openlayers_behavior_mybehavior', |
---|
34 | 'parent' => 'openlayers_behavior', |
---|
35 | ), |
---|
36 | ), |
---|
37 | ); |
---|
38 | } |
---|
39 | |
---|
40 | Note the essentials here: this tells the OpenLayers module that there is a file |
---|
41 | in `modules/modulename/includes/behaviors/` which implements a class called |
---|
42 | `openlayers_behavior_mybehavior`. It isn't strictly necessary to create an |
---|
43 | includes folder and a behaviors folder under it, but it's good practice so that |
---|
44 | your module doesn't become cluttered. |
---|
45 | |
---|
46 | So on to the PHP that this hook refers to: usually there's only a small amount |
---|
47 | of PHP written for each behavior. On the first level, the file simply must |
---|
48 | include a class that extends the class openlayers_behavior: |
---|
49 | |
---|
50 | class openlayers_behavior_mybehavior extends openlayers_behavior {} |
---|
51 | |
---|
52 | There'll be a little bit for this one, but it's not very functional - only |
---|
53 | adding JavaScript code and declaring forms. |
---|
54 | |
---|
55 | Here's what you'll write for this behavior: |
---|
56 | |
---|
57 | class openlayers_behavior_mybehavior extends openlayers_behavior {} |
---|
58 | |
---|
59 | function options_init() { |
---|
60 | return array( |
---|
61 | ); |
---|
62 | } |
---|
63 | |
---|
64 | function options_form($defaults) { |
---|
65 | return array( |
---|
66 | 'filteredlayer' => array( |
---|
67 | '#type' => 'select', |
---|
68 | '#options' => $this->map['layers'], |
---|
69 | '#description' => t('Select layer to filter'), |
---|
70 | '#default_value' => isset($defaults['filteredlayer']) ? |
---|
71 | $defaults['filteredlayer'] : NULL |
---|
72 | ), |
---|
73 | ); |
---|
74 | } |
---|
75 | |
---|
76 | function render(&$map) { |
---|
77 | drupal_add_js(drupal_get_path('module', 'mybehavior') |
---|
78 | .'/includes/behaviors/js/openlayers_behavior_mybehavior.js'); |
---|
79 | return $this->options; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | As you can see, there's an options_form method which is called when the list of |
---|
84 | behaviors is generated for each preset, and given a `$defaults` array if there |
---|
85 | have already been values saved. It isn't required to implement this method, |
---|
86 | although many behaviors will need it. And at this level - in the options_form, |
---|
87 | you have access to the preset/map with $this - so you can get layers, styles, |
---|
88 | and other parts of the preset to play around with The `render(&$map)` function |
---|
89 | is indeed required, since it is called for every behavior. |
---|
90 | |
---|
91 | There's quite a bit of Javascript to write for this behavior: |
---|
92 | |
---|
93 | /** |
---|
94 | * Maptimeline Behavior |
---|
95 | */ |
---|
96 | Drupal.behaviors.openlayers_behavior_mybehavior = function(context) { |
---|
97 | |
---|
98 | var data = $(context).data('openlayers'); |
---|
99 | var slider_div = {}; |
---|
100 | if (data && data.map.behaviors['openlayers_behavior_mybehavior']) { |
---|
101 | behavior = data.map.behaviors['openlayers_behavior_mybehavior']; |
---|
102 | layer = data.openlayers.getLayersBy( |
---|
103 | 'drupalID', |
---|
104 | behavior.filteredlayer)[0]; |
---|
105 | // Do things with this feature, etc. |
---|
106 | }); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | Note the essentials of this file: all of the functionality needed is contained |
---|
111 | in a single function, `Drupal.behaviors.openlayers_behavior_mybehavior`. The |
---|
112 | facts that the containing function is called `openlayers_behavior_mybehavior` |
---|
113 | and that it receives a single argument, `context`, are essential, but besides |
---|
114 | those restrictions, behaviors can contain any Javascript code whatsoever. |
---|
115 | Behaviors are called after all layers and styles are added to the map and the |
---|
116 | map is rendered. |
---|
117 | |
---|
118 | This code demonstrates a few core concepts of behavior-writing: |
---|
119 | |
---|
120 | * The OpenLayers [Map object](http://dev.openlayers.org/releases/OpenLayers-2.8/doc/apidocs/files/OpenLayers/Map-js.html) |
---|
121 | is accessible via `$(context).data('openlayers').openlayers` |
---|
122 | * The [jQuery Data function](http://api.jquery.com/jQuery.data/) is used in the |
---|
123 | OpenLayers module to simplify variable scope and avoid the possibility of |
---|
124 | memory leaks. |
---|