Adding a relationship in a View

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.

How to do it…

  1. Visit Structure and then Views. This will bring you to the administrative overview of all the views that have been created
  2. Find the Files view and click Edit.
  3. Click on Advanced to expand the section and then click Add next to Relationships.
  4. Search for user. Select the User who uploaded relationship option and click Apply (this display).
    How to do it…
  5. Next we will be presented with a configure form for the relationship. Click Apply (this display) to use the defaults.
  6. Add a new field by clicking Add in the Fields section.
  7. Search for name and select the Name field and click Apply (this display).
  8. This view uses aggregation, which presents a new configuration form when first adding a field. Click Apply and continue to use the defaults.

    Note

    We will discuss Views and aggregation in the There's more… section.

  9. We will use the default field settings that will provide the label Name and format it as the username and link to the user's profile. Click Apply (all displays).
    How to do it…
  10. Click on Save to finish editing the view and commit your changes.

How it works…

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.

There's more…

Using relationships in Views allows you to create some powerful displays. We will discuss aggregation and additional information about relationships.

Relationships provided by entity reference fields

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.

Relationships provided through custom code

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.

Using Aggregation and views.

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.

Using Aggregation and views.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset