api-upgrading.html 8.0 KB

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