View source
<?php
namespace Drupal\social_activity_filter\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ActivityFilterTags extends FilterPluginBase {
protected $database;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'));
}
public function canExpose() {
return FALSE;
}
public function query() {
$tags = isset($this->view->filter_tags) ? $this->view->filter_tags : '';
$taxonomy_field = isset($this->view->filter_vocabulary) ? $this->view->filter_vocabulary : '';
$or = new Condition('OR');
$and_wrapper = new Condition('AND');
$taxonomy_node_table = "node__{$taxonomy_field}";
$taxonomy_post_table = "post__field_{$taxonomy_field}";
$activity_entity_table = 'activity__field_activity_entity';
if ($this->database
->schema()
->tableExists($taxonomy_node_table)) {
$this->query
->addTable($taxonomy_node_table);
$this->query
->addTable($activity_entity_table);
$configuration = [
'left_table' => $activity_entity_table,
'left_field' => 'field_activity_entity_target_id',
'table' => $taxonomy_node_table,
'field' => 'entity_id',
'operator' => '=',
'extra' => [
0 => [
'left_field' => 'field_activity_entity_target_type',
'value' => 'node',
],
],
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$this->query
->addRelationship('filtered_nodes', $join, $taxonomy_node_table);
$and_node_wrapper = new Condition('AND');
$and_node_wrapper
->condition("filtered_nodes.{$taxonomy_field}_target_id", $tags, 'IN');
$or
->condition($and_node_wrapper);
$comment_table = 'comment_field_data';
$this->query
->addTable($comment_table);
$configuration = [
'left_table' => $activity_entity_table,
'left_field' => 'field_activity_entity_target_id',
'table' => $comment_table,
'field' => 'cid',
'operator' => '=',
'extra' => [
0 => [
'left_field' => 'field_activity_entity_target_type',
'value' => 'comment',
],
],
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$this->query
->addRelationship($comment_table, $join, $comment_table);
$and_comment_wrapper = new Condition('AND');
$and_comment_wrapper
->condition("{$comment_table}.comment_type", 'comment');
$configuration = [
'left_table' => $comment_table,
'left_field' => 'entity_id',
'table' => $taxonomy_node_table,
'field' => 'entity_id',
'operator' => '=',
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$this->query
->addRelationship('commented_nodes', $join, $comment_table);
$and_comment_wrapper
->condition("commented_nodes.{$taxonomy_field}_target_id", $tags, 'IN');
$or
->condition($and_comment_wrapper);
}
if ($this->database
->schema()
->tableExists($taxonomy_post_table)) {
$configuration = [
'table' => $taxonomy_post_table,
'field' => 'entity_id',
'left_table' => $activity_entity_table,
'left_field' => 'field_activity_entity_target_id',
'operator' => '=',
'extra' => [
0 => [
'left_field' => 'field_activity_entity_target_type',
'value' => 'post',
],
],
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$this->query
->addRelationship('filtered_posts', $join, $taxonomy_post_table);
$and_post_wrapper = new Condition('AND');
$and_post_wrapper
->condition("filtered_posts.field_{$taxonomy_field}_target_id", $tags, 'IN');
$or
->condition($and_post_wrapper);
}
if ($or
->count()) {
$and_wrapper
->condition($or);
$this->query
->addWhere('tags', $and_wrapper);
}
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'user';
return $contexts;
}
}