function ViewsTestQueryAccessHooks::restrictByUuid

Excludes entities with the 'hidden-ENTITY_TYPE_ID' UUID from a query.

Parameters

\Drupal\Core\Database\Query\AlterableInterface $query: The Views select query to alter.

2 calls to ViewsTestQueryAccessHooks::restrictByUuid()
ViewsTestQueryAccessHooks::queryBlockContentAccessAlter in core/modules/views/tests/modules/views_test_query_access/src/Hook/ViewsTestQueryAccessHooks.php
Implements hook_query_TAG_alter() for the 'block_content_access' query tag.
ViewsTestQueryAccessHooks::queryMediaAccessAlter in core/modules/views/tests/modules/views_test_query_access/src/Hook/ViewsTestQueryAccessHooks.php
Implements hook_query_TAG_alter() for the 'media_access' query tag.

File

core/modules/views/tests/modules/views_test_query_access/src/Hook/ViewsTestQueryAccessHooks.php, line 40

Class

ViewsTestQueryAccessHooks
Hook implementations for views_test_query_access.

Namespace

Drupal\views_test_query_access\Hook

Code

public function restrictByUuid(AlterableInterface $query) : void {
  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(), '<>');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.