source: sipei/modules/views/help/api-example.html @ ffa4103

drupal-6.x
Last change on this file since ffa4103 was ffa4103, checked in by Luis Peña <lpena@…>, 12 años ago

Cambiando el nombre de modulos a modules

  • Propiedad mode establecida a 100755
File size: 6.7 KB
Línea 
1<!-- $Id: api-example.html,v 1.2 2009/09/15 15:44:14 merlinofchaos Exp $ -->
2
3For the new table defined by the Node example module to be understood by the views module you need to create a node_example.views.inc file that describes the table and its relationships to the rest of the database.  In order for views to know that this file is to be loaded you need to implement hook_views_api.  This is done by adding the following function into your node_example.module file
4
5<pre>
6&lt;?php
7/**
8 * Implementation of hook_views_api().
9 *
10 * This tells drupal that there is Views integration file named
11 * module-name.views.inc
12 */
13function node_example_views_api() {
14  // Note that you can include 'path' in this array so that your views.inc
15  // file can be stored in a different location.
16  return array(
17    'api' => 2.0
18  );
19}
20?&gt;
21</pre>
22
23Below is the contents of a simple node_example.views.inc file that allows you to create views that include the new color and quantity information.
24
25<pre>
26&lt;?php
27// $Id: api-example.html,v 1.2 2009/09/15 15:44:14 merlinofchaos Exp $
28
29/**
30 * This file is used to tell the views module about the new node_example table.
31 *
32 * Database definition:
33 * @code
34 *   CREATE TABLE node_example (
35 *     vid int(10) unsigned NOT NULL default '0',
36 *     nid int(10) unsigned NOT NULL default '0',
37 *     color varchar(255) NOT NULL default '',
38 *     quantity int(10) unsigned NOT NULL default '0',
39 *     PRIMARY KEY (vid, nid),
40 *     KEY `node_example_nid` (nid)
41 *   )
42 * @endcode
43 */
44
45function node_example_views_data()  {
46  // Basic table information.
47
48  // ----------------------------------------------------------------
49  // node_example table
50  //  New group within Views called 'Example'
51  //  The group will appear in the UI in the dropdown tha allows you
52  //  to narrow down which fields and filters are available.
53
54  $data = array();
55  $data['node_example']['table']['group']  = t('Example');
56
57  // Let Views know that our example table joins to the 'node'
58  // base table. This means it will be available when listing
59  // nodes and automatically make its fields appear.
60  //
61  // We also show up for node revisions.
62  $data['node_example']['table']['join'] = array(
63    'node_revisions' => array(
64      'left_field' => 'vid',
65      'field' => 'vid',
66    ),
67    'node' => array(
68      'left_field' => 'vid',
69      'field' => 'vid',
70    ),
71  );
72
73  // quantity
74  $data['node_example']['quantity'] = array(
75    'title' => t('Quantity'),
76    'help' => t('Quantity of items.'),
77    'field' => array(
78      'handler' => 'views_handler_field_numeric',
79      'click sortable' => TRUE,
80     ),
81    'filter' => array(
82      'handler' => 'views_handler_filter_numeric',
83    ),
84    'sort' => array(
85      'handler' => 'views_handler_sort',
86    ),
87  );
88
89  // Color             
90  $data['node_example']['color'] = array(
91    'title' => t('Color'),
92    'help' => t('Color of item.'),
93
94    'field' => array(
95      'handler' => 'views_handler_field',
96      'click sortable' => TRUE,
97     ),
98     'filter' => array(
99      'handler' => 'views_handler_filter_string',
100     ),
101     'argument' => array(
102       'handler' => 'views_handler_argument_string',
103     ),
104     'sort' => array(
105      'handler' => 'views_handler_sort',
106     ),
107  );
108
109  return $data;
110}
111
112?&gt;
113</pre>
114
115Some notes on usage:
116
117Within Views, click on the Add tab.  You have a number of type options here.  Normally you would select either 'Node' (if you only want to display information on current nodes) or 'Node revision' (if you want to display information on all revisions of the nodes)
118
119With this configuration you always pull out of the database, data for every single node, whether or not it has color and quantity information.  To display information on just those nodes that have color and quantity information you can use a filter so that only nodes which don't have a NULL color or a NULL quantity are displayed.
120
121<h3>Type/relationship extension</h3>
122
123When your tables have first class data, you will often need to have own View types and View relationships defined.  With the current node_example table this isn't required although I try to justify it below on an efficiency basis. See [[http://groups.drupal.org/node/17236#comment-58980|this discussion]] as to why it isn't justified.
124
125Pulling data out of the database for every node when you only want data for the new Example node type is inefficient.  To reduce the initial data extraction to just that relating to the new Example nodes requires that you make the node_example table the base table.  This can be done by adding the following code into the node_example.views.inc file just before the 'return $data;'
126
127<pre>
128&lt;?php
129
130//  **** Begin optional extra for type and relationships ****
131
132  //  Use node_example as a new base table
133  //     by creating a new views type called 'Node example'
134  //  This allows it to be selected as the 'view type'
135  //          when you initially add a new view.
136  $data['node_example']['table']['base'] = array(
137    'field' => 'vid',
138    'title' => t('Node example'),
139    'help' => t("Node example type with color and quantity information."),
140    'weight' => -9,
141  );
142
143  // When using the new 'Node example' type you need to use relationships
144  //   to access fields in other tables.
145
146  // Relationship to the 'Node revision' table
147  $data['node_example']['vid'] = array(
148    'title' => t('Node revision'),
149    'help' => t('The particular node revision the color and quantity is attached to'),
150    'relationship' => array(
151      'label' => t('Node revision'),
152      'base' => 'node_revisions',
153      'base field' => 'vid',
154      // This allows us to not show this relationship if the base is already
155      // node_revisions so users won't create circular relationships.
156      'skip base' => array('node', 'node_revisions'),
157    ),
158  );
159
160  // Relationship to the 'Node' table
161  $data['node_example']['nid'] = array(
162    'title' => t('Node'),
163    'help' => t('The particular node the color and quantity is attached to'),
164    'relationship' => array(
165      'label' => t('Node'),
166      'base' => 'node',
167      'base field' => 'nid',
168      // This allows us to not show this relationship if the base is already
169      // node so users won't create circular relationships.
170      'skip base' => array('node', 'node_revisions'),
171    ),
172  );
173
174//  **** End optional extra for type and relationships ****
175
176?&gt;
177</pre>
178
179The above code adds a new 'Node example' to the view types that can be selected within the Add tab window of views.  Selecting this sets the node_example table to be the base table.
180
181If you select 'Node example' as view type, when you initially go into the edit window of views you will find  the only fields available are the color and quantity fields.  To get fields from other tables you need to add a relationship.  Relationships may be found at the top in the same column as the fields.
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.