-
Notifications
You must be signed in to change notification settings - Fork 3
288: Add address element to generated html during digital post #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jekuaitk
wants to merge
7
commits into
OS2Forms:develop
Choose a base branch
from
itk-dev:feature/address-in-digital-post-pdfs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d8224a
288: Add address element to generated html during digital post
jekuaitk ba26c19
288: Updated CHANGELOG
jekuaitk e0815dc
288: Added comment to preg_replace
jekuaitk 1fca0a2
288: Ensured required fields are set before accessing them
jekuaitk d25ba3e
288: Switched to h-card and saving lookup result in session
jekuaitk cfdd08f
288: Updated README with injected html and example styling
jekuaitk e3a719a
288: Cleaned up example styling
jekuaitk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+41.1 KB
modules/os2forms_digital_post/docs/digst_a4_farve_ej_til_kant_demo_ny_rudeplacering.pdf
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
modules/os2forms_digital_post/src/EventSubscriber/Os2formsDigitalPostSubscriber.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\os2forms_digital_post\EventSubscriber; | ||
|
|
||
| use Drupal\entity_print\Event\PrintEvents; | ||
| use Drupal\entity_print\Event\PrintHtmlAlterEvent; | ||
| use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult; | ||
| use Drupal\os2web_datalookup\LookupResult\CprLookupResult; | ||
| use Drupal\webform\WebformSubmissionInterface; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
|
|
||
| /** | ||
| * Used to alter the generated PDF to align with digital post requirements. | ||
| */ | ||
| final class Os2formsDigitalPostSubscriber implements EventSubscriberInterface { | ||
|
|
||
| public function __construct(private readonly SessionInterface $session) { | ||
| } | ||
|
|
||
| /** | ||
| * Post render entity_print event. | ||
| * | ||
| * Injects an envelope-window element containing address information. | ||
| */ | ||
| public function onPrintRender(PrintHtmlAlterEvent $event): void { | ||
| $html = &$event->getHtml(); | ||
|
|
||
| // Only modify HTML if there is exactly one submission. | ||
| if (count($event->getEntities()) === 1) { | ||
| $submission = $event->getEntities()[0]; | ||
| if ($submission instanceof WebformSubmissionInterface) { | ||
| // Check whether generation is for digital post. | ||
| if ($lookupResult = $this->getDigitalPostContext($submission)) { | ||
|
|
||
| // Combine address parts. | ||
| $streetAddress = $lookupResult->getStreet(); | ||
|
|
||
| if ($lookupResult->getHouseNr()) { | ||
| $streetAddress .= ' ' . $lookupResult->getHouseNr(); | ||
| } | ||
|
|
||
| $extendedAddress = ''; | ||
|
|
||
| if ($lookupResult->getFloor()) { | ||
| $extendedAddress = $lookupResult->getFloor(); | ||
| } | ||
| if ($lookupResult->getApartmentNr()) { | ||
| $extendedAddress .= ' ' . $lookupResult->getApartmentNr(); | ||
| } | ||
|
|
||
| // Generate address HTML. | ||
| $addressHtml = '<div id="envelope-window-digital-post"><div class="h-card">'; | ||
| $addressHtml .= '<div class="p-name">' . htmlspecialchars($lookupResult->getName()) . '</div>'; | ||
| if ($lookupResult->getCoName()) { | ||
| $addressHtml .= '<div class="p-name">c/o ' . htmlspecialchars($lookupResult->getCoName()) . '</div>'; | ||
| } | ||
| $addressHtml .= '<div>'; | ||
| $addressHtml .= '<span class="p-street-address">' . htmlspecialchars($streetAddress) . '</span>'; | ||
| if (!empty($extendedAddress)) { | ||
| $addressHtml .= ' <span class="p-extended-address">' . htmlspecialchars($extendedAddress) . '</span>'; | ||
| } | ||
| $addressHtml .= '</div>'; | ||
| $addressHtml .= '<div>'; | ||
| $addressHtml .= '<span class="p-postal-code">' . htmlspecialchars($lookupResult->getPostalCode()) . '</span>'; | ||
| $addressHtml .= ' <span class="p-locality">' . htmlspecialchars($lookupResult->getCity()) . '</span>'; | ||
| $addressHtml .= '</div>'; | ||
| $addressHtml .= '</div>'; | ||
| $addressHtml .= '</div>'; | ||
|
|
||
| // Insert address HTML immediately after body opening tag. | ||
| $html = preg_replace('@<body[^>]*>@', '${0}' . $addressHtml, $html); | ||
jekuaitk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public static function getSubscribedEvents(): array { | ||
| return [ | ||
| PrintEvents::POST_RENDER => ['onPrintRender'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Indicate Digital Post context in the current session. | ||
| */ | ||
| public function setDigitalPostContext(WebformSubmissionInterface $submission, CompanyLookupResult|CprLookupResult $lookupResult): void { | ||
| $key = $this->createSessionKeyFromSubmission($submission); | ||
| $this->session->set($key, $lookupResult); | ||
| } | ||
|
|
||
| /** | ||
| * Check for Digital Post context in the current session. | ||
| */ | ||
| public function getDigitalPostContext(WebformSubmissionInterface $submission): CompanyLookupResult|CprLookupResult|null { | ||
| $key = $this->createSessionKeyFromSubmission($submission); | ||
|
|
||
| $digitalPostContext = $this->session->get($key); | ||
|
|
||
| // We only need/use it once, so just remove it after fetching it. | ||
| if ($digitalPostContext) { | ||
| $this->session->remove($key); | ||
| } | ||
|
|
||
| return $digitalPostContext; | ||
| } | ||
|
|
||
| /** | ||
| * Create a session key from a submission that is unique to the submission. | ||
| */ | ||
| public function createSessionKeyFromSubmission(WebformSubmissionInterface $submission): string { | ||
| // Due to cloning of submission during attachment logic, we cannot use | ||
| // submission id or uuid. Webform serial, however, is copied along, so a | ||
| // combination of webform id and serial is used for uniqueness. | ||
| // @see \Drupal\os2forms_attachment\Element\AttachmentElement::overrideWebformSettings | ||
| return 'digital_post_context_' . $submission->getWebform()->id() . '_' . $submission->serial(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.