Make a View with K2!

Tips and tricks for making views in Drupal 8

https://kristin-dev.github.io/view-with-k2-presentation/index.html

Hook 42

hook42.com & @hook42inc

  • SEO, Multilingual, Migrations
  • Complex Enterprise Drupal projects
  • We like Douglas Adams

Me - K2

Kristin Bradham & @K2Hook42

  • Site Builder / Front End Developer
  • Been doing web stuff forever (not telling anymore how long)
  • Drupal for (almost) 5 years

You?

  • Developer?
  • Site Builder?
  • New to Drupal?

What we're going to do

  • What's new in D8
  • Get the major concepts solid
  • Make some views
  • Advanced Features
  • Tips and Tricks

New in Drupal 8

Entities

  • Many more things are entities
  • Taxonomy terms are entities
  • More things work the same way as content in D7

Blocks

Views Log Entries

  • Watchdog log entries
  • For things like broken links
  • Create a view of log entries

Note: Have to have database logging module enabled (often gets turned off in production, this deletes the view!)

Views Settings

Let's setup Views for success.

Setup tips for debugging

  • Go to Views Settings
  • Turn on Show SQL query
  • Turn on Show performance Statistics

Most of us have made a basic view

Show [view_type (entity)] of type [types_of_this_entity] tagged with: [core_tags] sorted by: [views_sort]

We have a deadline... let's do this quick!

Screenshot of basic view making

Screenshot of basic view

But what are we really building and why?

Lists of Data, displayed in different ways.

Things get complicated really fast.

View Types

  • View types are not just content, they can be any entity, and a few non-entity extras
  • Make sure you choose the right view type, so that your relationships work later on
  • Contrib modules can add additional view types

Filters

  • This is the large set of data.
  • Start by getting this correct.
  • Then you can get more sophisticated with display, sorting, contextual filters and relationships.

Sorts

The order we want to see things in. This is pretty straight forward, but there's more here...

Sorts and Exposed Filters

This is where sorts get powerful. We can set up complex sorts that the user can manipulate.

Adding an exposed filter

  • Create View
  • Add Filters
  • Select "Expose to visitors"

Basic view with exposed sorts (filters)

Change the view filter names

Example of the view with Filters

Advanced Options for Views - Exposed Forms

Advanced Options for Views - Exposed Forms Options

Adding tabs to views

Contextual Filters

Filters from somewhere else (not on this content)

Where regular filters are a broad stroke, Contextual Filters are details.

Screenshot of Contextual Filters

Basic Contextual Filter Options:

  1. What the filter should do if there isn't a value in the URL
  2. What the filter should do if there is a value, (and therefore include any default values from the first section)
  3. Some other options that are independent of how the value gets set

Some Options Explained:

  • Content ID from URL: 333 from '/node/333'
  • Taxonomy Term ID: 444 from '/taxonomy/term/444'
  • User ID from logged in user: 555 from '/user/555' (note: anonymous is 0)
  • User ID from route context: User ID from D8 Symphony, can also look at author ID

More Options Explained:

  • Raw value from the URL: each slash is an argument
    So: argument '2' would be 'structure' from '/admin/structure/types'
  • Query parameters: each query is an argument
    So: argument 'harry' would be '777' from /admin/structure/types/?harry=777&sally=888
  • Fixed: any 'hardcoded' value (not flexible, I just want to say the value right here)

Relationships

Links two entities together

If there is an entity reference (this includes taxonomy terms) there can be a relationship.

Without the relationship ALL the view can display is the ID of the entity that is referenced by the Entity ID.

Diagram of Relationships

One to many, many to one

MAKE SURE YOU GO THE RIGHT WAY
or you’ll take a performance hit.

Content referenced from field_job_company
this is what you want.

Content using field_job_company
this is the reverse relationship.

Stretch

Please stand up

and chat with your neighbor

ask them where they grew up.

Let's Make Some Views!

Examples of contextual filters and relationships

A view with a contextual filter

Adding a relationship

Wrong way to do contextual filters

Advanced Features

Descriptions of Advanced Features

  • Machine name: Add to target with code (a default will be added).
  • Administrative comment: Just add a note.
  • Use Ajax: See next slide.
  • Hide attachments in summaries: relates to attachment view type (kind of a corner case).

Advanced Features - AJAX

  • Use Ajax: YES!
  • Do this if you have exposed filters, or dynamic views.
  • Options such as paging, table sorting and exposed filters will not need a page refresh!
  • Use this unless it breaks things (more than one view on a page may break the AJAX).

More Descriptions of Advanced Features

  • Use Aggregation & Query Settings: change the fundamentals of views and how SQL queries are generated. Use if you have complex SQL problem.
  • Administrative comment: just add a note
  • Caching: see next slide
  • CSS class: Add a class to the view wrapper.

Advanced Features - Caching

  • Caching: YES!
  • Time based: Wait for some period of time, then rerun query. You might not see updates right away. (This is the old way from D7 and before - legacy option!)
  • Tag based: Default and D8 way! D8 puts tags everywhere that let keep track of what is related to what. When one tag changes, D8 keeps track of other changes that also might need to be made.

Tips and Tricks

Tips and Tricks 1

Skip the UI and use code

Tips and Tricks 2

Use Existing Views provided in Core

For example: Enable Archive View

The idea for this slide came from: https://www.webwash.net/build-a-blog-in-drupal-8-using-views/

Tips and Tricks 3

Use Taxonomy Terms to expand content quickly

Tips and Tricks 4

Use Different View Modes to make the same view look different quickly

Tips and Tricks 5

Use Additional Modules

Other Things to Consider

  • Name things well - so you can find them later
  • Do you really need a view for this?
  • Performance

Naming Matters

  • Naming a ton of views
  • Entity > Descriptor > Modifier
  • Biggest to smallest
  • Add Tags
  • Either all in one, or one for all

So long and thanks for all the fish!

Questions? Answers@hook42.com