api-default-views.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Views can be stored in the database, which is typical of smaller sites and hobby sites. However, Views may also be stored directly in the code as "default" views, (which simply means they're available by default). Modules often come with views that are specific to the module data, but it's also possible -- and <b>highly</b> recommended -- that sites which have separate "development" and "production" sites export their views into default views in a site-specific module. This makes it very easy to transfer views from dev to production without making database changes.
  2. <h3>Creating a module</h3>
  3. First, create a directory in <em>sites/all/modules</em> for your new module. Call it whatever you like, but for this example we will call it <em>mymodule</em>.
  4. In this directory, create a <em>mymodule.module</em> file. It can be empty for now, but it should at least contain an opening PHP tag:
  5. <pre>&lt;?php
  6. </pre>
  7. It should not contain a closing ?&gt; tag, as the closing ?&gt; tag is not required and anything AFTER the closing tag, such as a space or a linefeed, will be displayed directly to the browser and can potentially cause problems.
  8. The .module file will contain functions and drupal hooks. Hooks are specially named functions that Drupal will call in order to get your module's response at certain times while generating pages. The only function you will need for this exercise is the 'views_api' hook that tells Views that this module supports the Views API and what version:
  9. <pre>function mymodule_views_api() {
  10. return array('api' => 2.0);
  11. }
  12. </pre>
  13. For other uses you may well add additional functions.
  14. Second, you need to create a <em>mymodule.info</em> file:
  15. <pre>name = My module
  16. description = My site specific module.
  17. core = 6.x
  18. </pre>
  19. Once you have these two files set up, you should be able to activate your new module at the <em>Administer >> Modules</em> page.
  20. <h3>Exporting your views</h3>
  21. The easiest way to do this is to activate the 'views_export' module, and navigate to <em>Administer >> Structure >> Views >> Tools >> Bulk export</em> Place a check next to each view that you want in your module, type the module name into the text field, and click export. This will create the entire <em>hook_views_default_views()</em> function for you.
  22. You can also export individual views. If you do this, keep in mind that this export does not include the line that adds the exported $view into the larger $views array:
  23. <pre>$views[$view->name] = $view</pre>
  24. To place this into your <em>hook_views_default_views()</em> you will need to place that after the view, and make sure the function returns $views at the end.
  25. <h3>Placing your exported views into your module</h3>
  26. Cut and paste the entire output of the bulk export tool into mymodule.views_default.inc -- and be sure to put a &lt;?php at the top of the file so that the webserver knows that it's PHP code! Then visit the Views tools page and clear the Views cache. Your views should now be listed as <b>Overridden</b> on the view list page. If you <b>revert</b> these views, they will be removed from the database, but will remain in code.
  27. <h3>Theming your views in your module</h3>
  28. You can theme these views in the module and not need to rely on the theme to do this at all; and in fact, the theme can continue to override these just like it ordinarily would, even if your module provides a theme. This is very useful for distributing a module where the view needs to look "just so."
  29. To do this, you need to implement <em>hook_theme()</em> in your module:
  30. <pre>function mymodule_theme($existing) {
  31. return array(
  32. 'views_view__viewname__displayid' => array (
  33. 'arguments' => array('view' => NULL),
  34. 'template' => 'views-view--viewname--displayid',
  35. 'base hook' => 'views_view',
  36. 'path' => drupal_get_path('module', 'mymodule'),
  37. ),
  38. );
  39. }
  40. </pre>
  41. There are a small number of gotchas in doing this that you must be aware of.
  42. <ol>
  43. <li>When referring to a template filename, you always use dashes in the name. i.e, <em>views-view--viewname--displayid.tpl.php</em>. However, when referring to the hook or function names, you use underscores instead of dashes. i.e, <em>views_view</em> and <em>views_view__viewname__displayid</em></li>
  44. <li>The 'arguments' change based upon which of the 3 types you're overriding. There's the 'display', the 'style' and the 'row' style. The above code is assuming the display, which is usually just <em>views_view</em>. Here are the possibilities:
  45. <pre>display: array('view_array' => array(), 'view' => NULL),
  46. style: array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
  47. row: array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL),
  48. field: array('view' => NULL, 'field' => NULL, 'row' => NULL),
  49. </pre>
  50. Be sure to use the right arguments line or the theme system will not properly translate.
  51. </li>
  52. <li>The 'template' line should never include the extension, so drop the .tpl.php from it.</li>
  53. <li>You need to make sure that the Views preprocess functions get registered. The 'base hook' line in the definition does that, but it can only do it if it comes after the Views registration, which actually happens very late in theme building. 99% of the time, your module will come before Views. You have two choices to deal with this:
  54. <ol>
  55. <li>Set your module's weight to 11 or higher in the database. Views' weight is 10. You can make this happen automatically when the module is first installed by creating a mymodule.install file and using this code:
  56. <pre>function mymodule_install() {
  57. db_query("UPDATE {system} SET weight = 11 WHERE name = 'mymodule'");
  58. }
  59. </pre>
  60. If you use this method, the <em>base hook</em> should be set to the name of the original template being used. i.e, if this is a variate of views-view-list.tpl.php, this should be 'views_view_list'.
  61. </li>
  62. <li>You can also just force it to list the preprocessors without actually having to detect them. This doesn't require modifying your module's weight, which is not always possible, you can insert this code into the array:
  63. <pre> 'preprocess functions' => array(
  64. 'template_preprocess',
  65. 'template_preprocess_views_view',
  66. 'mymodule_preprocess_views_view__viewname_displayid',
  67. ),
  68. </pre>
  69. The first one is the global 'template_preprocess' function which all templates utilize. It does some basic things such as setting up $zebra and a few other items. See <a href="http://api.drupal.org/api/function/template_preprocess/6">api.drupal.org</a> for specifics.
  70. The second one is the plugin specific preprocess. Like 'base hook' it should conform to the name used by the original template. i.e, if the original template was <em>views-view-list.tpl.php</em> then that preprocess function would be named <em>template_preprocess_views_view_list</em>.
  71. The third one is your module's preprocess function, if it needs one. In general, you probably will not need one, and you should only attempt to use one if you are reasonably familiar with the concept of preprocess functions and Drupal's theme system in general. See Drupal's theme documentation for more information.
  72. </li>
  73. </ol>
  74. </li>
  75. <li>
  76. If you leave the path blank the template file will be searched for in "./" which is the Drupal install base path.
  77. </li>
  78. </ol>