Projet

Général

Profil

Paste
Télécharger (7,98 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / help / api-upgrading.html @ d719f12f

1
In order to take advantage of the changes in Drupal 7, Views has gone through several API changes.
2
Here's what you should know.
3

    
4
<h3>Handler registry</h3>
5

    
6
Views now uses Drupal's dynamic-loading code registry.
7
You can safely remove your implementations of hook_views_handlers(), since they are no longer used.
8

    
9
Please remember to specify the handlers in your module's .info file. For example:
10
<pre>
11
name = Example module
12
description = "Gives an example of a module."
13
core = 7.x
14
files[] = example.module
15
files[] = example.install
16

    
17
; Views handlers
18
files[] = includes/views/handlers/example_handler_argument_string.inc
19
</pre>
20

    
21
<h3>Removed handlers</h3>
22

    
23
Note that views_handler_filter_float has been removed.
24
This functionality is now handled by views_handler_filter_numeric.
25
There's no need for having a special handler any more, thanks to the new DB layer in Drupal 7.
26

    
27
views_handler_sort_formula has been removed.
28
Everyone who used it can extend from views_handler_sort, too.
29

    
30
<h3>Ctools dependency</h3>
31
Views requires ctools now, so it can use the dependency system of ctools.
32
The only thing you have to do is to remove views_process_dependency.
33

    
34
<h3>Changed add_where api</h3>
35
If your field is a plain sql field:
36
<pre>
37
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field " . $this->operator . " '%s'", $this->value);
38
</pre>
39
has to be converted to
40
<pre>
41
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
42
</pre>
43
If your field is a complex where condition:
44
<pre>
45
$this->query->add_where($this->options['group'], "$upper($field) NOT LIKE $upper('%%%s')", $this->value);
46
</pre>
47
has to be converted to
48
<pre>
49
$placeholder = $this->placeholder();
50
$this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
51
</pre>
52
placeholder() generates a automatic unique placeholder for you.
53

    
54
add_where with operator 'formula' can be converted to add_where_expression.
55
add_having with operator 'formula' can be converted to add_having_expression.
56

    
57
<h3>Changed place for display specific settings</h3>
58
In the new ui the new place for display settings is at the top of the second column.
59
Therefore use something like this code in your display plugin:
60
<pre>
61
$categories['block'] = array(
62
  'title' => t('Block settings'),
63
  'column' => 'second',
64
  'build' => array(
65
    '#weight' => -10,
66
  ),
67
);
68
</pre>
69

    
70
<h3>Changed filter settings and associated class variables</h3>
71
'optional' and 'single' are now 'required' and 'multiple', the logic is now opposite.
72
Also, the 'no_single' and 'no_optional' class variables (known as "object flags" in the API docs)
73
are now 'always_multiple' and 'always_required'.
74

    
75
<h3>Changed argument settings</h3>
76
See the init() function in views_handler_argument for an overview of everything
77
that changed.
78

    
79
1. The default actions 'summary asc', 'summary desc', 'summary asc by count', 'summary asc by count'
80
have been replaced by a single 'summary' action (which takes the sort order and type as options).
81
2. Wildcards are now called exceptions.
82
<pre>
83
$this->options['exception']['value'] = $options['wildcard'];
84
$this->options['exception']['title'] = $options['wildcard_substitution'];
85
</pre>
86
3. Summary plugin options are now stored in 'summary_options' instead of 'style_options'
87
<pre>
88
$this->options['summary_options'] = $options['style_options'];
89
</pre>
90
4. The selected summary plugin is no longer stored in 'style_plugin'.
91
<pre>
92
$this->options['summary']['format'] = $options['style_plugin'];
93
</pre>
94
5. The validator options have been moved.
95
<pre>
96
$options['validate']['type'] = $options['validate_type'];
97
$options['validate']['fail'] = $options['validate_fail'];
98
</pre>
99
6. The validator settings have been moved from $form['argument_validate'] to ['validate_options']
100
This means that dependent code in validate plugins needs to change.
101
Example change for views_plugin_argument_validate_user:
102
<pre>
103
    $form['roles'] = array(
104
       '#dependency' => array(
105
-        'edit-options-argument-validate-user-restrict-roles' => array(1),
106
+        'edit-options-validate-options-user-restrict-roles' => array(1),
107
       ),
108
</pre>
109

    
110
<h3>The introduction of get_value() and sanitize_value()</h3>
111
The views_handler class got two new functions:
112
<pre>
113
/**
114
 * Get the value that's supposed to be rendered.
115
 *
116
 * @param $values
117
 *   An object containing all retrieved values.
118
 * @param $field
119
 *   Optional name of the field where the value is stored.
120
 */
121
function get_value($values, $field = NULL) {
122
  $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
123
  if (isset($values->{$alias})) {
124
    return $values->{$alias};
125
  }
126
}
127

    
128
/**
129
 * Sanitize the value for output.
130
 *
131
 * @param $value
132
 *   The value being rendered.
133
 * @param $type
134
 *   The type of sanitization needed. If not provided, check_plain() is used.
135
 */
136
function sanitize_value($value, $type = NULL) {
137
  switch ($type) {
138
    case 'xss':
139
      $value = filter_xss($value);
140
      break;
141
    case 'url':
142
      $value = check_url($value);
143
      break;
144
    default:
145
      $value = check_plain($value);
146
      break;
147
  }
148
  return $value;
149
}
150
</pre>
151
These functions are meant to be used in the render() functions of field handlers,
152
for fetching data (usually by alias) from the $values object, and for sanitizing values.
153

    
154
The abstraction of fetching data from rendering data is important because
155
different query backends have different ways of storing data in $values, and the field alias
156
is a SQL specific thing. So instead of overriding the whole render() function and copying
157
all of the logic there (as well as having to constantly keep up with upstream Views changes),
158
the backend can just override get_values(), which is significantly less code.
159

    
160
Of course, different ways of fetching and displaying data might require different
161
ways of sanitizing it, hence the usage of the sanitize_value() function.
162

    
163
Examples of converting render() field handler implementations:
164
<pre>
165
// This
166
$value = $values->{$this->field_alias};
167
// Becomes this
168
$value = $this->get_value($values);
169

    
170
// And this
171
$format = $values->{$this->aliases['format']};
172
// Becomes this
173
$format = $this->get_values($values, 'format');
174

    
175
// Instead of this:
176
return check_plain($value);
177
// We write:
178
return $this->sanitize_value($value);
179

    
180
// Since sanitize_value() supports different sanitization functions, this:
181
return filter_xss($value);
182
// Can become:
183
return $this->sanitize_value($value, 'xss');
184
</pre>
185

    
186

    
187
<h3>Changed views_get_page_view</h3>
188
In contrast to 6.x views_get_page_view now does stores the current view, not the current page display.
189

    
190
<h3>Removed views-view-row-node</h3>
191
Due to changes in comment.module there is no extra views-view-row-node template needed to display the comments. If you do some custom stuff there you should now be able to do everything in your node.tpl.php.
192

    
193
<h3>Entity type Key on Base tables</h3>
194
During the development of the drupal7 version of views the entity type associated with a table got added to $data['name']['table']['base']['entity type']. It should be moved to $data['name']['table']['entity type'].
195

    
196
<h3>Changed views_plugin_style::render_grouping()</h3>
197
The parameters as well as the structure of the methods return have changed.
198
The method now accepts a third optional parameter called "$group_rendered".
199
This parameter defines whether to use the rendered or the raw field value for grouping.
200
Intention for adding the parameter was that the grouping could have been acted
201
unexpected if the rendered field contained unique values e.g. by using drupal_html_id().
202
<dl>
203
<dt>New return structure</dt>
204
<dd>
205
{grouping value} is the value affected by the new parameter.
206
<pre>
207
  array (
208
    {grouping value} => array(
209
      'group' => {rendered_value of the grouping field},
210
      'rows' => array({group rows}),
211
    ),
212
  );
213
</pre>
214
</dd>
215
<dt>Old return structure</dt>
216
<dd>
217
<strong>If the new parameter isn't explicitly set or its value is NULL the structure of the return will be the same as in D6!</strong>
218
<pre>
219
  array (
220
    {rendered_value of the grouping field} => array({group rows}),
221
  );
222
</pre>
223
</dd>
224
</dl>