Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit ff334c6

Browse files
authored
Task A: Implemented redirect route for task console to get better UX for users (#130)
* Implemented redirect route for task console to get better UX for users * Fixed condition to define correct queue id * Adjusted route with os2forms-forloeb namespace * Refactored controller method with early return
1 parent a005009 commit ff334c6

4 files changed

Lines changed: 218 additions & 0 deletions

File tree

os2forms_forloeb.routing.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
os2forms_forloeb.forloeb_task_console_controller_execute:
2+
path: 'os2forms-forloeb/execute-task'
3+
defaults:
4+
_controller: '\Drupal\os2forms_forloeb\Controller\ForloebTaskConsoleController::execute'
5+
_title: 'Execute task'
6+
requirements:
7+
_permission: 'view maestro task console'
8+
options:
9+
no_cache: TRUE

os2forms_forloeb.services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
logger.channel.os2forms_forloeb:
3+
parent: logger.channel_base
4+
arguments: ['os2forms_forloeb']
5+
os2forms_forloeb.task_console:
6+
class: Drupal\os2forms_forloeb\ForloebTaskConsole
7+
arguments: ['@entity_type.manager']
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_forloeb\Controller;
4+
5+
use Drupal\Component\Utility\UrlHelper;
6+
use Drupal\Core\Controller\ControllerBase;
7+
use Drupal\Core\Entity\EntityTypeManagerInterface;
8+
use Drupal\Core\Url;
9+
use Drupal\maestro\Engine\MaestroEngine;
10+
use Drupal\maestro\Utility\TaskHandler;
11+
use Drupal\os2forms_forloeb\ForloebTaskConsole;
12+
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use Symfony\Component\HttpFoundation\RedirectResponse;
15+
16+
/**
17+
* Class ForloebTaskConsoleController.
18+
*/
19+
class ForloebTaskConsoleController extends ControllerBase {
20+
21+
/**
22+
* Update manager service.
23+
*
24+
* @var \Drupal\os2forms_forloeb\ForloebTaskConsole
25+
*/
26+
protected $forloebTaskConsole;
27+
28+
/**
29+
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
30+
*
31+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
32+
*/
33+
protected $entityTypeManager;
34+
35+
/**
36+
* Constructs update status data.
37+
*
38+
* @param \Drupal\os2forms_forloeb\ForloebTaskConsole $forloebTaskConsole
39+
* Forloeb task console Service.
40+
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
41+
* The Entity type manager.
42+
*/
43+
public function __construct(ForloebTaskConsole $forloeb_task_console, EntityTypeManagerInterface $entity_type_manager) {
44+
$this->forloebTaskConsole = $forloeb_task_console;
45+
$this->entityTypeManager = $entity_type_manager;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public static function create(ContainerInterface $container) {
52+
return new static(
53+
$container->get('os2forms_forloeb.task_console'),
54+
$container->get('entity_type.manager')
55+
);
56+
}
57+
58+
/**
59+
* Redirects to the task execution URL.
60+
*
61+
* In case it's not possible to define task, redirects to task console.
62+
*
63+
* @return RedirectResponse
64+
* Redirect object.
65+
*/
66+
public function execute() {
67+
$redirect_to = Url::fromRoute('maestro_taskconsole.taskconsole');
68+
69+
// Check webform submission token.
70+
$token = \Drupal::request()->query->get('token', '');
71+
if ($token) {
72+
$queueRecord = $this->forloebTaskConsole->getQueueIdByWebformSubmissionToken($token);
73+
}
74+
else {
75+
// For empty token there is user last task from taskconsole queue.
76+
$queueIDs = MaestroEngine::getAssignedTaskQueueIds(\Drupal::currentUser()->id());
77+
$queueRecord = count($queueIDs) ? $this->entityTypeManager->getStorage('maestro_queue')->load(end($queueIDs)) : NULL;
78+
79+
// In case there are more than 1 task warning message will be shown.
80+
if (count($queueIDs) > 1) {
81+
$this->messenger()->addWarning($this->t('You have @amount @tasks available for you. See list of the all tasks on <a href=":tasksonsole">taskconsole</a>', [
82+
':tasksonsole' => Url::fromRoute('maestro_taskconsole.taskconsole')->toString(),
83+
'@amount' => count($queueIDs),
84+
'@tasks' => new PluralTranslatableMarkup(count($queueIDs), 'task','tasks'),
85+
]));
86+
}
87+
}
88+
89+
if (empty($queueRecord)) {
90+
$this->messenger()->addWarning($this->t('No tasks found to execute.'));
91+
return new RedirectResponse($redirect_to->toString());
92+
}
93+
94+
// Processing QueueRecord to get execution URL to redirect to.
95+
$handler = $queueRecord->handler->getString();
96+
$query_options = [
97+
'queueid' => $queueRecord->id(),
98+
'modal' => 'notmodal'
99+
];
100+
101+
// As inspiration MaestroTaskConsoleController::getTasks() method was used.
102+
if ($handler && !empty($handler) && $queueRecord->is_interactive->getString() == '1') {
103+
global $base_url;
104+
$handler = str_replace($base_url, '', $handler);
105+
$handler_type = TaskHandler::getType($handler);
106+
107+
$handler_url_parts = UrlHelper::parse($handler);
108+
$query_options += $handler_url_parts['query'];
109+
110+
}
111+
elseif ($queueRecord->is_interactive->getString() == '1' && empty($handler)) {
112+
// Handler is empty. If this is an interactive task and has no handler, we're still OK. This is an interactive function that uses a default handler then.
113+
$handler_type = 'function';
114+
}
115+
else {
116+
$this->messenger()->addWarning($this->t('Undefined handler'));
117+
}
118+
119+
switch ($handler_type) {
120+
case 'external':
121+
$redirect_to = Url::fromUri($handler, ['query' => $query_options]);
122+
break;
123+
124+
case 'internal':
125+
$redirect_to = Url::fromUserInput($handler, ['query' => $query_options]);
126+
break;
127+
128+
case 'function':
129+
$redirect_to = Url::fromRoute('maestro.execute', $query_options);
130+
break;
131+
}
132+
133+
return new RedirectResponse($redirect_to->toString());
134+
}
135+
136+
}

src/ForloebTaskConsole.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_forloeb;
4+
use Drupal\Core\Entity\EntityTypeManagerInterface;
5+
use Drupal\maestro\Engine\MaestroEngine;
6+
use Drupal\webform\Entity\WebformSubmission;
7+
8+
/**
9+
* Class ForloebTaskConsole.
10+
*/
11+
class ForloebTaskConsole {
12+
13+
/**
14+
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
15+
*
16+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
17+
*/
18+
protected $entityTypeManager;
19+
20+
/**
21+
* Constructs a new ForloebTaskConsole object.
22+
*/
23+
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
24+
$this->entityTypeManager = $entity_type_manager;
25+
}
26+
27+
/**
28+
* Gets MaestroQueue record by webforms submission token.
29+
*
30+
* @param string $token
31+
*
32+
* @return \Drupal\maestro\Entity\MaestroQueue|null
33+
* Returns MaestroQueue entity or NULL
34+
*
35+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
36+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
37+
*/
38+
public function getQueueIdByWebformSubmissionToken($token = '') {
39+
$engine = new MaestroEngine();
40+
// Fetch the user's queue items.
41+
$queueIDs = $engine->getAssignedTaskQueueIds(\Drupal::currentUser()->id());
42+
43+
foreach ($queueIDs as $queueID) {
44+
$this->entityTypeManager->getStorage('maestro_queue')->resetCache([$queueID]);
45+
/** @var \Drupal\maestro\Entity\MaestroQueue $queueRecord */
46+
$queueRecord = $this->entityTypeManager->getStorage('maestro_queue')->load($queueID);
47+
$processID = $engine->getProcessIdFromQueueId($queueID);
48+
$templateMachineName = $engine->getTemplateIdFromProcessId($processID);
49+
50+
// Get user input from 'inherit_webform_unique_id'
51+
$taskMachineName = $engine->getTaskIdFromQueueId($queueID);
52+
$task = $engine->getTemplateTaskByID($templateMachineName, $taskMachineName);
53+
54+
// Load its corresponding webform submission.
55+
$sid = $engine->getEntityIdentiferByUniqueID($processID, $task['data']['inherit_webform_unique_id'] ?? '');
56+
$webform_submission = $sid ? WebformSubmission::load($sid) : NULL;
57+
58+
// Compare webform submission with token from request.
59+
if ($webform_submission && $webform_submission->getToken() == $token) {
60+
return $queueRecord;
61+
}
62+
}
63+
64+
return NULL;
65+
}
66+
}

0 commit comments

Comments
 (0)