class ViewsTestQueryAccessHooks

Hook implementations for views_test_query_access.

Hierarchy

Expanded class hierarchy of ViewsTestQueryAccessHooks

File

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

Namespace

Drupal\views_test_query_access\Hook
View source
class ViewsTestQueryAccessHooks {
    
    /**
     * Implements hook_query_TAG_alter() for the 'media_access' query tag.
     */
    public function queryMediaAccessAlter(AlterableInterface $query) : void {
        $this->restrictByUuid($query);
    }
    
    /**
     * Implements hook_query_TAG_alter() for the 'block_content_access' query tag.
     */
    public function queryBlockContentAccessAlter(AlterableInterface $query) : void {
        $this->restrictByUuid($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.
     */
    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(), '<>');
    }

}

Members

Title Sort descending Modifiers Object type Summary
ViewsTestQueryAccessHooks::queryBlockContentAccessAlter public function Implements hook_query_TAG_alter() for the &#039;block_content_access&#039; query tag.
ViewsTestQueryAccessHooks::queryMediaAccessAlter public function Implements hook_query_TAG_alter() for the &#039;media_access&#039; query tag.
ViewsTestQueryAccessHooks::restrictByUuid public function Excludes entities with the &#039;hidden-ENTITY_TYPE_ID&#039; UUID from a query.

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