DomainAccessCurrentAllFilter.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Drupal\domain_access\Plugin\views\filter;
  3. use Drupal\views\Plugin\views\filter\BooleanOperator;
  4. use Drupal\views\Plugin\views\display\DisplayPluginBase;
  5. use Drupal\views\ViewExecutable;
  6. /**
  7. * Handles matching of current domain.
  8. *
  9. * @ingroup views_filter_handlers
  10. *
  11. * @ViewsFilter("domain_access_current_all_filter")
  12. */
  13. class DomainAccessCurrentAllFilter extends BooleanOperator {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  18. parent::init($view, $display, $options);
  19. $this->value_value = t('Available on current domain');
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getValueOptions() {
  25. $this->valueOptions = [1 => $this->t('Yes'), 0 => $this->t('No')];
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function operators() {
  31. return [];
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function query() {
  37. $this->ensureMyTable();
  38. // @TODO: Proper abstraction of table and field name.
  39. $all_table = $this->query->ensureTable('node__field_domain_all_affiliates');
  40. $current_domain = \Drupal::service('domain.negotiator')->getActiveId();
  41. if (empty($this->value)) {
  42. // @TODO proper handling of NULL?
  43. $where = "$this->tableAlias.$this->realField <> '$current_domain'";
  44. $where = '(' . $where . " OR $this->tableAlias.$this->realField IS NULL)";
  45. $where = '(' . $where . " AND ($all_table.field_domain_all_affiliates_value = 0 OR $all_table.field_domain_all_affiliates_value IS NULL))";
  46. }
  47. else {
  48. $where = "($this->tableAlias.$this->realField = '$current_domain' OR $all_table.field_domain_all_affiliates_value = 1)";
  49. }
  50. $this->query->addWhereExpression($this->options['group'], $where);
  51. // This filter causes duplicates.
  52. $this->query->options['distinct'] = TRUE;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getCacheContexts() {
  58. $contexts = parent::getCacheContexts();
  59. $contexts[] = 'url.site';
  60. return $contexts;
  61. }
  62. }