Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [PR-397](https://github.com/OS2Forms/os2forms/pull/397)
Cleaned up attachment element
- [PR-307](https://github.com/OS2Forms/os2forms/pull/307)
Added proper entity access checks on Maestro notification preview routes
- [#269](https://github.com/OS2Forms/os2forms/issues/269)
Expand Down
222 changes: 97 additions & 125 deletions modules/os2forms_attachment/src/Element/AttachmentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\os2forms_attachment\Element;

use Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_attachment\Element\WebformAttachmentBase;

Expand Down Expand Up @@ -31,102 +30,121 @@ public function getInfo() {
* {@inheritdoc}
*/
public static function getFileContent(array $element, WebformSubmissionInterface $webform_submission) {
$submissionUuid = $webform_submission->uuid();

// Override webform settings.
static::overrideWebformSettings($element, $webform_submission);
// Override some webform settings before rendering PDF.
$webform = $webform_submission->getWebform();
$originalWebformSettings = [
'submission_excluded_elements' => $webform->getSetting('submission_excluded_elements'),
'submission_exclude_empty' => $webform->getSetting('submission_exclude_empty'),
'submission_exclude_empty_checkbox' => $webform->getSetting('submission_exclude_empty_checkbox'),
];
// Override with settings from attachment element.
$newWebformSettings = [
'submission_excluded_elements' => $element['#excluded_elements'] ?? FALSE,
'submission_exclude_empty' => $element['#exclude_empty'] ?? FALSE,
'submission_exclude_empty_checkbox' => $element['#exclude_empty_checkbox'] ?? FALSE,
];
try {
foreach ($newWebformSettings as $key => $value) {
$webform->setSetting($key, $value);
}

/** @var \Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface $print_engine_manager */
$print_engine_manager = \Drupal::service('plugin.manager.entity_print.print_engine');
/** @var \Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface $print_engine_manager */
$print_engine_manager = \Drupal::service('plugin.manager.entity_print.print_engine');

/** @var \Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder $print_builder */
$print_builder = \Drupal::service('os2forms_attachment.print_builder');
/** @var \Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder $print_builder */
$print_builder = \Drupal::service('os2forms_attachment.print_builder');

// Make sure Webform Entity Print template is used.
// @see webform_entity_print_entity_view_alter()
\Drupal::request()->request->set('_webform_entity_print', TRUE);
// Make sure Webform Entity Print template is used.
// @see webform_entity_print_entity_view_alter()
\Drupal::request()->request->set('_webform_entity_print', TRUE);

// Set view mode or render custom twig.
// @see \Drupal\webform\WebformSubmissionViewBuilder::view
// @see webform_entity_print_attachment_webform_submission_view_alter()
$view_mode = $element['#view_mode'] ?? 'html';
if ($view_mode === 'twig') {
$webform_submission->_webform_view_mode_twig = $element['#template'];
}
\Drupal::request()->request->set('_webform_submissions_view_mode', $view_mode);

if ($element['#export_type'] === 'pdf') {
$file_path = NULL;

// Digital signature settings live on the digital signature handler.
// Resolving them here ensures all consumers of the element (email
// handlers, attachment downloads) serve an already signed document
// and render the validation text consistently.
// Webform skips handlers whose per-submission conditions do not match,
// so the same check is applied here to avoid rendering signature
// validation text on a document that was never signed.
$elementKey = $element['#webform_key'] ?? NULL;
if ($elementKey !== NULL) {
foreach ($webform_submission->getWebform()->getHandlers('os2forms_digital_signature') as $handler) {
$settings = $handler->getConfiguration()['settings'] ?? [];
if ($handler->isEnabled()
&& $handler->checkConditions($webform_submission)
&& ($settings['attachment_element'] ?? '') === $elementKey) {
$element['#digital_signature'] = TRUE;
$element['#digital_signature_position'] = $settings['signature_position']
?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT;
break;
// Set view mode or render custom twig.
// @see \Drupal\webform\WebformSubmissionViewBuilder::view
// @see webform_entity_print_attachment_webform_submission_view_alter()
$view_mode = $element['#view_mode'] ?? 'html';
if ($view_mode === 'twig') {
$webform_submission->_webform_view_mode_twig = $element['#template'];
}
\Drupal::request()->request->set('_webform_submissions_view_mode', $view_mode);

if ($element['#export_type'] === 'pdf') {
$file_path = NULL;

// Digital signature settings live on the digital signature handler.
// Resolving them here ensures all consumers of the element (email
// handlers, attachment downloads) serve an already signed document
// and render the validation text consistently.
// Webform skips handlers whose per-submission conditions do not match,
// so the same check is applied here to avoid rendering signature
// validation text on a document that was never signed.
$elementKey = $element['#webform_key'] ?? NULL;
if ($elementKey !== NULL) {
foreach ($webform_submission->getWebform()->getHandlers('os2forms_digital_signature') as $handler) {
$settings = $handler->getConfiguration()['settings'] ?? [];
if ($handler->isEnabled()
&& $handler->checkConditions($webform_submission)
&& ($settings['attachment_element'] ?? '') === $elementKey) {
$element['#digital_signature'] = TRUE;
$element['#digital_signature_position'] = $settings['signature_position']
?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT;
break;
}
}
}
}

// If attachment with digital signatur, check if we already have one.
if (isset($element['#digital_signature']) && $element['#digital_signature']) {
// Get scheme.
$scheme = 'private';
// If attachment with digital signatur, check if we already have one.
if (isset($element['#digital_signature']) && $element['#digital_signature']) {
// Get scheme.
$scheme = 'private';

// Get filename.
$file_name = 'webform/' . $webform_submission->getWebform()->id() . '/digital_signature/' . $submissionUuid . '.pdf';
$file_path = "$scheme://$file_name";
}
// Get filename.
$file_name = 'webform/' . $webform_submission->getWebform()->id() . '/digital_signature/' . $webform_submission->uuid() . '.pdf';
$file_path = "$scheme://$file_name";
}

if (!$file_path || !file_exists($file_path)) {
// Get scheme.
$scheme = 'temporary';
// Get filename.
$file_name = 'webform-entity-print-attachment--' . $webform_submission->getWebform()->id() . '-' . $webform_submission->id() . '.pdf';
if (!$file_path || !file_exists($file_path)) {
// Get scheme.
$scheme = 'temporary';
// Get filename.
$file_name = 'webform-entity-print-attachment--' . $webform_submission->getWebform()->id() . '-' . $webform_submission->id() . '.pdf';

// Save printable document.
$print_engine = $print_engine_manager->createSelectedInstance($element['#export_type']);
// Save printable document.
$print_engine = $print_engine_manager->createSelectedInstance($element['#export_type']);

// Adding digital signature.
if (isset($element['#digital_signature']) && $element['#digital_signature']) {
$signaturePosition = $element['#digital_signature_position'] ?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT;
$file_path = $print_builder->savePrintableDigitalSignature([$webform_submission], $print_engine, $scheme, $file_name, TRUE, $signaturePosition);
}
else {
$file_path = $print_builder->savePrintable([$webform_submission], $print_engine, $scheme, $file_name);
// Adding digital signature.
if (isset($element['#digital_signature']) && $element['#digital_signature']) {
$signaturePosition = $element['#digital_signature_position'] ?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT;
$file_path = $print_builder->savePrintableDigitalSignature([$webform_submission], $print_engine, $scheme, $file_name, TRUE, $signaturePosition);
}
else {
$file_path = $print_builder->savePrintable([$webform_submission], $print_engine, $scheme, $file_name);
}
}
}

if ($file_path) {
$contents = file_get_contents($file_path);
if ($file_path) {
$contents = file_get_contents($file_path);

// Deleting temporary file.
if ($scheme == 'temporary') {
\Drupal::service('file_system')->delete($file_path);
// Deleting temporary file.
if ($scheme == 'temporary') {
\Drupal::service('file_system')->delete($file_path);
}
}
else {
// Log error.
$context = ['@filename' => $file_name];
\Drupal::logger('webform_entity_print')->error("Unable to generate '@filename'.", $context);
$contents = '';
}
}
else {
// Log error.
$context = ['@filename' => $file_name];
\Drupal::logger('webform_entity_print')->error("Unable to generate '@filename'.", $context);
$contents = '';
// Save HTML document.
$contents = $print_builder->printHtml($webform_submission);
}
} finally {
// Restore webform settings.
foreach ($originalWebformSettings as $key => $value) {
$webform->setSetting($key, $value);
}
}
else {
// Save HTML document.
$contents = $print_builder->printHtml($webform_submission);
}

return $contents;
Expand All @@ -144,50 +162,4 @@ public static function getFileName(array $element, WebformSubmissionInterface $w
}
}

/**
* Overrides connected webform settings.
*
* Does that by creating a duplicate webform element with connected to a
* webform with the settings updated.
*
* @param array $element
* The webform attachment element.
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission
* A webform submission.
*/
public static function overrideWebformSettings(array $element, WebformSubmissionInterface &$webform_submission) {
$webform = $webform_submission->getWebform();

// Rewriting webform settings.
$webform->setSetting('submission_excluded_elements', $element['#excluded_elements'] ?? FALSE);
$webform->setSetting('submission_exclude_empty', $element['#exclude_empty'] ?? FALSE);
$webform->setSetting('submission_exclude_empty_checkbox', $element['#exclude_empty_checkbox'] ?? FALSE);

// Creating temporary submission to be able to swap original webform
// settings.
$webform_submission_temp = WebformSubmission::create([
'webform' => $webform,
'entity_type' => $webform_submission->getEntityTypeId(),
'entity_id' => $webform_submission->id(),
'data' => $webform_submission->getData(),
]);
// Clone ids.
$webform_submission_temp->set('serial', $webform_submission->get('serial')->value);
$webform_submission_temp->set('token', $webform_submission->get('token')->value);
// Clone states.
$webform_submission_temp->set('in_draft', $webform_submission->get('in_draft')->value);
$webform_submission_temp->set('current_page', $webform_submission->get('current_page')->value);
// Clone timestamps.
$webform_submission_temp->set('created', $webform_submission->get('created')->value);
$webform_submission_temp->set('changed', $webform_submission->get('changed')->value);
$webform_submission_temp->set('completed', $webform_submission->get('completed')->value);
// Clone admin notes, sticky, and locked.
$webform_submission_temp->set('notes', $webform_submission->get('notes')->value);
$webform_submission_temp->set('sticky', $webform_submission->get('sticky')->value);
$webform_submission_temp->set('sticky', $webform_submission->get('locked')->value);

// Finalize cloning: swap the webform submission.
$webform_submission = $webform_submission_temp;
}

}
Loading