diff --git a/fillpdf.api.php b/fillpdf.api.php new file mode 100644 index 0000000000000000000000000000000000000000..a4319d0567fab975e1da18f80c498ab2cd633591 --- /dev/null +++ b/fillpdf.api.php @@ -0,0 +1,46 @@ +<?php + +/** + * @file + * Hooks related to FillPDF module. + */ + +/** + * @addtogroup hooks + * @{ + */ + +/** + * Alter the $context in HandlePdfController::populatePdf(). + * + * @param array $context + * The context array with keys 'fid', 'force_download', 'flatten', 'sample', + * and 'entity_ids'. + */ +function hook_fillpdf_populate_pdf_context_alter(array &$context): void { + // If there are no webform_submission entities but there is at least one + // webform entity, add the most recent submission for each webform. + // Only do this for authenticated users and when webform_submission storage + // exists. + $current_uid = (int) \Drupal::currentUser()->id(); + if ($current_uid && empty($context['entity_ids']['webform_submission']) && !empty($context['entity_ids']['webform']) && \Drupal::entityTypeManager()->hasDefinition('webform_submission')) { + $webform_submission_storage = \Drupal::entityTypeManager()->getStorage('webform_submission'); + foreach ($context['entity_ids']['webform'] as $webform_id) { + // Load submission IDs from webform_submission storage. + $query = $webform_submission_storage->getQuery()->condition('webform_id', $webform_id); + $query->condition('uid', $uid); + $query->condition('in_draft', 0); + $query->sort('created', 'ASC'); + $entity_id = $query->execute(); + // If there is at least one, return the last as integer, otherwise NULL. + $entity_id = $entity_id ? (int) end($entity_id) : NULL; + if ($entity_id) { + $context['entity_ids']['webform_submission'][$entity_id] = $entity_id; + } + } + } +} + +/** + * @} End of "addtogroup hooks". + */