PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/user/src/Plugin/views/filter/Current.php

http://github.com/drupal/drupal
PHP | 58 lines | 32 code | 12 blank | 14 comment | 3 complexity | a43160370161e86e9b6496af17dfd7a2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\user\Plugin\views\filter;
  3. use Drupal\Core\Database\Query\Condition;
  4. use Drupal\views\Plugin\views\display\DisplayPluginBase;
  5. use Drupal\views\ViewExecutable;
  6. use Drupal\views\Plugin\views\filter\BooleanOperator;
  7. /**
  8. * Filter handler for the current user.
  9. *
  10. * @ingroup views_filter_handlers
  11. *
  12. * @ViewsFilter("user_current")
  13. */
  14. class Current extends BooleanOperator {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  19. parent::init($view, $display, $options);
  20. $this->value_value = $this->t('Is the logged in user');
  21. }
  22. public function query() {
  23. $this->ensureMyTable();
  24. $field = $this->tableAlias . '.' . $this->realField . ' ';
  25. $or = new Condition('OR');
  26. if (empty($this->value)) {
  27. $or->condition($field, '***CURRENT_USER***', '<>');
  28. if ($this->accept_null) {
  29. $or->isNull($field);
  30. }
  31. }
  32. else {
  33. $or->condition($field, '***CURRENT_USER***', '=');
  34. }
  35. $this->query->addWhere($this->options['group'], $or);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getCacheContexts() {
  41. $contexts = parent::getCacheContexts();
  42. // This filter depends on the current user.
  43. $contexts[] = 'user';
  44. return $contexts;
  45. }
  46. }