You are here

views_test_query_access.module in Drupal 8

Module to test entity query access in Views.

File

core/modules/views/tests/modules/views_test_query_access/views_test_query_access.module
View source
<?php

/**
 * @file
 * Module to test entity query access in Views.
 */
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Entity\Sql\DefaultTableMapping;

/**
 * Implements hook_query_TAG_alter() for the 'media_access' query tag.
 */
function views_test_query_access_query_media_access_alter(AlterableInterface $query) {
  _views_test_query_access_restrict_by_uuid($query);
}

/**
 * Implements hook_query_TAG_alter() for the 'block_content_access' query tag.
 */
function views_test_query_access_query_block_content_access_alter(AlterableInterface $query) {
  _views_test_query_access_restrict_by_uuid($query);
}

/**
 * Excludes entities with the 'hidden-ENTITY_TYPE_ID' UUID from a query.
 *
 * @param \Drupal\Core\Database\Query\AlterableInterface $query
 *   The Views select query to alter.
 */
function _views_test_query_access_restrict_by_uuid(AlterableInterface $query) {
  if (!$query instanceof SelectInterface) {
    return;
  }

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $query
    ->getMetaData('view');
  $entity_type = $view
    ->getBaseEntityType();
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type
    ->id());
  if (!$storage instanceof SqlEntityStorageInterface) {
    return;
  }
  $table_mapping = $storage
    ->getTableMapping();
  if (!$table_mapping instanceof DefaultTableMapping) {
    return;
  }
  $base_table = $table_mapping
    ->getBaseTable();
  $data_table = $table_mapping
    ->getDataTable();

  // We are excluding entities by UUID, which means we need to be certain the
  // base table is joined in the query.
  $tables = $query
    ->getTables();
  if (isset($tables[$data_table]) && !isset($tables[$base_table])) {
    $data_table_alias = $tables[$data_table]['alias'];
    $id_key = $entity_type
      ->getKey('id');
    $base_table = $query
      ->innerJoin($base_table, NULL, "{$data_table_alias}.{$id_key} = {$base_table}.{$id_key}");
  }

  // Figure out the column name of the UUID field and add a condition on that.
  $base_field_definitions = \Drupal::service('entity_field.manager')
    ->getBaseFieldDefinitions($entity_type
    ->id());
  $uuid_key = $entity_type
    ->getKey('uuid');
  $uuid_column_name = $table_mapping
    ->getFieldColumnName($base_field_definitions[$uuid_key], NULL);
  $query
    ->condition("{$base_table}.{$uuid_column_name}", 'hidden-' . $entity_type
    ->id(), '<>');
}

Functions

Namesort descending Description
views_test_query_access_query_block_content_access_alter Implements hook_query_TAG_alter() for the 'block_content_access' query tag.
views_test_query_access_query_media_access_alter Implements hook_query_TAG_alter() for the 'media_access' query tag.
_views_test_query_access_restrict_by_uuid Excludes entities with the 'hidden-ENTITY_TYPE_ID' UUID from a query.