2 * Implements hook_views_data().
4 function {{ machine_name }}_views_data() {
5 // This example describes how to write hook_views_data() for a table defined
7 // CREATE TABLE example_table (
8 // nid INT(11) NOT NULL COMMENT 'Primary key: {node}.nid.',
9 // plain_text_field VARCHAR(32) COMMENT 'Just a plain text field.',
10 // numeric_field INT(11) COMMENT 'Just a numeric field.',
11 // boolean_field INT(1) COMMENT 'Just an on/off field.',
12 // timestamp_field INT(8) COMMENT 'Just a timestamp field.',
13 // langcode VARCHAR(12) COMMENT 'Language code field.',
17 // Define the return array.
20 // The outermost keys of $data are Views table names, which should usually
21 // be the same as the hook_schema() table names.
22 $data['example_table'] = [];
24 // The value corresponding to key 'table' gives properties of the table
26 $data['example_table']['table'] = [];
28 // Within 'table', the value of 'group' (translated string) is used as a
29 // prefix in Views UI for this table's fields, filters, etc. When adding
30 // a field, filter, etc. you can also filter by the group.
31 $data['example_table']['table']['group'] = t('Example table');
33 // Within 'table', the value of 'provider' is the module that provides schema
34 // or the entity type that causes the table to exist. Setting this ensures
35 // that views have the correct dependencies. This is automatically set to the
36 // module that implements hook_views_data().
37 $data['example_table']['table']['provider'] = 'example_module';
39 // Some tables are "base" tables, meaning that they can be the base tables
40 // for views. Non-base tables can only be brought in via relationships in
41 // views based on other tables. To define a table to be a base table, add
42 // key 'base' to the 'table' array:
43 $data['example_table']['table']['base'] = [
44 // Identifier (primary) field in this table for Views.
47 'title' => t('Example table'),
48 // Longer description in the UI. Required.
49 'help' => t('Example table contains example content and can be related to nodes.'),
53 // Some tables have an implicit, automatic relationship to other tables,
54 // meaning that when the other table is available in a view (either as the
55 // base table or through a relationship), this table's fields, filters, etc.
56 // are automatically made available without having to add an additional
57 // relationship. To define an implicit relationship that will make your
58 // table automatically available when another table is present, add a 'join'
59 // section to your 'table' section. Note that it is usually only a good idea
60 // to do this for one-to-one joins, because otherwise your automatic join
61 // will add more rows to the view. It is also not a good idea to do this if
62 // most views won't need your table -- if that is the case, define a
63 // relationship instead (see below).
65 // If you've decided an automatic join is a good idea, here's how to do it;
66 // the resulting SQL query will look something like this:
67 // ... FROM example_table et ... JOIN node_field_data nfd
68 // ON et.nid = nfd.nid AND ('extra' clauses will be here) ...
69 // although the table aliases will be different.
70 $data['example_table']['table']['join'] = [
71 // Within the 'join' section, list one or more tables to automatically
72 // join to. In this example, every time 'node_field_data' is available in
73 // a view, 'example_table' will be too. The array keys here are the array
74 // keys for the other tables, given in their hook_views_data()
75 // implementations. If the table listed here is from another module's
76 // hook_views_data() implementation, make sure your module depends on that
78 'node_field_data' => [
79 // Primary key field in node_field_data to use in the join.
80 'left_field' => 'nid',
81 // Foreign key field in example_table to use in the join.
83 // 'extra' is an array of additional conditions on the join.
86 // Adds AND node_field_data.published = TRUE to the join.
87 'field' => 'published',
91 // Adds AND example_table.numeric_field = 1 to the join.
92 'left_field' => 'numeric_field',
94 // If true, the value will not be surrounded in quotes.
98 // Adds AND example_table.boolean_field <>
99 // node_field_data.published to the join.
100 'field' => 'published',
101 'left_field' => 'boolean_field',
102 // The operator used, Defaults to "=".
109 // You can also do a more complex join, where in order to get to a certain
110 // base table defined in a hook_views_data() implementation, you will join
111 // to a different table that Views knows how to auto-join to the base table.
112 // For instance, if another module that your module depends on had
113 // defined a table 'foo' with an automatic join to 'node_field_table' (as
114 // shown above), you could join to 'node_field_table' via the 'foo' table.
115 // Here's how to do this, and the resulting SQL query would look something
117 // ... FROM example_table et ... JOIN foo foo
118 // ON et.nid = foo.nid AND ('extra' clauses will be here) ...
119 // JOIN node_field_data nfd ON (definition of the join from the foo
120 // module goes here) ...
121 // although the table aliases will be different.
122 $data['example_table']['table']['join']['node_field_data'] = [
123 // 'node_field_data' above is the base we're joining to in Views.
124 // 'left_table' is the table we're actually joining to, in order to get to
125 // 'node_field_data'. It has to be something that Views knows how to join
126 // to 'node_field_data'.
127 'left_table' => 'foo',
128 'left_field' => 'nid',
130 // 'extra' is an array of additional conditions on the join.
132 // This syntax matches additional fields in the two tables:
133 // ... AND foo.langcode = example_table.langcode ...
134 ['left_field' => 'langcode', 'field' => 'langcode'],
135 // This syntax adds a condition on our table. 'operator' defaults to
136 // '=' for non-array values, or 'IN' for array values.
137 // ... AND example_table.numeric_field > 0 ...
138 ['field' => 'numeric_field', 'value' => 0, 'numeric' => TRUE, 'operator' => '>'],
142 // Other array elements at the top level of your table's array describe
143 // individual database table fields made available to Views. The array keys
144 // are the names (unique within the table) used by Views for the fields,
145 // usually equal to the database field names.
147 // Each field entry must have the following elements:
148 // - title: Translated label for the field in the UI.
149 // - help: Description of the field in the UI.
151 // Each field entry may also have one or more of the following elements,
152 // describing "handlers" (plugins) for the field:
153 // - relationship: Specifies a handler that allows this field to be used
154 // to define a relationship to another table in Views.
155 // - field: Specifies a handler to make it available to Views as a field.
156 // - filter: Specifies a handler to make it available to Views as a filter.
157 // - sort: Specifies a handler to make it available to Views as a sort.
158 // - argument: Specifies a handler to make it available to Views as an
159 // argument, or contextual filter as it is known in the UI.
160 // - area: Specifies a handler to make it available to Views to add content
161 // to the header, footer, or as no result behavior.
163 // Note that when specifying handlers, you must give the handler plugin ID
164 // and you may also specify overrides for various settings that make up the
165 // plugin definition. See examples below; the Boolean example demonstrates
166 // setting overrides.
168 // Node ID field, exposed as relationship only, since it is a foreign key
170 $data['example_table']['nid'] = [
171 'title' => t('Example content'),
172 'help' => t('Relate example content to the node content'),
174 // Define a relationship to the node_field_data table, so views whose
175 // base table is example_table can add a relationship to nodes. To make a
176 // relationship in the other direction, you can:
177 // - Use hook_views_data_alter() -- see the function body example on that
179 // - Use the implicit join method described above.
181 // Views name of the table to join to for the relationship.
182 'base' => 'node_field_data',
183 // Database field name in the other table to join on.
184 'base field' => 'nid',
185 // ID of relationship handler plugin to use.
187 // Default label for relationship in the UI.
188 'label' => t('Example node'),
192 // Plain text field, exposed as a field, sort, filter, and argument.
193 $data['example_table']['plain_text_field'] = [
194 'title' => t('Plain text field'),
195 'help' => t('Just a plain text field.'),
198 // ID of field handler plugin to use.
203 // ID of sort handler plugin to use.
208 // ID of filter handler plugin to use.
213 // ID of argument handler plugin to use.
218 // Numeric field, exposed as a field, sort, filter, and argument.
219 $data['example_table']['numeric_field'] = [
220 'title' => t('Numeric field'),
221 'help' => t('Just a numeric field.'),
224 // ID of field handler plugin to use.
229 // ID of sort handler plugin to use.
234 // ID of filter handler plugin to use.
239 // ID of argument handler plugin to use.
244 // Boolean field, exposed as a field, sort, and filter. The filter section
245 // illustrates overriding various settings.
246 $data['example_table']['boolean_field'] = [
247 'title' => t('Boolean field'),
248 'help' => t('Just an on/off field.'),
251 // ID of field handler plugin to use.
256 // ID of sort handler plugin to use.
261 // ID of filter handler plugin to use.
263 // Override the generic field title, so that the filter uses a different
265 'label' => t('Published'),
266 // Override the default BooleanOperator filter handler's 'type' setting,
267 // to display this as a "Yes/No" filter instead of a "True/False" filter.
269 // Override the default Boolean filter handler's 'use_equal' setting, to
270 // make the query use 'boolean_field = 1' instead of 'boolean_field <> 0'.
275 // Integer timestamp field, exposed as a field, sort, and filter.
276 $data['example_table']['timestamp_field'] = [
277 'title' => t('Timestamp field'),
278 'help' => t('Just a timestamp field.'),
281 // ID of field handler plugin to use.
286 // ID of sort handler plugin to use.
291 // ID of filter handler plugin to use.
296 // Area example. Areas are not generally associated with actual data
297 // tables and fields. This example is from views_views_data(), which defines
298 // the "Global" table (not really a table, but a group of Fields, Filters,
299 // etc. that are grouped into section "Global" in the UI). Here's the
300 // definition of the generic "Text area":
301 $data['views']['area'] = [
302 'title' => t('Text area'),
303 'help' => t('Provide markup text for the area.'),
305 // ID of the area handler plugin to use.