Problem/Motivation

Implementing node access through the $query->addTag('node_access') method requires queries that do not use {node} as base table to explicitly define the base table through $query->addMetaData('base_table', table).

At the moment, while it allows modules to add the node_access tag via 'query access tag' in hook_views_data(), views does not allow passing query metadata via that same hook. This is holding up some of the list-to-views conversion issues such as #2020387: Convert "Active forum topics" block to a View, since the query node_access tag on a non-node base table causes exceptions:

Exception: Query tagged for node access but there is no node table, specify the base_table using meta data. in node_query_node_access_alter() (line 1867 of /d8/core/modules/node/node.module).

For background, see

Proposed resolution

Allow modules to pass query metadata along with the 'query access tag' in hook_views_data().

Here's how this would look for the forum module:

/**
 * Implements hook_views_data().
 */
function forum_views_data() {

  $data['forum_index']['table']['group'] = t('Forum');
  $data['forum_index']['table']['base'] = array(
    'field' => 'nid',
    'title' => t('Forum content'),
    'access query tag' => 'node_access',
    'query metadata' => array('base_table' => 'forum_index'),
  );
  ...

Remaining tasks

User interface changes

None.

API changes

One more key (for example 'query metadata') added to the hook_views_data return value.

Comments

dawehner’s picture

Issue summary: View changes
Issue tags: +VDC
StatusFileSize
new3.83 KB
new3.68 KB

Wow, I was not aware of that functionality. Thanks for working on that patch!

I decided to try to write a proper test for it, and really got confused about the fact that they did not passed ... You missed some curly braces.

damiankloip’s picture

  1. +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/SqlQueryTest.php
    @@ -0,0 +1,104 @@
    +    $this->doTestExecuteMetadata();
    ...
    +  public function testExecute() {
    ...
    +  }
    

    Sorry, but why?

  2. +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/SqlQueryTest.php
    @@ -0,0 +1,104 @@
    +      $this->assertEqual($query->getMetaData('key1'), 'test_metadata');
    +      $this->assertEqual($query->getMetaData('key2'), 'test_metadata2');
    

    might as well assert identical it?

dawehner’s picture

StatusFileSize
new843 bytes
new3.84 KB

Sorry, but why?

There is a lot of additional stuff to test in the execute method in the future.

dawehner’s picture

StatusFileSize
new3.71 KB
new738 bytes
damiankloip’s picture

Status: Needs review » Reviewed & tested by the community

Thank you! This looks good.

xjm’s picture

4: vdc-2099511.patch queued for re-testing.

dawehner’s picture

Priority: Normal » Major

.

webchick’s picture

Title: Need support for adding metadata in hook_views_data(). » Change notice: Need support for adding metadata in hook_views_data().
Category: Bug report » Task
Status: Reviewed & tested by the community » Active
Issue tags: +Needs change record

Committed and pushed to 8.x. Thanks!

We'll need a change notice to explain which modules need to use this and why/how.

xjm’s picture

Project: Drupal core » Views (for Drupal 7)
Version: 8.x-dev » 8.x-3.x-dev
Component: views.module » Code
Priority: Major » Normal

Sending over to Views-land.

chris matthews’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.7.x-dev
Component: Code » views.module
Assigned: mr.baileys » Unassigned

For more information as to why this issue was moved to the Drupal core project, please see issue #3030347: Plan to clean process issue queue

chris matthews’s picture

Project: Drupal core » Views (for Drupal 7)
Version: 8.7.x-dev » 7.x-3.x-dev
Component: views.module » Code
Status: Active » Closed (outdated)

Moving back to the contributed Views issue queue and closing as outdated per https://www.drupal.org/project/views/issues/3030347#comment-13023447