annotate sites/all/modules/views/help/api-upgrading.html @ 2:b74b41bb73f0

-- Google analytics module
author danieleb <danielebarchiesi@me.com>
date Thu, 22 Aug 2013 17:22:54 +0100
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 In order to take advantage of the changes in Drupal 7, Views has gone through several API changes.
danielebarchiesi@0 2 Here's what you should know.
danielebarchiesi@0 3
danielebarchiesi@0 4 <h3>Handler registry</h3>
danielebarchiesi@0 5
danielebarchiesi@0 6 Views now uses Drupal's dynamic-loading code registry.
danielebarchiesi@0 7 You can safely remove your implementations of hook_views_handlers(), since they are no longer used.
danielebarchiesi@0 8
danielebarchiesi@0 9 Please remember to specify the handlers in your module's .info file. For example:
danielebarchiesi@0 10 <pre>
danielebarchiesi@0 11 name = Example module
danielebarchiesi@0 12 description = "Gives an example of a module."
danielebarchiesi@0 13 core = 7.x
danielebarchiesi@0 14 files[] = example.module
danielebarchiesi@0 15 files[] = example.install
danielebarchiesi@0 16
danielebarchiesi@0 17 ; Views handlers
danielebarchiesi@0 18 files[] = includes/views/handlers/example_handler_argument_string.inc
danielebarchiesi@0 19 </pre>
danielebarchiesi@0 20
danielebarchiesi@0 21 <h3>Removed handlers</h3>
danielebarchiesi@0 22
danielebarchiesi@0 23 Note that views_handler_filter_float has been removed.
danielebarchiesi@0 24 This functionality is now handled by views_handler_filter_numeric.
danielebarchiesi@0 25 There's no need for having a special handler any more, thanks to the new DB layer in Drupal 7.
danielebarchiesi@0 26
danielebarchiesi@0 27 views_handler_sort_formula has been removed.
danielebarchiesi@0 28 Everyone who used it can extend from views_handler_sort, too.
danielebarchiesi@0 29
danielebarchiesi@0 30 <h3>Ctools dependency</h3>
danielebarchiesi@0 31 Views requires ctools now, so it can use the dependency system of ctools.
danielebarchiesi@0 32 The only thing you have to do is to remove views_process_dependency.
danielebarchiesi@0 33
danielebarchiesi@0 34 <h3>Changed add_where api</h3>
danielebarchiesi@0 35 If your field is a plain sql field:
danielebarchiesi@0 36 <pre>
danielebarchiesi@0 37 $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field " . $this->operator . " '%s'", $this->value);
danielebarchiesi@0 38 </pre>
danielebarchiesi@0 39 has to be converted to
danielebarchiesi@0 40 <pre>
danielebarchiesi@0 41 $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
danielebarchiesi@0 42 </pre>
danielebarchiesi@0 43 If your field is a complex where condition:
danielebarchiesi@0 44 <pre>
danielebarchiesi@0 45 $this->query->add_where($this->options['group'], "$upper($field) NOT LIKE $upper('%%%s')", $this->value);
danielebarchiesi@0 46 </pre>
danielebarchiesi@0 47 has to be converted to
danielebarchiesi@0 48 <pre>
danielebarchiesi@0 49 $placeholder = $this->placeholder();
danielebarchiesi@0 50 $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
danielebarchiesi@0 51 </pre>
danielebarchiesi@0 52 placeholder() generates a automatic unique placeholder for you.
danielebarchiesi@0 53
danielebarchiesi@0 54 add_where with operator 'formula' can be converted to add_where_expression.
danielebarchiesi@0 55 add_having with operator 'formula' can be converted to add_having_expression.
danielebarchiesi@0 56
danielebarchiesi@0 57 <h3>Changed place for display specific settings</h3>
danielebarchiesi@0 58 In the new ui the new place for display settings is at the top of the second column.
danielebarchiesi@0 59 Therefore use something like this code in your display plugin:
danielebarchiesi@0 60 <pre>
danielebarchiesi@0 61 $categories['block'] = array(
danielebarchiesi@0 62 'title' => t('Block settings'),
danielebarchiesi@0 63 'column' => 'second',
danielebarchiesi@0 64 'build' => array(
danielebarchiesi@0 65 '#weight' => -10,
danielebarchiesi@0 66 ),
danielebarchiesi@0 67 );
danielebarchiesi@0 68 </pre>
danielebarchiesi@0 69
danielebarchiesi@0 70 <h3>Changed filter settings and associated class variables</h3>
danielebarchiesi@0 71 'optional' and 'single' are now 'required' and 'multiple', the logic is now opposite.
danielebarchiesi@0 72 Also, the 'no_single' and 'no_optional' class variables (known as "object flags" in the API docs)
danielebarchiesi@0 73 are now 'always_multiple' and 'always_required'.
danielebarchiesi@0 74
danielebarchiesi@0 75 <h3>Changed argument settings</h3>
danielebarchiesi@0 76 See the init() function in views_handler_argument for an overview of everything
danielebarchiesi@0 77 that changed.
danielebarchiesi@0 78
danielebarchiesi@0 79 1. The default actions 'summary asc', 'summary desc', 'summary asc by count', 'summary asc by count'
danielebarchiesi@0 80 have been replaced by a single 'summary' action (which takes the sort order and type as options).
danielebarchiesi@0 81 2. Wildcards are now called exceptions.
danielebarchiesi@0 82 <pre>
danielebarchiesi@0 83 $this->options['exception']['value'] = $options['wildcard'];
danielebarchiesi@0 84 $this->options['exception']['title'] = $options['wildcard_substitution'];
danielebarchiesi@0 85 </pre>
danielebarchiesi@0 86 3. Summary plugin options are now stored in 'summary_options' instead of 'style_options'
danielebarchiesi@0 87 <pre>
danielebarchiesi@0 88 $this->options['summary_options'] = $options['style_options'];
danielebarchiesi@0 89 </pre>
danielebarchiesi@0 90 4. The selected summary plugin is no longer stored in 'style_plugin'.
danielebarchiesi@0 91 <pre>
danielebarchiesi@0 92 $this->options['summary']['format'] = $options['style_plugin'];
danielebarchiesi@0 93 </pre>
danielebarchiesi@0 94 5. The validator options have been moved.
danielebarchiesi@0 95 <pre>
danielebarchiesi@0 96 $options['validate']['type'] = $options['validate_type'];
danielebarchiesi@0 97 $options['validate']['fail'] = $options['validate_fail'];
danielebarchiesi@0 98 </pre>
danielebarchiesi@0 99 6. The validator settings have been moved from $form['argument_validate'] to ['validate_options']
danielebarchiesi@0 100 This means that dependent code in validate plugins needs to change.
danielebarchiesi@0 101 Example change for views_plugin_argument_validate_user:
danielebarchiesi@0 102 <pre>
danielebarchiesi@0 103 $form['roles'] = array(
danielebarchiesi@0 104 '#dependency' => array(
danielebarchiesi@0 105 - 'edit-options-argument-validate-user-restrict-roles' => array(1),
danielebarchiesi@0 106 + 'edit-options-validate-options-user-restrict-roles' => array(1),
danielebarchiesi@0 107 ),
danielebarchiesi@0 108 </pre>
danielebarchiesi@0 109
danielebarchiesi@0 110 <h3>The introduction of get_value() and sanitize_value()</h3>
danielebarchiesi@0 111 The views_handler class got two new functions:
danielebarchiesi@0 112 <pre>
danielebarchiesi@0 113 /**
danielebarchiesi@0 114 * Get the value that's supposed to be rendered.
danielebarchiesi@0 115 *
danielebarchiesi@0 116 * @param $values
danielebarchiesi@0 117 * An object containing all retrieved values.
danielebarchiesi@0 118 * @param $field
danielebarchiesi@0 119 * Optional name of the field where the value is stored.
danielebarchiesi@0 120 */
danielebarchiesi@0 121 function get_value($values, $field = NULL) {
danielebarchiesi@0 122 $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
danielebarchiesi@0 123 if (isset($values->{$alias})) {
danielebarchiesi@0 124 return $values->{$alias};
danielebarchiesi@0 125 }
danielebarchiesi@0 126 }
danielebarchiesi@0 127
danielebarchiesi@0 128 /**
danielebarchiesi@0 129 * Sanitize the value for output.
danielebarchiesi@0 130 *
danielebarchiesi@0 131 * @param $value
danielebarchiesi@0 132 * The value being rendered.
danielebarchiesi@0 133 * @param $type
danielebarchiesi@0 134 * The type of sanitization needed. If not provided, check_plain() is used.
danielebarchiesi@0 135 */
danielebarchiesi@0 136 function sanitize_value($value, $type = NULL) {
danielebarchiesi@0 137 switch ($type) {
danielebarchiesi@0 138 case 'xss':
danielebarchiesi@0 139 $value = filter_xss($value);
danielebarchiesi@0 140 break;
danielebarchiesi@0 141 case 'url':
danielebarchiesi@0 142 $value = check_url($value);
danielebarchiesi@0 143 break;
danielebarchiesi@0 144 default:
danielebarchiesi@0 145 $value = check_plain($value);
danielebarchiesi@0 146 break;
danielebarchiesi@0 147 }
danielebarchiesi@0 148 return $value;
danielebarchiesi@0 149 }
danielebarchiesi@0 150 </pre>
danielebarchiesi@0 151 These functions are meant to be used in the render() functions of field handlers,
danielebarchiesi@0 152 for fetching data (usually by alias) from the $values object, and for sanitizing values.
danielebarchiesi@0 153
danielebarchiesi@0 154 The abstraction of fetching data from rendering data is important because
danielebarchiesi@0 155 different query backends have different ways of storing data in $values, and the field alias
danielebarchiesi@0 156 is a SQL specific thing. So instead of overriding the whole render() function and copying
danielebarchiesi@0 157 all of the logic there (as well as having to constantly keep up with upstream Views changes),
danielebarchiesi@0 158 the backend can just override get_values(), which is significantly less code.
danielebarchiesi@0 159
danielebarchiesi@0 160 Of course, different ways of fetching and displaying data might require different
danielebarchiesi@0 161 ways of sanitizing it, hence the usage of the sanitize_value() function.
danielebarchiesi@0 162
danielebarchiesi@0 163 Examples of converting render() field handler implementations:
danielebarchiesi@0 164 <pre>
danielebarchiesi@0 165 // This
danielebarchiesi@0 166 $value = $values->{$this->field_alias};
danielebarchiesi@0 167 // Becomes this
danielebarchiesi@0 168 $value = $this->get_value($values);
danielebarchiesi@0 169
danielebarchiesi@0 170 // And this
danielebarchiesi@0 171 $format = $values->{$this->aliases['format']};
danielebarchiesi@0 172 // Becomes this
danielebarchiesi@0 173 $format = $this->get_values($values, 'format');
danielebarchiesi@0 174
danielebarchiesi@0 175 // Instead of this:
danielebarchiesi@0 176 return check_plain($value);
danielebarchiesi@0 177 // We write:
danielebarchiesi@0 178 return $this->sanitize_value($value);
danielebarchiesi@0 179
danielebarchiesi@0 180 // Since sanitize_value() supports different sanitization functions, this:
danielebarchiesi@0 181 return filter_xss($value);
danielebarchiesi@0 182 // Can become:
danielebarchiesi@0 183 return $this->sanitize_value($value, 'xss');
danielebarchiesi@0 184 </pre>
danielebarchiesi@0 185
danielebarchiesi@0 186
danielebarchiesi@0 187 <h3>Changed views_get_page_view</h3>
danielebarchiesi@0 188 In contrast to 6.x views_get_page_view now does stores the current view, not the current page display.
danielebarchiesi@0 189
danielebarchiesi@0 190 <h3>Removed views-view-row-node</h3>
danielebarchiesi@0 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.
danielebarchiesi@0 192
danielebarchiesi@0 193 <h3>Entity type Key on Base tables</h3>
danielebarchiesi@0 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'].
danielebarchiesi@0 195
danielebarchiesi@0 196 <h3>Changed views_plugin_style::render_grouping()</h3>
danielebarchiesi@0 197 The parameters as well as the structure of the methods return have changed.
danielebarchiesi@0 198 The method now accepts a third optional parameter called "$group_rendered".
danielebarchiesi@0 199 This parameter defines whether to use the rendered or the raw field value for grouping.
danielebarchiesi@0 200 Intention for adding the parameter was that the grouping could have been acted
danielebarchiesi@0 201 unexpected if the rendered field contained unique values e.g. by using drupal_html_id().
danielebarchiesi@0 202 <dl>
danielebarchiesi@0 203 <dt>New return structure</dt>
danielebarchiesi@0 204 <dd>
danielebarchiesi@0 205 {grouping value} is the value affected by the new parameter.
danielebarchiesi@0 206 <pre>
danielebarchiesi@0 207 array (
danielebarchiesi@0 208 {grouping value} => array(
danielebarchiesi@0 209 'group' => {rendered_value of the grouping field},
danielebarchiesi@0 210 'rows' => array({group rows}),
danielebarchiesi@0 211 ),
danielebarchiesi@0 212 );
danielebarchiesi@0 213 </pre>
danielebarchiesi@0 214 </dd>
danielebarchiesi@0 215 <dt>Old return structure</dt>
danielebarchiesi@0 216 <dd>
danielebarchiesi@0 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>
danielebarchiesi@0 218 <pre>
danielebarchiesi@0 219 array (
danielebarchiesi@0 220 {rendered_value of the grouping field} => array({group rows}),
danielebarchiesi@0 221 );
danielebarchiesi@0 222 </pre>
danielebarchiesi@0 223 </dd>
danielebarchiesi@0 224 </dl>