Commit 9e0a3231 authored by catch's avatar catch

Issue #1829734 by dawehner, damiankloip, cam8001, dww: Expose tracker data to views.

parent c33191b6
<?php
/**
* @file
* Contains \Drupal\tracker\Plugin\views\argument\UserUid
*/
namespace Drupal\tracker\Plugin\views\argument;
use Drupal\Component\Annotation\PluginID;
use Drupal\comment\Plugin\views\argument\UserUid as CommentUserUid;
/**
* UID argument to check for nodes that user posted or commented on.
*
* @ingroup views_argument_handlers
*
* @PluginID("tracker_user_uid")
*/
class UserUid extends CommentUserUid {
/**
* {@inheritdoc}
*/
public function query($group_by = FALSE) {
// Because this handler thinks it's an argument for a field on the {node}
// table, we need to make sure {tracker_user} is JOINed and use its alias
// for the WHERE clause.
$tracker_user_alias = $this->query->ensure_table('tracker_user');
$this->query->add_where(0, "$tracker_user_alias.uid", $this->argument);
}
}
<?php
/**
* @file
* Contains \Drupal\tracker\Plugin\views\filter\UserUid.
*/
namespace Drupal\tracker\Plugin\views\filter;
use Drupal\Component\Annotation\PluginID;
use Drupal\user\Plugin\views\filter\Name;
/**
* UID filter to check for nodes that a user posted or commented on.
*
* @ingroup views_filter_handlers
*
* @PluginID("tracker_user_uid")
*/
class UserUid extends Name {
/**
* {@inheritdoc}
*/
public function query() {
// Because this handler thinks it's an argument for a field on the {node}
// table, we need to make sure {tracker_user} is JOINed and use its alias
// for the WHERE clause.
$tracker_user_alias = $this->query->ensure_table('tracker_user');
$this->query->add_where(0, "$tracker_user_alias.uid", $this->value);
}
}
<?php
/**
* @file
* Contains \Drupal\tracker\Tests\Views\TrackerTestBase.
*/
namespace Drupal\tracker\Tests\Views;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Base class for all tracker tests.
*/
abstract class TrackerTestBase extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment', 'tracker', 'tracker_test_views');
protected function setUp() {
parent::setUp();
ViewTestData::importTestViews(get_class($this), array('tracker_test_views'));
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
$permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval');
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
$this->node = $this->drupalCreateNode(array(
'title' => $this->randomName(8),
'uid' => $account->id(),
'status' => 1,
));
$this->comment = entity_create('comment', array(
'nid' => $this->node->id(),
'subject' => $this->randomName(),
'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this->randomName(20),
));
}
}
<?php
/**
* @file
* Contains \Drupal\tracker\Tests\Views\TrackerUserUidTest.
*/
namespace Drupal\tracker\Tests\Views;
/**
* Tests the tracker user uid handlers.
*/
class TrackerUserUidTest extends TrackerTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_tracker_user_uid');
public static function getInfo() {
return array(
'name' => 'Tracker: User UID tests',
'description' => 'Tests the tracker comment user uid handlers.',
'group' => 'Views module integration',
);
}
/**
* Tests the user uid filter and argument.
*/
public function testUserUid() {
$map = array(
'nid' => 'nid',
'node_title' => 'title',
);
$expected = array(
array(
'nid' => $this->node->id(),
'title' => $this->node->label(),
)
);
$view = views_get_view('test_tracker_user_uid');
$this->executeView($view);
// We should have no results as the filter is set for uid 0.
$this->assertIdenticalResultSet($view, array(), $map);
$view->destroy();
// Change the filter value to our user.
$view->initHandlers();
$view->filter['uid_touch_tracker']->value = $this->node->uid;
$this->executeView($view);
// We should have one result as the filter is set for the created user.
$this->assertIdenticalResultSet($view, $expected, $map);
$view->destroy();
// Remove the filter now, so only the argument will affect the query.
$view->removeItem('default', 'filter', 'uid_touch_tracker');
// Test the incorrect argument UID.
$view->initHandlers();
$this->executeView($view, array(rand()));
$this->assertIdenticalResultSet($view, array(), $map);
$view->destroy();
// Test the correct argument UID.
$view->initHandlers();
$this->executeView($view, array($this->node->uid));
$this->assertIdenticalResultSet($view, $expected, $map);
}
}
base_field: nid
base_table: node
core: 8.x
description: ''
status: '1'
display:
default:
display_plugin: default
id: default
display_title: Master
position: ''
display_options:
access:
type: perm
cache:
type: none
query:
type: views_query
exposed_form:
type: basic
pager:
type: full
style:
type: table
options:
grouping: { }
row_class: ''
default_row_class: '1'
row_class_special: '1'
override: '1'
sticky: '0'
summary: ''
columns:
title: title
info:
title:
sortable: '1'
default_sort_order: asc
align: ''
separator: ''
empty_column: '0'
responsive: ''
default: '-1'
empty_table: '0'
row:
type: fields
fields:
title:
id: title
table: node
field: title
relationship: none
group_type: group
admin_label: ''
label: Title
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '0'
ellipsis: '0'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
link_to_node: '1'
filters:
uid_touch_tracker:
id: uid_touch_tracker
table: node
field: uid_touch_tracker
relationship: none
group_type: group
admin_label: ''
operator: in
value:
- '0'
group: '1'
exposed: '0'
expose:
operator_id: ''
label: 'User posted or commented'
description: ''
use_operator: '0'
operator: uid_touch_tracker_op
identifier: uid_touch_tracker
required: '0'
remember: '0'
multiple: '0'
remember_roles:
authenticated: authenticated
anonymous: '0'
administrator: '0'
reduce: '0'
is_grouped: '0'
group_info:
label: ''
description: ''
identifier: ''
optional: '1'
widget: select
multiple: '0'
remember: '0'
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: tracker_user_uid
arguments:
uid_touch_tracker:
id: uid_touch_tracker
table: node
field: uid_touch_tracker
relationship: none
group_type: group
admin_label: ''
default_action: ignore
exception:
value: all
title_enable: '0'
title: All
title_enable: '0'
title: ''
breadcrumb_enable: '0'
breadcrumb: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: '0'
summary_options:
base_path: ''
count: '1'
items_per_page: '25'
override: '0'
summary:
sort_order: asc
number_of_records: '0'
format: default_summary
specify_validation: '0'
validate:
type: none
fail: 'not found'
validate_options: { }
plugin_id: tracker_user_uid
human_name: 'tracker test'
module: views
id: test_tracker_user_uid
tag: ''
langcode: en
name: 'Tracker test views'
description: 'Provides default views for views tracker tests.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- tracker
- views
hidden: true
<?php
/**
* @file
* Provide views data for tracker.module.
*
* @ingroup views_module_handlers
*/
/**
* Implements hook_views_data().
*/
function tracker_views_data() {
$data = array();
$data['tracker_node']['table']['group'] = t('Tracker');
$data['tracker_node']['table']['join'] = array(
'node' => array(
'type' => 'INNER',
'left_field' => 'nid',
'field' => 'nid',
),
);
$data['tracker_node']['nid'] = array(
'title' => t('Nid'),
'help' => t('The node ID of the node.'),
'field' => array(
'id' => 'node',
),
'argument' => array(
'id' => 'node_nid',
'name field' => 'title',
'numeric' => TRUE,
'validate type' => 'nid',
),
'filter' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['tracker_node']['published'] = array(
'title' => t('Published'),
'help' => t('Whether or not the node is published.'),
'field' => array(
'id' => 'boolean',
),
'filter' => array(
'id' => 'boolean',
'label' => t('Published'),
'type' => 'yes-no',
'accept null' => TRUE,
'use_equal' => TRUE,
),
'sort' => array(
'id' => 'standard',
),
);
$data['tracker_node']['changed'] = array(
'title' => t('Updated date'),
'help' => t('The date the node was last updated.'),
'field' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
);
$data['tracker_user']['table']['group'] = t('Tracker - User');
$data['tracker_user']['table']['join'] = array(
'node' => array(
'type' => 'INNER',
'left_field' => 'nid',
'field' => 'nid',
),
'user' => array(
'type' => 'INNER',
'left_field' => 'uid',
'field' => 'uid',
),
);
$data['tracker_user']['nid'] = array(
'title' => t('Nid'),
'help' => t('The node ID of the node a user created or commented on. You must use an argument or filter on UID or you will get misleading results using this field.'),
'field' => array(
'id' => 'node',
),
'argument' => array(
'id' => 'node_nid',
'name field' => 'title',
'numeric' => TRUE,
'validate type' => 'nid',
),
'filter' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['tracker_user']['uid'] = array(
'title' => t('Uid'),
'help' => t('The user ID of a user who touched the node (either created or commented on it).'),
'field' => array(
'id' => 'user_name',
),
'argument' => array(
'id' => 'user_uid',
'name field' => 'name',
),
'filter' => array(
'title' => t('Name'),
'id' => 'user_name',
),
'sort' => array(
'id' => 'standard',
),
);
$data['tracker_user']['published'] = array(
'title' => t('Published'),
'help' => t('Whether or not the node is published. You must use an argument or filter on UID or you will get misleading results using this field.'),
'field' => array(
'id' => 'boolean',
),
'filter' => array(
'id' => 'boolean',
'label' => t('Published'),
'type' => 'yes-no',
'accept null' => TRUE,
'use_equal' => TRUE,
),
'sort' => array(
'id' => 'standard',
),
);
$data['tracker_user']['changed'] = array(
'title' => t('Updated date'),
'help' => t('The date the node was last updated or commented on. You must use an argument or filter on UID or you will get misleading results using this field.'),
'field' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
);
return $data;
}
/**
* Implements hook_views_data_alter().
*/
function tracker_views_data_alter(&$data) {
// Provide additional uid_touch handlers which are handled by tracker
$data['node']['uid_touch_tracker'] = array(
'group' => t('Tracker - User'),
'title' => t('User posted or commented'),
'help' => t('Display nodes only if a user posted the node or commented on the node.'),
'argument' => array(
'field' => 'uid',
'name table' => 'users',
'name field' => 'name',
'id' => 'tracker_user_uid',
'no group by' => TRUE,
),
'filter' => array(
'field' => 'uid',
'name table' => 'users',
'name field' => 'name',
'id' => 'tracker_user_uid'
),
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment