source: sipes/modules_contrib/views/help/api-tables.html @ 65dadeb

stableversion-3.0
Last change on this file since 65dadeb was 59029b2, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo la version del modulo views

  • Propiedad mode establecida a 100644
File size: 12.2 KB
Línea 
1Tables 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:
2<pre>$data = array(
3  'foo' =&gt; array(
4    // ...info here...
5  ),
6  'bar' =&gt; array(
7    // ...info here...
8  ),
9  'baz' =&gt; array(
10    // ...info here...
11  ),
12);
13</pre>
14
15The 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.
16
17Each item in the array should be a field in the table, with the exception of a special information section called 'table'. Example:
18
19<pre>$data['foo'] = array(
20  'table' =&gt; array(
21    // ... info about the table, described later ...
22  ),
23  'bar' =&gt; array(
24    // ... info about the field named 'bar', i.e, foo.bar,
25  ),
26  'baz' =&gt; array(
27    // ... info about the field named 'baz', i.e, foo.baz,
28  ),
29);
30</pre>
31
32Once you get down to an array that contains actual data, that piece of the array will often be referred to as the definition.
33
34<h2>The 'table' section</h2>
35Each 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.
36
37First, there are several items that are actually for fields but can be placed here so that all fields within the table inherit them:
38<dl>
39<dt>group</dt>
40<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>
41<dt>title</dt>
42<dd>The actual name of the field; it should be concise and descriptive.</dd>
43<dt>help</dt>
44<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>
45</dl>
46
47In 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':
48
49<pre>
50  $data['foo']['table']['group'] = t('Foo');
51</pre>
52
53The other items in the 'table' section are described in the following sections.
54
55<h3>'base': Base table</h3>
56If 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.
57For example:
58<pre>
59  // Advertise this table as a possible base table
60  $data['node']['table']['base'] = array(
61    'field' =&gt; 'nid',
62    'title' =&gt; t('Node'),
63    'help' =&gt; t("Nodes are a Drupal site's primary content."),
64    'weight' =&gt; -10,
65  );
66</pre>
67
68The following items are available in the base section :
69<dl>
70<dt>field</dt>
71<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>
72<dt>title</dt>
73<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>
74<dt>help</dt>
75<dd>A short piece of text to describe what object this table contains.</dd>
76<dt>database</dt>
77<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:
78<pre>
79  // In settings.php for your site
80  // Your drupal (site) database needs to be called 'default'
81  $db_url['default'] = 'mysqli://user:pass@host/drupal_db';
82  $db_url['budget'] = 'mysqli://user:pass@host/other_db';
83</pre>
84Then when you are describing the external database in your base table you would write something like this:
85<pre>
86  $data[$table]['table']['base'] = array(
87    'field' => 'Primary key',
88    'title' => t('Field name'),
89    'help' => t('Field description'),
90    'database' => 'budget',
91    'weight' => -10,
92    );
93</pre>
94</dd>
95</dl>
96
97<h3>'join': Linking your table to existing base tables</h3>
98For 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.
99
100<div class="help-box" style="text-align:center">
101<a href="path:images/node-term_node-term_data-large.png"><img src="path:images/node-term_node-term_data.png" /></a>
102<em>How term_data joins to node</em>
103</div>
104
105In 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:
106
107<pre>
108$data['term_data']['table']['join']['node'] = array(
109  'left_table' =&gt; 'term_node',
110  'left_field' =&gt; 'tid',
111  'field' =&gt; 'tid',
112);
113</pre>
114
115The 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.
116
117<pre>
118$data['term_node']['table']['join']['node'] = array(
119  'left_field' =&gt; 'nid',
120  'field' =&gt; 'nid',
121);
122</pre>
123
124Above, 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.
125
126Quite a few more fields are available in this definition:
127<dl>
128  <dt>handler</dt>
129  <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>
130  <dt>table</dt>
131  <dd>Table to join. This is optional, and should only be used if the table being referenced is an alias.</dd>
132  <dt>field</dt>
133  <dd>Field to join on. This is required.</dd>
134  <dt>left_table</dt>
135  <dd>The next step toward the final destination. If this is the final destination it may be omitted.</dd>
136  <dt>left_field</dt>
137  <dd>The field to join to on the left side. This is required.</dd>
138  <dt>type</dt>
139  <dd>Either LEFT (default) or INNER.</dd>
140  <dt>extra</dt>
141  <dd>Either a string that's directly added, or an array of items. Each item is, itself, an array:
142    <dl>
143      <dt>field</dt>
144      <dd>Field or formula</dd>
145      <dt>operator</dt>
146      <dd>Similar to filters, this is the operator, such as &gt;, &lt;, =, etc. Defaults to = or IN.</dd>
147      <dt>value</dt>
148      <dd>Must be set. If an array, operator will be defaulted to IN.</dd>
149      <dt>numeric</dt>
150      <dd>If true, the value will not be surrounded in quotes, and %d will be used for its placeholder.</dd>
151    </dl>
152  </dd>
153  <dt>extra type</dt>
154  <dd> How all the extras will be combined. Either AND or OR. Defaults to AND.</dd>
155</dl>
156
157<h2>Describing fields on tables</h2>
158Aside 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.
159
160Each 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:
161
162<pre>
163$data['node']['nid'] = array(
164  'title' =&gt; t('Nid'),
165  'help' =&gt; t('The node ID of the node.'), // The help that appears on the UI,
166  // Information for displaying the nid
167  'field' =&gt; array(
168    'handler' =&gt; 'views_handler_field_node',
169    'click sortable' =&gt; TRUE,
170  ),
171  // Information for accepting a nid as an argument
172  'argument' =&gt; array(
173    'handler' =&gt; 'views_handler_argument_node_nid',
174    'name field' =&gt; 'title', // the field to display in the summary.
175    'numeric' =&gt; TRUE,
176    'validate type' =&gt; 'nid',
177  ),
178  // Information for accepting a nid as a filter
179  'filter' =&gt; array(
180    'handler' =&gt; 'views_handler_filter_numeric',
181  ),
182  // Information for sorting on a nid.
183  'sort' =&gt; array(
184    'handler' =&gt; 'views_handler_sort',
185  ),
186);
187</pre>
188
189The 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:
190
191<pre>
192$data['node']['edit_node'] = array(
193  'field' =&gt; array(
194    'title' =&gt; t('Edit link'),
195    'help' =&gt; t('Provide a simple link to edit the node.'),
196    'handler' =&gt; 'views_handler_field_node_link_edit',
197  ),
198);
199</pre>
200
201The 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:
202
203<pre>
204$data['users']['uid_current'] = array(
205  'real field' =&gt; 'uid',
206  'title' =&gt; t('Current'),
207  'help' =&gt; t('Filter the view to the currently logged in user.'),
208  'filter' =&gt; array(
209    'handler' =&gt; 'views_handler_filter_user_current',
210  ),
211);
212</pre>
213
214The above definition provides an alternate filter handler on the uid field for the current user.
215
216The following items are allowed in the field definition:
217
218<dl>
219<dt>group, title, help</dt>
220<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>
221<dt>real field</dt>
222<dd>If this field is an alias, the "real field" may be placed here, and the handler will never know the difference.</dd>
223
224<dt>field</dt>
225<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>
226<dt>filter</dt>
227<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>
228<dt>sort</dt>
229<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>
230<dt>relationship</dt>
231<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>
232<dt>argument</dt>
233<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>
234</dl>
235
236For 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.