Commit db0104f4 authored by alexpott's avatar alexpott

Issue #1817672 by jibran, dawehner, bdgreen, damiankloip: Replace db_select()...

Issue #1817672 by jibran, dawehner, bdgreen, damiankloip: Replace db_select() with an injected database connection in views plugins.
parent 2a17837f
......@@ -7,8 +7,10 @@
namespace Drupal\comment\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler to accept a user id to check for nodes that
......@@ -20,15 +22,44 @@
*/
class UserUid extends ArgumentPluginBase {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
function title() {
if (!$this->argument) {
$title = config('user.settings')->get('anonymous');
}
else {
$query = db_select('users', 'u');
$query->addField('u', 'name');
$query->condition('u.uid', $this->argument);
$title = $query->execute()->fetchField();
$title = $this->database->query('SELECT u.name FROM {users} u WHERE u.uid = :uid', array(':uid' => $this->argument))->fetchField();
}
if (empty($title)) {
return t('No user');
......@@ -54,7 +85,7 @@ protected function defaultActions($which = NULL) {
public function query($group_by = FALSE) {
$this->ensureMyTable();
$subselect = db_select('comment', 'c');
$subselect = $this->database->select('comment', 'c');
$subselect->addField('c', 'cid');
$subselect->condition('c.uid', $this->argument);
$subselect->where("c.nid = $this->tableAlias.nid");
......
......@@ -7,10 +7,12 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\field\Numeric;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Component\Annotation\PluginID;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field handler to display the number of new comments.
......@@ -21,6 +23,38 @@
*/
class NodeNewComments extends Numeric {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
/**
* Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
*/
......@@ -76,16 +110,15 @@ function pre_render(&$values) {
}
if ($nids) {
$query = db_select('node', 'n');
$query->addField('n', 'nid');
$query->innerJoin('comment', 'c', 'n.nid = c.nid');
$query->addExpression('COUNT(c.cid)', 'num_comments');
$query->leftJoin('history', 'h', 'h.nid = n.nid');
$query->condition('n.nid', $nids);
$query->where('c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp)', array(':timestamp' => HISTORY_READ_LIMIT));
$query->condition('c.status', COMMENT_PUBLISHED);
$query->groupBy('n.nid');
$result = $query->execute();
$result = $this->database->query('SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment} c ON n.nid = c.nid
LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN (:nids)
AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp) AND c.status = :status GROUP BY n.nid', array(
':status' => COMMENT_PUBLISHED,
':h_uid' => $user->uid,
':nids' => $nids,
':timestamp' => HISTORY_READ_LIMIT,
));
foreach ($result as $node) {
foreach ($ids[$node->nid] as $id) {
$values[$id]->{$this->field_alias} = $node->num_comments;
......
......@@ -7,8 +7,10 @@
namespace Drupal\node\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\Numeric;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\argument\Numeric;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler to accept a node revision id.
......@@ -17,7 +19,37 @@
*/
class Vid extends Numeric {
// No constructor is necessary.
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
/**
* Override the behavior of title(). Get the title of the revision.
......@@ -25,11 +57,7 @@ class Vid extends Numeric {
public function titleQuery() {
$titles = array();
$results = db_select('node_field_revision', 'npr')
->fields('npr', array('vid', 'nid', 'title'))
->condition('npr.vid', $this->value)
->execute()
->fetchAllAssoc('vid', PDO::FETCH_ASSOC);
$results = $this->database->query('SELECT npr.vid, npr.nid, npr.title FROM {node_field_revision} npr WHERE npr.vid IN (:vids)', array(':vids' => $this->value))->fetchAllAssoc('vid', PDO::FETCH_ASSOC);
$nids = array();
foreach ($results as $result) {
$nids[] = $result['nid'];
......
......@@ -9,7 +9,9 @@
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Validate whether an argument is a valid user.
......@@ -26,6 +28,38 @@
*/
class User extends ArgumentValidatorPluginBase {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['type'] = array('default' => 'uid');
......@@ -104,7 +138,7 @@ public function validateArgument($argument) {
}
if (!isset($account)) {
$account = db_select('users', 'u')
$account = $this->database->select('users', 'u')
->fields('u', array('uid', 'name'))
->condition($condition, $argument)
->execute()
......@@ -120,12 +154,8 @@ public function validateArgument($argument) {
$roles = $this->options['roles'];
$account->roles = array();
$account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
$query = db_select('users_roles', 'u');
$query->addField('u', 'rid');
$query->condition('u.uid', $account->uid);
$result = $query->execute();
foreach ($result as $role) {
$account->roles[] = $role->rid;
foreach ($account->getRoles() as $rid) {
$account->roles[] = $rid;
}
if (!(bool) array_intersect($account->roles, $roles)) {
return FALSE;
......
......@@ -8,9 +8,11 @@
namespace Drupal\user\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\field\PrerenderList;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field handler to provide a list of permissions.
......@@ -21,6 +23,38 @@
*/
class Permissions extends PrerenderList {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
/**
* Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
*/
......@@ -55,14 +89,8 @@ function pre_render(&$values) {
$permissions = module_invoke_all('permission');
$query = db_select('role_permission', 'rp');
$query->join('users_roles', 'u', 'u.rid = rp.rid');
$query->fields('u', array('uid', 'rid'));
$query->addField('rp', 'permission');
$query->condition('u.uid', $uids);
$query->condition('rp.module', array_keys($modules));
$query->orderBy('rp.permission');
$result = $query->execute();
$result = $this->database->query('SELECT u.uid, u.rid, rp.permission FROM {role_permission} rp INNER JOIN {users_roles} u ON u.rid = rp.rid WHERE u.uid IN (:uids) AND rp.module IN (:modules) ORDER BY rp.permission',
array(':uids' => $uids, ':modules' => array_keys($modules)));
foreach ($result as $perm) {
$this->items[$perm->uid][$perm->permission]['permission'] = $permissions[$perm->permission]['title'];
......
......@@ -8,9 +8,11 @@
namespace Drupal\user\Plugin\views\field;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Database\Connection;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\field\PrerenderList;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field handler to provide a list of roles.
......@@ -21,6 +23,38 @@
*/
class Roles extends PrerenderList {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
}
/**
* Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
*/
......@@ -45,12 +79,7 @@ function pre_render(&$values) {
if ($uids) {
$roles = user_roles();
$query = db_select('users_roles', 'u');
$query->fields('u', array('uid', 'rid'));
$query->condition('u.rid', array_keys($roles));
$query->condition('u.uid', $uids);
$result = $query->execute();
$result = $this->database->query('SELECT u.uid, u.rid FROM {users_roles} u WHERE u.uid IN (:uids) AND u.rid IN (:rids)', array(':uids' => $uids, ':rids' => array_keys($roles)));
foreach ($result as $role) {
$this->items[$role->uid][$role->rid]['role'] = check_plain($roles[$role->rid]->label());
$this->items[$role->uid][$role->rid]['rid'] = $role->rid;
......
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