diff --git a/views_example/views_example.info.yml b/views_example/views_example.info.yml new file mode 100644 index 0000000..abdccfe --- /dev/null +++ b/views_example/views_example.info.yml @@ -0,0 +1,9 @@ +name: Views Example Module +description: Code examples for views module. +package: Example modules +type: module +version: 1.0 +core: 8.x +dependencies: + - views + - examples \ No newline at end of file diff --git a/views_example/views_example.install b/views_example/views_example.install new file mode 100644 index 0000000..91b684d --- /dev/null +++ b/views_example/views_example.install @@ -0,0 +1,55 @@ + 'The base table for views example.', + 'fields' => array( + 'nid' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE + ), + 'string' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '' + ), + 'number' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0 + ), + 'boolean' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0 + ), + 'timestamp' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0 + ), + 'serialized' => array( + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + ), + ), + 'foreign keys' => array( + 'node' => array( + 'table' => 'node', + 'columns' => array('nid' => 'nid'), + ), + ), + 'primary key' => array('nid'), + ); + + return $schema; +} diff --git a/views_example/views_example.views.inc b/views_example/views_example.views.inc new file mode 100644 index 0000000..6aac3cd --- /dev/null +++ b/views_example/views_example.views.inc @@ -0,0 +1,220 @@ + 'nid', + // Label in the UI. + 'title' => t('Views example table'), + // Longer description in the UI. Required. + 'help' => t('Example table contains example content and can be related to nodes.'), + 'weight' => -10, + ); + + // Some tables have an implicit, automatic relationship to other tables, + // meaning that when the other table is available in a view (either as the + // base table or through a relationship), this table's fields, filters, etc. + // are automatically made available without having to add an additional + // relationship. To define an implicit relationship that will make your + // table automatically available when another table is present, add a 'join' + // section to your 'table' section. Note that it is usually only a good idea + // to do this for one-to-one joins, because otherwise your automatic join + // will add more rows to the view. It is also not a good idea to do this if + // most views won't need your table -- if that is the case, define a + // relationship instead (see the field section below). + // + // If you've decided an automatic join is a good idea, here's how to do it: + $data['views_example_table']['table']['join'] = array( + // Within the 'join' section, list one or more tables to automatically + // join to. In this example, every time 'node' is available in a view, + // 'views_example_table' will be too. The array keys here are the array keys + // for the other tables, given in their hook_views_data() implementations. + // If the table listed here is from another module's hook_views_data() + // implementation, make sure your module depends on that other module. + 'node' => array( + // Primary key field in node to use in the join. + 'left_field' => 'nid', + // Foreign key field in views_example_table to use in the join. + 'field' => 'nid', + ), + ); + + // Other array elements at the top level of your table's array describe + // individual database table fields made available to Views. The array keys + // are the names (unique within the table) used by Views for the fields, + // usually equal to the database field names. + // + // Each field entry must have the following elements: + // - title: Translated label for the field in the UI. + // - help: Description of the field in the UI. + // + // Each field entry may also have one or more of the following elements, + // describing "handlers" (plugins) for the field: + // - relationship: Specifies a handler that allows this field to be used + // to define a relationship to another table in Views. + // - field: Specifies a handler to make it available to Views as a field. + // - filter: Specifies a handler to make it available to Views as a filter. + // - sort: Specifies a handler to make it available to Views as a sort. + // - argument: Specifies a handler to make it available to Views as an + // argument, or contextual filter as it is known in the UI. + // - area: Specifies a handler to make it available to Views to add content + // to the header, footer, or as no result behavior. + // + // Note that when specifying handlers, you must give the handler plugin ID + // and you may also specify overrides for various settings that make up the + // plugin definition. See examples below; the Boolean example demonstrates + // setting overrides. + + // Node ID field, exposed as relationship only, since it is a foreign key + // in this table. + $data['views_example_table']['nid'] = array( + 'title' => t('Node'), + 'help' => t('Relate views_example_table content to the node content'), + + // Define a relationship to the node table, so views whose base table is + // views_example_table can add a relationship to the node table. To make a + // relationship in the other direction, you can: + // - Use hook_views_data_alter() -- see the function body example on that + // hook for details. + // - Use the implicit join method described above. + 'relationship' => array( + // Views name of the table to join to for the relationship. + 'base' => 'node', + // Database field name in the other table to join on. + 'base field' => 'nid', + // ID of relationship handler plugin to use. + 'id' => 'standard', + // Default label for relationship in the UI. + 'label' => t('Example node'), + ), + ); + + // Plain text field, exposed as a field, sort, filter, and argument. + $data['views_example_table']['string'] = array( + 'title' => t('String'), + 'help' => t('Simple string field.'), + + 'field' => array( + // ID of field handler plugin to use. + 'id' => 'standard', + ), + + 'sort' => array( + // ID of sort handler plugin to use. + 'id' => 'standard', + ), + + 'filter' => array( + // ID of filter handler plugin to use. + 'id' => 'string', + ), + + 'argument' => array( + // ID of argument handler plugin to use. + 'id' => 'string', + ), + ); + + // Numeric field, exposed as a field, sort, filter, and argument. + $data['views_example_table']['number'] = array( + 'title' => t('Number'), + 'help' => t('Just a numeric field.'), + + 'field' => array( + // ID of field handler plugin to use. + 'id' => 'numeric', + ), + + 'sort' => array( + // ID of sort handler plugin to use. + 'id' => 'standard', + ), + + 'filter' => array( + // ID of filter handler plugin to use. + 'id' => 'numeric', + ), + + 'argument' => array( + // ID of argument handler plugin to use. + 'id' => 'numeric', + ), + ); + + // Boolean field, exposed as a field, sort, and filter. The filter section + // illustrates overriding various settings. + $data['views_example_table']['boolean'] = array( + 'title' => t('Boolean'), + 'help' => t('Just an on/off field.'), + + 'field' => array( + // ID of field handler plugin to use. + 'id' => 'boolean', + ), + + 'sort' => array( + // ID of sort handler plugin to use. + 'id' => 'standard', + ), + + 'filter' => array( + // ID of filter handler plugin to use. + 'id' => 'boolean', + // Override the generic field title, so that the filter uses a different + // label in the UI. + 'label' => t('Published'), + // Override the default BooleanOperator filter handler's 'type' setting, + // to display this as a "Yes/No" filter instead of a "True/False" filter. + 'type' => 'yes-no', + // Override the default Boolean filter handler's 'use_equal' setting, to + // make the query use 'boolean_field = 1' instead of 'boolean_field <> 0'. + 'use_equal' => TRUE, + ), + ); + + // Integer timestamp field, exposed as a field, sort, and filter. + $data['views_example_table']['timestamp'] = array( + 'title' => t('Timestamp'), + 'help' => t('Just a timestamp field.'), + + 'field' => array( + // ID of field handler plugin to use. + 'id' => 'date', + ), + + 'sort' => array( + // ID of sort handler plugin to use. + 'id' => 'date', + ), + + 'filter' => array( + // ID of filter handler plugin to use. + 'id' => 'date', + ), + ); + + return $data; +}