source: sipei/modules/views/help/api-tables.html

drupal-6.x
Last change on this file 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: 12.1 KB
Línea 
1<!-- $Id: api-tables.html,v 1.8 2009/08/22 09:22:43 dww Exp $ -->
2Tables are described to Views via hook_views_data(), which returns an array of table information, keyed by the name of the table. For example, if your module is describing three tables, 'foo', 'bar' and 'baz', your array will look like this:
3<pre>$data = array(
4  'foo' =&gt; array(
5    // ...info here...
6  ),
7  'bar' =&gt; array(
8    // ...info here...
9  ),
10  'baz' =&gt; array(
11    // ...info here...
12  ),
13);
14</pre>
15
16The key should be the actual database name of the table (not including prefix), but it can be an alias as long as the join information (explained later) contains the real name of the table.
17
18Each item in the array should be a field in the table, with the exception of a special information section called 'table'. Example:
19
20<pre>$data['foo'] = array(
21  'table' =&gt; array(
22    // ... info about the table, described later ...
23  ),
24  'bar' =&gt; array(
25    // ... info about the field named 'bar', i.e, foo.bar,
26  ),
27  'baz' =&gt; array(
28    // ... info about the field named 'baz', i.e, foo.baz,
29  ),
30);
31</pre>
32
33Once you get down to an array that contains actual data, that piece of the array will often be referred to as the definition.
34
35<h2>The 'table' section</h2>
36Each table should have a 'table' section in it, which is used to set default information for the table, such as the group, as well as the very important joins and whether or not this is a base table.
37
38First, there are several items that are actually for fields but can be placed here so that all fields within the table inherit them:
39<dl>
40<dt>group</dt>
41<dd>The name of the group this item will be with. In the UI, this is displayed as Group: Title. For example, "Node: Node ID", "Taxonomy: Term description", etc. It is important to be consistent with groups, because the UI sorts by group, and allows filtering by group to find fields as well.</dd>
42<dt>title</dt>
43<dd>The actual name of the field; it should be concise and descriptive.</dd>
44<dt>help</dt>
45<dd>A longer description to help describe what the field is or does. It should try to be only a line or two so as not to clutter the UI.</dd>
46</dl>
47
48In general, having 'title' and 'help' at the table level doesn't make a lot of sense, but usually every item in a table is in the same group. Thus it is very common to define the 'group':
49
50<pre>
51  $data['foo']['table']['group'] = t('Foo');
52</pre>
53
54<h3>Base table</h3>
55If your table is a base table -- meaning it can be the primary, central table for a View to use, you can declare it to be a base table. This primarily provides UI information so that it can be selected.
56For example:
57<pre>
58  // Advertise this table as a possible base table
59  $data['node']['table']['base'] = array(
60    'field' =&gt; 'nid',
61    'title' =&gt; t('Node'),
62    'help' =&gt; t("Nodes are a Drupal site's primary content."),
63    'weight' =&gt; -10,
64  );
65</pre>
66
67The following tags are available in the
68<dl>
69<dt>field</dt>
70<dd>The primary key field for this table. For Views to treat any table as a base table, it <b>must</b> have a primary field. For node this is the 'nid', for users this is the 'uid', etc. <strong>Without a single primary key field (i.e. not a composite key), Views will not be able to utilize the table as a base table.</strong> If your table does not have a primary key field, it is not too difficult to just add a serial field to it, usually.</dd>
71<dt>title</dt>
72<dd>The title of this table in the UI. It should be singular and describe the object that this table contains from the perspective of the user.</dd>
73<dt>help</dt>
74<dd>A short piece of text to describe what object this table contains.</dd>
75<dt>database</dt>
76<dd>If this table is held in a different database from your Drupal database, specify it as a string in the exact same format as the settings.php file. This is a special purpose variable that will probably be only used in site specific code, and <b>it must be the same database type as your Drupal database</b>. Also, don't try to join it to any table that isn't in the same database. That'll just create all kinds of silly errors. For example:
77<pre>
78  // In settings.php for your site
79  // Your drupal (site) database needs to be called 'default'
80  $db_url['default'] = 'mysqli://user:pass@host/drupal_db';
81  $db_url['budget'] = 'mysqli://user:pass@host/other_db';
82</pre>
83Then when you are describing the external database in your base table you would write something like this:
84<pre>
85  $data[$table]['table']['base'] = array(
86    'field' => 'Primary key',
87    'title' => t('Field name'),
88    'help' => t('Field description'),
89    'database' => 'budget',
90    'weight' => -10,
91    );
92</pre>
93</dd>
94</dl>
95
96<h3>Linking your table to existing base tables</h3>
97For Views to use your table, it has to either be a base table, or know how to link to an existing base table. Or sometimes both. Views uses this information to create a path to the base table; when the table is added to the query, Views will walk along this path, adding all tables required into the query.
98
99<div class="help-box" style="text-align:center">
100<a href="path:images/node-term_node-term_data-large.png"><img src="path:images/node-term_node-term_data.png" /></a>
101<em>How term_data joins to node</em>
102</div>
103
104In the above example, to use these with 'node' as the base table, both 'term_data' and 'term_node' need to be defined, and they each need a join handler for node:
105
106<pre>
107$data['term_data']['table']['join']['node'] = array(
108  'left_table' =&gt; 'term_node',
109  'left_field' =&gt; 'tid',
110  'field' =&gt; 'tid',
111);
112</pre>
113
114The above can be read as "In order to join to the node table, the term_data table must first link to the term_node table, and they join on the 'tid' field.". When adding this table to the query for a node view, Views will look at this and then look for the term_node table.
115
116<pre>
117$data['term_node']['table']['join']['node'] = array(
118  'left_field' =&gt; 'nid',
119  'field' =&gt; 'nid',
120);
121</pre>
122
123Above, the fact that 'left_table' is left out lets us know that term_node links directly to the node table, using the 'nid' field on both sides of the join.
124
125Quite a few more fields are available in this definition:
126<dl>
127  <dt>handler</dt>
128  <dd>The name of the handler object to use. Defaults to 'views_join'. You may create custom join handlers that may or may not use any of the data below, as they see fit.</dd>
129  <dt>table</dt>
130  <dd>Table to join. This is optional, and should only be used if the table being referenced is an alias.</dd>
131  <dt>field</dt>
132  <dd>Field to join on. This is required.</dd>
133  <dt>left_table</dt>
134  <dd>The next step toward the final destination. If this is the final destination it may be omitted.</dd>
135  <dt>left_field</dt>
136  <dd>The field to join to on the left side. This is required.</dd>
137  <dt>type</dt>
138  <dd>Either LEFT (default) or INNER.</dd>
139  <dt>extra</dt>
140  <dd>Either a string that's directly added, or an array of items. Each item is, itself, an array:
141    <dl>
142      <dt>field</dt>
143      <dd>Field or formula</dd>
144      <dt>operator</dt>
145      <dd>Similar to filters, this is the operator, such as &gt;, &lt;, =, etc. Defaults to = or IN.</dd>
146      <dt>value</dt>
147      <dd>Must be set. If an array, operator will be defaulted to IN.</dd>
148      <dt>numeric</dt>
149      <dd>If true, the value will not be surrounded in quotes, and %d will be used for its placeholder.</dd>
150      <dt>extra type</dt>
151      <dd> How all the extras will be combined. Either AND or OR. Defaults to AND.</dd>
152    </dl>
153  </dd>
154</dl>
155
156<h2>Describing fields on tables</h2>
157Aside from the special table tag, each table can also have an unlimited number of field designations; these correspond roughly to fields on the table, though it is very common to use non-fields to display data that isn't directly in a field, such as data arrived from formulae, or special links related to the object the table is part of.
158
159Each field is described in the view data with an array, keyed to the database name of the field. This array may contain some information fields, plus an entry in each of the five types of items Views has per field: argument, field, filter, relationship, sort. For example:
160
161<pre>
162$data['node']['nid'] = array(
163  'title' =&gt; t('Nid'),
164  'help' =&gt; t('The node ID of the node.'), // The help that appears on the UI,
165  // Information for displaying the nid
166  'field' =&gt; array(
167    'handler' =&gt; 'views_handler_field_node',
168    'click sortable' =&gt; TRUE,
169  ),
170  // Information for accepting a nid as an argument
171  'argument' =&gt; array(
172    'handler' =&gt; 'views_handler_argument_node_nid',
173    'name field' =&gt; 'title', // the field to display in the summary.
174    'numeric' =&gt; TRUE,
175    'validate type' =&gt; 'nid',
176  ),
177  // Information for accepting a nid as a filter
178  'filter' =&gt; array(
179    'handler' =&gt; 'views_handler_filter_numeric',
180  ),
181  // Information for sorting on a nid.
182  'sort' =&gt; array(
183    'handler' =&gt; 'views_handler_sort',
184  ),
185);
186</pre>
187
188The above example describes the 'nid' field on the 'node' table, providing 4 of the 5 handlers. Note that while field is normally expected to be the database name of the field, it doesn't have to be; you can use an alias (which is how you get multiple handlers per field) or something completely made up for items that aren't tied to the database. For example:
189
190<pre>
191$data['node']['edit_node'] = array(
192  'field' =&gt; array(
193    'title' =&gt; t('Edit link'),
194    'help' =&gt; t('Provide a simple link to edit the node.'),
195    'handler' =&gt; 'views_handler_field_node_link_edit',
196  ),
197);
198</pre>
199
200The above handler definition an edit link to a node, but this isn't a field in and of itself. For aliased fields, here is another example:
201
202<pre>
203$data['users']['uid_current'] = array(
204  'real field' =&gt; 'uid',
205  'title' =&gt; t('Current'),
206  'help' =&gt; t('Filter the view to the currently logged in user.'),
207  'filter' =&gt; array(
208    'handler' =&gt; 'views_handler_filter_user_current',
209  ),
210);
211</pre>
212
213The above definition provides an alternate filter handler on the uid field for the current user.
214
215The following items are allowed in the field definition:
216
217<dl>
218<dt>group, title, help</dt>
219<dd>As above, these fields are for the UI. If placed here, any of these fields will override a setting on the base table.</dd>
220<dt>real field</dt>
221<dd>If this field is an alias, the "real field" may be placed here, and the handler will never know the difference.</dd>
222
223<dt>field</dt>
224<dd>A handler definition for the "Field" section, which is a field that may be displayed in a view. The definition is an array; the contents of the array are completely up to the handler, other than the 'handler' definition. If omitted, handler will default to 'views_handler_field'.</dd>
225<dt>filter</dt>
226<dd>A handler definition for the "Filters" section, which will be used to apply WHERE clauses to the view. The definition is an array; the contents of the array are completely up to the handler, other than the 'handler' definition. If omitted, handler will default to 'views_handler_filter'.</dd>
227<dt>sort</dt>
228<dd>A handler definition for the "Sort criteria" section, which will be used to add an ORDER BY clause to the view. The definition is an array; the contents of the array are completely up to the handler, other than the 'handler' definition. If omitted, handler will default to 'views_handler_sort'.</dd>
229<dt>relationship</dt>
230<dd>A handler definition for the "Field" section, which is a way to bring in new or alternative base tables in the view. The definition is an array; the contents of the array are completely up to the handler, other than the 'handler' definition. If omitted, handler will default to 'views_handler_relationship'. The basic relationship handler requires 'base' and 'base field' to be set; 'base' and 'base field' represent the "right" half of the join that will use this field as the left side.</dd>
231<dt>argument</dt>
232<dd>A handler definition for the "Field" section, which is method of accepting user input from the URL or some other source. The definition is an array; the contents of the array are completely up to the handler, other than the 'handler' definition. If omitted, handler will default to 'views_handler_argument'.</dd>
233</dl>
234
235For more information about what handlers need/use what data, visit <a href="http://views.doc.logrus.com">the Views API site</a> and check out the available handlers.
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.