Skip to content

Commit c4b327e

Browse files
authored
Merge pull request #8 from itk-dev/feature/add-submission-exists-filter
Added filter for existing submissions
2 parents 8ba210e + b8380dc commit c4b327e

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

os2forms_failed_jobs.module

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ function os2forms_failed_jobs_views_data(): array {
5353
'title' => t('Submission serial id'),
5454
'field' => 'id',
5555
'id' => 'advancedqueue_job_submission_serial',
56+
'allow empty' => TRUE,
57+
],
58+
];
59+
60+
$data['advancedqueue']['webform_submission_serial_exists'] = [
61+
'title' => t('Webform submission serial exists filter'),
62+
'help' => t('Provides a filter for checking if webform submission exists from job'),
63+
'filter' => [
64+
'title' => t('Submission serial exists'),
65+
'field' => 'id',
66+
'id' => 'advancedqueue_job_submission_exists',
67+
'allow empty' => TRUE,
5668
],
5769
];
5870

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

src/Plugin/views/filter/SubmissionSerialFilter.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ public function query(): void {
5858
/** @var \Drupal\views\Plugin\views\query\Sql $query */
5959
$query = $this->query;
6060
$table = array_key_first($query->tables);
61+
6162
$input = $this->value;
6263
$webform = $this->routeMatch->getParameter('webform');
6364

64-
$jobs = $this->helper->getQueueJobIdsFromSerial($input, $webform->get('id'));
65+
if ($webform) {
66+
$jobs = $this->helper->getQueueJobIdsFromSerial($input, $webform->get('id'));
67+
if (empty($jobs)) {
68+
// The 'IN' operator requires a non empty array.
69+
$jobs = [0];
70+
}
6571

66-
if (empty($jobs)) {
67-
// The 'IN' operator requires a non empty array.
68-
$jobs = [0];
72+
$query->addWhere($this->options['group'], $table . '.job_id', $jobs, 'IN');
6973
}
70-
71-
$query->addWhere($this->options['group'], $table . '.job_id', $jobs, 'IN');
7274
}
7375

7476
}

0 commit comments

Comments
 (0)