Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Drupal Views development

12,969 views

Published on

Slides from Frederic G. MARAND's "Developing to the Views 7.3 API" at the Drupal Dev Days in Brussels, 2011-02-05.

More details and code access on my blog.

Published in: Technology
  • Be the first to comment

Drupal Views development

  1. 1. <ul>Developing for the Views 7.3 API Frédéric G. MARAND aka fgm aka OSInet </ul>
  2. 2. Session summary <ul><li>Concepts
  3. 3. Basic integration: default views & embedding
  4. 4. Drupal-style integration: lifecycle hooks
  5. 5. Views-style integration: handlers & plugins
  6. 6. Going further </li></ul>
  7. 7. 1. Concepts
  8. 8. Concepts: Is this for you ? <ul><li>Views UI
  9. 9. PHP5 OOP
  10. 10. Module development </li></ul>
  11. 11. Concepts: Views API, why care ? <ul><li>Code-based views
  12. 12. Views-based code
  13. 13. Views theming
  14. 14. Extra view types </li></ul>
  15. 15. Concepts: Why care ? More extras <ul><li>Handlers... </li><ul><li>Area contents
  16. 16. Argument types
  17. 17. Field types
  18. 18. Filters
  19. 19. Relationships
  20. 20. Sort rules </li></ul></ul><ul><li>Plugins... </li><ul><li>Access controls
  21. 21. Argument defaults
  22. 22. Argument validators
  23. 23. Caching mechanisms
  24. 24. Exposed forms
  25. 25. Localization
  26. 26. Pager types
  27. 27. Query engines
  28. 28. Layouts </li></ul></ul>
  29. 29. Concepts: I'm sold: what is a View ? <ul><li>A pre-built query </li><ul><li>SQL, NoSQL, WS...
  30. 30. From Views UI or from code
  31. 31. On known entities ....
  32. 32. .... not in the EFQ sense </li></ul></ul>
  33. 33. Concepts: What is a View, again ? <ul><li>A parametric query </li><ul><li>Arguments
  34. 34. Exposed filters </li></ul><li>A query with formatted results </li><ul><li>Layout plugins: Display, Style, Row
  35. 35. Themable
  36. 36. Yes, it means a REPORT </li></ul></ul>
  37. 37. Concepts: a report for coders <ul><li>The « exportable » concept </li><ul><li>Default views
  38. 38. Normal views
  39. 39. Overridden views </li></ul><li>Features, anyone ? </li></ul>
  40. 40. Concepts: enough talk <ul>Time for some <?php hooks(); ?> </ul>
  41. 41. 2. Basic default/embed
  42. 42. Basic: Hello, Views <ul><li>hook_views_api() </li><ul><li>Version matching
  43. 43. Component location
  44. 44. THE hook in your module </li></ul></ul>
  45. 45. Basic: HVA demo <ul>(Switch to code view) </ul>
  46. 46. Basic: intermezzo Intermezzo: hooks & OO in Views Skip to HVD
  47. 47. Basic: hooks & OO in Views <ul><li>Hooks
  48. 48. Passive observer pattern
  49. 49. Parent is in charge </li><ul><li>can override child
  50. 50. can extend child </li></ul><li>Implementations alter sibling results
  51. 51. « horizontal » </li></ul><ul><li>Views OO
  52. 52. Inheritance
  53. 53. Children are in charge </li><ul><li>can override parent
  54. 54. can extend parent </li></ul><li>No sibling results
  55. 55. « vertical » </li></ul>
  56. 56. Basic: Views objects <ul><li>views_db_object </li><ul><li>view
  57. 57. view_display </li></ul><li>views_object </li><ul><li>views_handler
  58. 58. views_plugin … And a small collection of miscellaneous classes </li></ul></ul>
  59. 59. Basic: Views hooks <ul><li>Pseudo-hooks </li><ul><li>Declare a list of data
  60. 60. Build a registry
  61. 61. Use exactly one specified piece of data
  62. 62. HV Data, HV DV, HV Plugins, HV QS… </li></ul><li>True hooks </li><ul><li>Accumulators: lifecycle hooks
  63. 63. Alter hooks: HV QA, HV ALA </li></ul></ul>
  64. 64. Basic: supporting custom tables <ul><li>hook_views_data </li><ul><li>Base tables </li><ul><li>[base] view type = base table
  65. 65. The [table] array </li></ul><li>Linked tables </li><ul><li>POV: From the table being defined
  66. 66. ...to another (left) table via a (left) field
  67. 67. ...iteratively
  68. 68. ...until a base table is found at the far left </li></ul><li>Do not forget relationship handlers. Just not here.
  69. 69. Yes, that's a pseudo-hook </li></ul></ul>Back to Intermezzo
  70. 70. Basic: altering the schema <ul><li>hook_views_data_alter </li><ul><li>Plain Old Drupal Alterator </li></ul><li>Use cases </li><ul><li>Expose new fields in a table supported by another module
  71. 71. Add new sorts, filters... to another module </li></ul><li>HVD/HVDA: Invocation time ? </li></ul>
  72. 72. Basic: HVD demo <ul>Add Views support to dblog.module (Switch to code view) </ul>
  73. 73. Basic: code-based Views <ul><li>hook_views_default_views </li><ul><li>Build a view object in code
  74. 74. PHP as a DSL ?
  75. 75. Didn't we see this somewhere else in Drupal ? </li></ul><li>Work process </li><ul><li>Views UI to/from code
  76. 76. Single or multiple view files
  77. 77. <?php ?> wrapper
  78. 78. File names </li></ul></ul>
  79. 79. Basic: HVDV demo <ul>Provide a default view to dblog.module (Switch to code view) </ul>
  80. 80. Basic: a page with a view <ul><li>views_embed_view($name, $display = 'default') </li><ul><li>The good: </li><ul><li>Dead simple to use
  81. 81. Argument passing </li></ul><li>The bad: </li><ul><li>Default display ? Really ?
  82. 82. $view unavailable </li></ul><li>The ugly: </li><ul><li>Display side effects </li></ul></ul></ul>
  83. 83. Basic: a page with a better view <ul><li>view::execute_display() </li><ul><li>Still side effects
  84. 84. But more control: $view is available </li></ul><li>view::preview() </li><ul><li>Core of views_embed_view()
  85. 85. Can refine the process as needed
  86. 86. $view available </li></ul></ul>
  87. 87. 3. Drupal-style: lifecycle hooks
  88. 88. Drupal style: early lifecycle hooks <ul><li>A bunch of traditional hooks
  89. 89. From view initialization to result rendering: </li><ul><li>HV_pre_view : </li><ul><li>start, no work done
  90. 90. Alter view with view::attachment_(before|after) </li></ul><li>HV_pre_build : </li><ul><li>Displays initialized
  91. 91. Alter view with view::attachment_(before|after)
  92. 92. Before query building </li></ul></ul></ul>
  93. 93. Drupal style: query lifecycle hooks <ul><li>HV_query_alter() </li><ul><li>Modify the query
  94. 94. Beware the aliases ! </li></ul><li>HV_query_substitutions </li><ul><li>Pseudo-hook: registry of ***CURRENT_USER*** -like
  95. 95. Uncommon in « userland » </li></ul><li>HV_pre_execute </li><ul><li>nodeAccess not yet applied </li></ul></ul>
  96. 96. Drupal style: render lifecycle hooks <ul><li>Query executed
  97. 97. HV_pre_render </li><ul><li>After the pre_render() methods in handlers </li></ul><li>HV_admin_links_alter </li><ul><li>Damage control on side effects
  98. 98. Example: hide inline edit controls in list </li></ul><li>Theming </li><ul><li>Another story in its own right </li></ul></ul>
  99. 99. 4. Views style: handlers & plugins
  100. 100. Views: handlers and plugins <ul><li>views_db_object : the helpful parent </li><ul><li>definition
  101. 101. options
  102. 102. forms </li></ul><li>views_handler </li><ul><li>define the query: fields, ordering, conditions, relations
  103. 103. define area content </li></ul><li>views_plugin </li><ul><li>everything else
  104. 104. including the query /engine/ </li></ul></ul>
  105. 105. Views: « layout » plugins <ul><li>Display </li><ul><li>Outer content
  106. 106. Define format: block, page, feed </li></ul><li>Style </li><ul><li>Row set
  107. 107. Grid, table, list... </li></ul><li>Row </li><ul><li>Optional: some style plugins (table) have to intervene between rows
  108. 108. Typical: views_plugin_row_fields </li></ul></ul>
  109. 109. Views: access plugins <ul><li>Avoid running unauthorized queries
  110. 110. Mandatory
  111. 111. Built -in: </li><ul><li>None
  112. 112. Permission (BCP)
  113. 113. Role </li></ul></ul>
  114. 114. Views: query plugin <ul><li>Build and run the actual query
  115. 115. Mandatory
  116. 116. Built-in: SQL via DBTNG (db_api in D6)
  117. 117. Opportunity to interface non-SQL services with Views. Samples/experiments: </li><ul><li>EntityFieldQuery http://drupal.org/project/efq_views
  118. 118. MongoDB http://bit.ly/mongodbviews
  119. 119. Web Services http://bit.ly/extendrviews
  120. 120. SPARQL, YQL, Wolfram Alpha, ApacheSolr... </li></ul></ul>
  121. 121. Views: other plugins <ul><li>Argument plugins </li><ul><li>Not arguments themselves
  122. 122. Default plugins
  123. 123. Validation plugins </li></ul><li>Cache plugins </li><ul><li>Built-in: none, basic time-based
  124. 124. Optional: views_content_cache </li></ul><li>Others </li><ul><li>Pagers, exposed_form, localization (none / core) </li></ul></ul>
  125. 125. Views: implementing plugins <ul><li>Declare to Views </li><ul><li>hook_views_plugins
  126. 126. views_discover_plugins </li></ul><li>File layout </li><ul><li>D7 autoloading: <module>.info
  127. 127. One per plugin
  128. 128. Naming conventions </li></ul><li>Alterability </li><ul><li>hook_views_plugins_alter() </li></ul></ul>
  129. 129. Views: access plugin demo <ul>Write a variable-based access control plugin (Switch to code view) </ul>
  130. 130. Views: access plugin questions <ul><li>Views caching ?
  131. 131. Non-Views caching ? </li></ul>
  132. 132. Views: simple handlers <ul><li>Filter handlers </li><ul><li>Add conditions rules
  133. 133. Translate to WHERE or HAVING for SQL
  134. 134. Express a problem space condition as a (set of) query engine conditions </li></ul><li>Sort handlers </li><ul><li>Add sorting rules
  135. 135. Translate to ORDER BY for SQL
  136. 136. Express a problem space sorting as a query ordering </li></ul></ul>
  137. 137. Views: field handlers <ul><li>Basic </li><ul><li>Expose a DB column under its own name </li></ul><li>Evolved </li><ul><li>Expose several aliases from a single column
  138. 138. Synthesize several columns into one field </li></ul></ul>
  139. 139. Views: complex handlers <ul><li>Relationships </li><ul><li>Remember implicit joins ? Make them explicit
  140. 140. Allow multiple named join paths from table to base </li></ul><li>Arguments </li><ul><li>Basically filters
  141. 141. But context-injected
  142. 142. Can modify page: title, breadcrumbs...
  143. 143. Can modify display: eg summary mode </li></ul><li>Aggregate GROUP BY support ? </li><ul><li>argument & field & filter & sort </li></ul></ul>
  144. 144. Views: area handlers <ul><li>Areas ? </li><ul><li>Header
  145. 145. Footer
  146. 146. Empty </li></ul><li>Why ? </li><ul><li>No more in-DB PHP to have dynamic area content
  147. 147. No more embedded views just for dynamic area content </li></ul><li>Why handlers ? </li><ul><li>Field handlers for the #global virtual table </li></ul></ul>
  148. 148. Views: implementing handlers <ul><li>Declare to Views </li><ul><li>No more hook_views_handlers
  149. 149. Properties: hook_views_data[_alter]
  150. 150. D7 autoloading: <module>.info </li></ul><li>File layout: like plugins </li></ul>
  151. 151. Views: area handler demo <ul>(Switch to code view) </ul>
  152. 152. 5. Going further
  153. 153. Going further: more info <ul><li>This is just an intro! </li><ul><li>Merlinofchaos' et alii book about Views 2 still relevant
  154. 154. http://bit.ly/cckviewspanels </li></ul><li>Online </li><ul><li>http://views.doc.logrus.com/
  155. 155. Advanced Help in your own site </li></ul><li>Freenode IRC : </li><ul><li>Work on Views itself: #drupal-views
  156. 156. Support for your Views code: #drupal and #drupal-contribute (for contrib) </li></ul></ul>
  157. 157. The mandatory plug <ul><li>This session is only an excerpt from the hands -on OSInet DR-713 course </li><ul><li>7-hour hands-on workshop
  158. 158. Available in English and French
  159. 159. On-demand in other languages </li></ul><li>OSInet is a state registered french training provider with a full Drupal training syllabus </li><ul><li>From content creator... to site builder ...
  160. 160. ...to advanced module developer [email_address] </li></ul></ul>
  161. 161. The « other » mandatory plug We're hiring ! [email_address]
  162. 162. <ul></Developing for the Views 7.3 API Frédéric G. MARAND aka fgm aka OSInet> </ul>

×