22
33namespace Drupal \os2forms_forloeb ;
44use Drupal \Core \Entity \EntityTypeManagerInterface ;
5+ use Drupal \Core \StringTranslation \StringTranslationTrait ;
56use Drupal \maestro \Engine \MaestroEngine ;
7+ use Drupal \maestro \Entity \MaestroEntityIdentifiers ;
8+ use Drupal \maestro \Entity \MaestroProcess ;
69use Drupal \webform \Entity \WebformSubmission ;
10+ use Psr \Log \LoggerInterface ;
711
812/**
913 * Class ForloebTaskConsole.
1014 */
1115class ForloebTaskConsole {
1216
17+ use StringTranslationTrait;
18+
1319 /**
1420 * Drupal\Core\Entity\EntityTypeManagerInterface definition.
1521 *
1622 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
1723 */
1824 protected $ entityTypeManager ;
1925
26+ /**
27+ * Logger.
28+ *
29+ * @var \Psr\Log\LoggerInterface
30+ */
31+ protected $ logger ;
32+
2033 /**
2134 * Constructs a new ForloebTaskConsole object.
2235 */
23- public function __construct (EntityTypeManagerInterface $ entity_type_manager ) {
36+ public function __construct (EntityTypeManagerInterface $ entity_type_manager, LoggerInterface $ logger ) {
2437 $ this ->entityTypeManager = $ entity_type_manager ;
38+ $ this ->logger = $ logger ;
2539 }
2640
41+
2742 /**
2843 * Gets MaestroQueue record by webforms submission token.
2944 *
@@ -36,31 +51,32 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) {
3651 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
3752 */
3853 public function getQueueIdByWebformSubmissionToken ($ token = '' ) {
39- $ engine = new MaestroEngine ();
40- // Fetch the user's queue items.
41- $ queueIDs = $ engine ->getAssignedTaskQueueIds (\Drupal::currentUser ()->id ());
54+ /** @var WebformSubmission $webform_submission */
55+ $ webform_submissions = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->loadByProperties (['token ' => $ token ]);
4256
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+ if (empty ($ webform_submissions )) {
58+ $ this ->logger ->warning ($ this ->t ('Submission with token @token not found ' , ['@token ' => $ token ]));
59+ return NULL ;
60+ }
5761
58- // Compare webform submission with token from request.
59- if ($ webform_submission && $ webform_submission ->getToken () == $ token ) {
60- return $ queueRecord ;
61- }
62+ $ webform_submission = reset ($ webform_submissions );
63+ /** @var MaestroEntityIdentifiers $maestro_entity_identifier */
64+ $ maestro_entity_identifiers = $ this ->entityTypeManager ->getStorage ('maestro_entity_identifiers ' )->loadByProperties (['entity_type ' => 'webform_submission ' , 'entity_id ' => $ webform_submission ->id (),]);
65+ $ maestro_entity_identifier = reset ($ maestro_entity_identifiers );
66+ $ processIDs = $ maestro_entity_identifier ->process_id ->referencedEntities ();
67+ if (empty ($ processIDs )) {
68+ $ this ->logger ->warning ($ this ->t ('Process with entity type: webform_submission and entity_id: @entity_id not found ' , ['@entity_id ' => $ webform_submission ->id ()]));
69+ return NULL ;
6270 }
6371
64- return NULL ;
72+ $ processID = reset ($ processIDs );
73+ $ maestro_queues = $ this ->entityTypeManager ->getStorage ('maestro_queue ' )->loadByProperties (['process_id ' => $ processID ->id (), 'task_class_name ' => 'MaestroWebformInherit ' ,]);
74+ if (empty ($ maestro_queues )) {
75+ $ this ->logger ->warning ($ this ->t ('Maestro queue with task_class_name: MaestroWebformInherit and process_id: @process_id not found ' , ['@process_id ' => $ processID ->id ()]));
76+ return NULL ;
77+ }
78+ $ maestro_queue = reset ($ maestro_queues );
79+ return $ maestro_queue ;
6580 }
81+
6682}
0 commit comments