You are here

public function RecentlyReadRelationship::query in Recently Read 8

Add anything to the query that we might need to.

Overrides RelationshipPluginBase::query

File

src/Plugin/views/relationship/RecentlyReadRelationship.php, line 138

Class

RecentlyReadRelationship
Provides a views relationship to recently read.

Namespace

Drupal\recently_read\Plugin\views\relationship

Code

public function query() {
  parent::query();
  $this
    ->ensureMyTable();

  // Get base table and entity_type from relationship.
  $basetable = $this->definition['base_table'];
  $entity_type = $this->definition['recently_read_type'];

  // Add query for selected entity type.
  if ($this->options['required']) {
    $this->query
      ->addWhere('recently_read', "recently_read_{$basetable}.type", $entity_type, "=");
  }

  // Add query to filter data if auth.user or anonymous.
  if ($this->currentUser
    ->id() === 0) {

    // Disable page caching for anonymous users.
    $this->killSwitch
      ->trigger();
    $this->query
      ->addWhere('recently_read', "recently_read_{$basetable}.session_id", $this->sessionManager
      ->getId(), "=");
  }
  elseif ($this->options['required']) {
    $this->query
      ->addWhere('recently_read', "recently_read_{$basetable}.user_id", $this->currentUser
      ->id(), "=");
  }

  // Filter by entity bundles selected while configuring the relationship.
  if (!empty(array_filter($this->options['bundles']))) {
    $this->query
      ->addWhere('recently_read', "{$basetable}.bundle", array_filter(array_values($this->options['bundles'])), "IN");
  }
}