Projet

Général

Profil

Paste
Télécharger (8,01 ko) Statistiques
| Branche: | Révision:

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

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 object $values
117
 *   An object containing all retrieved values.
118
 * @param string $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 string $value
132
 *   The value being rendered.
133
 * @param string $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

    
142
    case 'url':
143
      $value = check_url($value);
144
      break;
145

    
146
    default:
147
      $value = check_plain($value);
148
      break;
149
  }
150
  return $value;
151
}
152
</pre>
153
These functions are meant to be used in the render() functions of field handlers,
154
for fetching data (usually by alias) from the $values object, and for sanitizing values.
155

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

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

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

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

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

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

    
188

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

    
192
<h3>Removed views-view-row-node</h3>
193
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.
194

    
195
<h3>Entity type Key on Base tables</h3>
196
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'].
197

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