As stated at the beginning of the chapter, Views is a visual query builder. When you first create a view, a base table is specified to pull data from. Views automatically knows how to join tables for field data, such as body text or custom attached fields.
When using an entity reference field, you have the ability to display the value as the raw identifier, the referenced entity's label, or the entire rendered entity. However, if you add a Relationship based on a reference field you will have access to display any of that entity's available fields.
In this recipe, we will update the Files view, used for administering files, to display the username of the user who uploaded the file.
Drupal stores data in a normalized format. Database normalization, in short, involves the organization of data in specific related tables. Each entity type has its own database table and all fields have their own database table. When you create a view and specify what kind of data will be shown, you are specifying a base table in the database that Views will query. Views will automatically associate fields that belong to the entity and the relationship to those tables for you.
When an entity has an Entity reference field you have the ability to add a relationship to the referenced entity type's table. This is an explicit definition, whereas fields are implicit. When the relationship is explicitly defined all of the referenced entity type's fields come into scope. The fields on the referenced entity type can then be displayed, filtered, and sorted by.
Using relationships in Views allows you to create some powerful displays. We will discuss aggregation and additional information about relationships.
Views uses a series of hooks to retrieve data that it uses to represent ways to interact with the database. One of these is the hook_field_views_data
hook, which processes a field storage configuration entity and registers its data with Views. The Views module implements this on behalf of Drupal core to add relationships, and reverse relationships, for Entity reference fields.
Since Entity reference fields have set schema information, Views can dynamically generate these relationships by knowing the field's table name, destination entity's table name, and the destination entity's identifier column.
There are times where you would need to define a relation on your own with custom code. Typically, when working with custom data in Drupal, you would more than likely create a new entity type, covered in Chapter 9, Confiuration Management – Deploying in Drupal 8. This is not always the case, however, and you may just need a simple method of storing data. An example can be found in the Database Logging module. The Database Logging module defines schema for a database table and then uses hook_views_data
to expose its database table to Views.
The dblog_schema
hook implementation returns a uid
column on the watchdog database table created by the module. That column is then exposed to Views with the following definition:
$data['watchdog']['uid'] = array( 'title' => t('UID'), 'help' => t('The user ID of the user on which the log entry was written..'), 'field' => array( 'id' => 'numeric', ), 'filter' => array( 'id' => 'numeric', ), 'argument' => array( 'id' => 'numeric', ), 'relationship' => array( 'title' => t('User'), 'help' => t('The user on which the log entry as written.'), 'base' => 'users', 'base field' => 'uid', 'id' => 'standard', ), );
This array tells Views that the watchdog
table has a column named uid
. It is numeric
in nature for its display, filtering capabilities and sorting capabilities. The relationship
key is an array of information that instructs Views how to use this to provide a relationship (LEFT JOIN)
on the users
table. The User
entity uses the users
table and has the primary key of uid
.
There is a view setting under the Advanced section that allows you to enable aggregation. This feature allows you to enable the usage of SQL aggregate functions, such as MIN, MAX, SUM, AVG, and COUNT. In this recipe, the Files view uses aggregation to SUM the usage counts of each file in the Drupal site.
Aggregation settings are set for each field and when enabled have their own link to configure the settings.