77use Drupal \Core \Controller \ControllerBase ;
88use Drupal \Core \Entity \EntityTypeManagerInterface ;
99use Drupal \Core \Url ;
10+ use Drupal \os2forms_fordelingskomponent \Helper \WebformHelperSF2900 ;
1011use Drupal \os2forms_fordelingskomponent \Hook \ThemeHooks ;
12+ use Drupal \os2forms_fordelingskomponent \Model \Attachment ;
1113use Drupal \os2forms_fordelingskomponent \Plugin \WebformHandler \WebformHandlerSF2900 ;
1214use Drupal \os2forms_fordelingskomponent \Settings ;
15+ use Drupal \os2forms_fordelingskomponent \Settings \HandlerSettings ;
1316use Drupal \webform \WebformInterface ;
17+ use Drupal \webform \WebformSubmissionInterface ;
1418use Drupal \webform \WebformSubmissionStorageInterface ;
1519use Symfony \Component \HttpFoundation \Request ;
1620use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
@@ -27,6 +31,7 @@ final class Os2formsFordelingskomponentDistributionObjectPreviewController exten
2731
2832 public function __construct (
2933 private readonly Settings $ settings ,
34+ private readonly WebformHelperSF2900 $ helper ,
3035 EntityTypeManagerInterface $ entityTypeManager ,
3136 ) {
3237 $ this ->submissionStorage = $ entityTypeManager ->getStorage ('webform_submission ' );
@@ -35,7 +40,7 @@ public function __construct(
3540 /**
3641 * Builds the response.
3742 */
38- public function __invoke (Request $ request , WebformInterface $ webform , string $ webform_handler ): array {
43+ public function __invoke (Request $ request , WebformInterface $ webform , string $ webform_handler, ? WebformSubmissionInterface $ webform_submission ): array {
3944 try {
4045 $ handler = $ webform ->getHandler ($ webform_handler );
4146 }
@@ -55,19 +60,22 @@ public function __invoke(Request $request, WebformInterface $webform, string $we
5560 ->condition ('webform_id ' , $ webform ->id ())
5661 ->sort ('created ' , 'DESC ' )
5762 ->execute ());
58- $ currentSubmissionId = ( int ) $ request -> query -> get ( ' submission ' );
63+ $ currentSubmissionId = $ webform_submission ?->id( );
5964 $ index = array_search ($ currentSubmissionId , $ submissionIds );
6065 if (FALSE === $ index ) {
6166 $ currentSubmissionId = reset ($ submissionIds ) ?: NULL ;
6267 $ index = array_search ($ currentSubmissionId , $ submissionIds );
6368 }
69+ if ($ currentSubmissionId ) {
70+ $ webform_submission = $ this ->submissionStorage ->load ($ currentSubmissionId );
71+ }
6472
6573 $ routeName = $ request ->attributes ->get ('_route ' );
66- $ previewUrls = array_map (
74+ $ links = array_map (
6775 static fn ($ submission ) => Url::fromRoute ($ routeName , [
6876 'webform ' => $ webform ->id (),
6977 'webform_handler ' => $ handler ->getHandlerId (),
70- 'submission ' => $ submission ,
78+ 'webform_submission ' => $ submission ,
7179 ]),
7280 array_filter ([
7381 'prev ' => $ submissionIds [$ index + 1 ] ?? NULL ,
@@ -76,21 +84,46 @@ public function __invoke(Request $request, WebformInterface $webform, string $we
7684 ])
7785 );
7886
79- $ renderUrl = NULL !== $ currentSubmissionId
80- ? Url::fromRoute ('os2forms_fordelingskomponent.fordelingskomponent_distribution_object.preview_render ' , [
81- 'webform ' => $ webform ->id (),
82- 'webform_handler ' => $ handler ->getHandlerId (),
83- 'submission ' => $ currentSubmissionId ,
84- ])
85- : NULL ;
86-
8787 return [
8888 '#theme ' => ThemeHooks::DISTRIBUTION_OBJECT_PREVIEW ,
8989 '#webform ' => $ webform ,
90+ '#submission ' => $ webform_submission ,
9091 '#handler ' => $ handler ,
9192 '#handler_settings ' => $ handlerSettings ,
92- '#render_url ' => $ renderUrl ,
93- '#preview_urls ' => $ previewUrls ,
93+ '#preview ' => $ webform_submission ? $ this ->renderPreview ($ handler , $ handlerSettings , $ webform_submission ) : NULL ,
94+ '#links ' => $ links ,
95+ ];
96+ }
97+
98+ /**
99+ * Render preview of distribution object.
100+ */
101+ public function renderPreview (WebformHandlerSF2900 $ handler , HandlerSettings $ handlerSettings , WebformSubmissionInterface $ submission ): array {
102+ $ exceptions = [];
103+ $ warnings = [];
104+
105+ $ distributionObject = NULL ;
106+ $ xml = [];
107+ try {
108+ $ attachment = new Attachment ('preview ' , Attachment::MIME_TYPE_PDF , 'preview.pdf ' );
109+ $ distributionObject = $ this ->helper ->buildDistributionObject ($ handlerSettings , $ submission , $ attachment );
110+ }
111+ catch (\Exception $ exception ) {
112+ $ exceptions [] = $ exception ;
113+ }
114+
115+ try {
116+ $ xml = $ this ->helper ->renderXml ($ handlerSettings , $ submission , validateXml: FALSE );
117+ }
118+ catch (\Throwable ) {
119+ // Silently ignore any errors.
120+ }
121+
122+ return [
123+ 'exceptions ' => $ exceptions ,
124+ 'warnings ' => $ warnings ,
125+ 'distribution_object ' => $ distributionObject ,
126+ 'xml ' => $ xml ,
94127 ];
95128 }
96129
0 commit comments