source: sipes/modules_contrib/openlayers/docs/BEHAVIORS.txt @ c43ea01

stableversion-3.0
Last change on this file since c43ea01 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100644
File size: 4.8 KB
Línea 
1
2Current for 6.x-2.0-alpha6
3
4
5# Creating a new OpenLayers Behavior from Scratch
6
7First, you'll need to create a module. Of course, skip through this step if
8there's already a module that exists to which this behavior will be added. But
9if 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
17In this case, you're creating a module just for this feature. So you'll need to
18implement a hook to notify OpenLayers that your module can do something for it.
19There's a hook called `hook_openlayers_behaviors` for this, and since your
20module 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
40Note the essentials here: this tells the OpenLayers module that there is a file
41in `modules/modulename/includes/behaviors/` which implements a class called
42`openlayers_behavior_mybehavior`. It isn't strictly necessary to create an
43includes folder and a behaviors folder under it, but it's good practice so that
44your module doesn't become cluttered.
45
46So on to the PHP that this hook refers to: usually there's only a small amount
47of PHP written for each behavior. On the first level, the file simply must
48include a class that extends the class openlayers_behavior:
49
50    class openlayers_behavior_mybehavior extends openlayers_behavior {}
51
52There'll be a little bit for this one, but it's not very functional - only
53adding JavaScript code and declaring forms.
54
55Here'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
83As you can see, there's an options_form method which is called when the list of
84behaviors is generated for each preset, and given a `$defaults` array if there
85have already been values saved. It isn't required to implement this method,
86although many behaviors will need it. And at this level - in the options_form,
87you have access to the preset/map with $this - so you can get layers, styles,
88and other parts of the preset to play around with The `render(&$map)` function
89is indeed required, since it is called for every behavior.
90
91There'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
110Note the essentials of this file: all of the functionality needed is contained
111in a single function, `Drupal.behaviors.openlayers_behavior_mybehavior`. The
112facts that the containing function is called `openlayers_behavior_mybehavior`
113and that it receives a single argument, `context`, are essential, but besides
114those restrictions, behaviors can contain any Javascript code whatsoever.
115Behaviors are called after all layers and styles are added to the map and the
116map is rendered.
117
118This 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.
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.