|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_failed_jobs\Plugin\views\filter; |
| 4 | + |
| 5 | +use Drupal\Core\Database\Connection; |
| 6 | +use Drupal\Core\Routing\RouteMatchInterface; |
| 7 | +use Drupal\os2forms_failed_jobs\Helper\Helper; |
| 8 | +use Drupal\views\Plugin\views\filter\StringFilter; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Filter by submission exists. |
| 13 | + * |
| 14 | + * @ingroup views_filter_handlers |
| 15 | + * |
| 16 | + * @ViewsFilter("advancedqueue_job_submission_exists") |
| 17 | + */ |
| 18 | +final class SubmissionExistsFilter extends StringFilter { |
| 19 | + |
| 20 | + /** |
| 21 | + * Class constructor. |
| 22 | + * |
| 23 | + * @phpstan-param array<string, mixed> $configuration |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + $configuration, |
| 27 | + $plugin_id, |
| 28 | + $plugin_definition, |
| 29 | + Connection $connection, |
| 30 | + protected Helper $helper, |
| 31 | + protected RouteMatchInterface $routeMatch |
| 32 | + ) { |
| 33 | + parent::__construct($configuration, $plugin_id, $plugin_definition, $connection); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + * |
| 39 | + * @phpstan-param array<string, mixed> $configuration |
| 40 | + */ |
| 41 | + public static function create(ContainerInterface $container, $configuration, $plugin_id, $plugin_definition) { |
| 42 | + return new static( |
| 43 | + $configuration, |
| 44 | + $plugin_id, |
| 45 | + $plugin_definition, |
| 46 | + $container->get('database'), |
| 47 | + $container->get(Helper::class), |
| 48 | + $container->get('current_route_match'), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + public function query(): void { |
| 56 | + $this->ensureMyTable(); |
| 57 | + |
| 58 | + /** @var \Drupal\views\Plugin\views\query\Sql $query */ |
| 59 | + $query = $this->query; |
| 60 | + $table = array_key_first($query->tables); |
| 61 | + |
| 62 | + $webform = $this->routeMatch->getParameter('webform'); |
| 63 | + |
| 64 | + if ($webform) { |
| 65 | + $jobs = []; |
| 66 | + $jobIds = $this->helper->getQueueJobIds($webform->get('id')); |
| 67 | + foreach ($jobIds as $job) { |
| 68 | + if ($this->helper->getSubmissionSerialIdFromJob($job) > 0) { |
| 69 | + $jobs[] = $job; |
| 70 | + } |
| 71 | + } |
| 72 | + if (empty($jobs)) { |
| 73 | + // The 'IN' operator requires a non empty array. |
| 74 | + $jobs = [0]; |
| 75 | + } |
| 76 | + |
| 77 | + $query->addWhere($this->options['group'], $table . '.job_id', $jobs, 'IN'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments