55use Drupal \Core \Entity \EntityTypeManagerInterface ;
66use Drush \Commands \DrushCommands ;
77use Drupal \os2forms_digital_post \Manager \TemplateManager ;
8- use Drupal \Core \Config \ConfigFactoryInterface ;
98
109/**
11- * A drush command file.
10+ * A drush command file for commands related to os2forms_digital_post .
1211 *
1312 * @package Drupal\event_database_pull\Commands
1413 */
@@ -35,7 +34,7 @@ public function __construct(TemplateManager $templateManager, EntityTypeManagerI
3534 }
3635
3736 /**
38- * Create PDF named test.pdf in current directory.
37+ * Create PDF in directory relative to Drupal root directory.
3938 *
4039 * @param string $template
4140 * The template name to use.
@@ -50,30 +49,28 @@ public function __construct(TemplateManager $templateManager, EntityTypeManagerI
5049 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
5150 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
5251 */
53- public function create ($ template , $ options = ['submission_id ' => 0 ]) {
52+ public function create ($ template , $ options = ['submission_id ' => 0 , ' file_location ' => '' , ' file_name ' => ' test.pdf ' ]) {
5453 $ elements [] = [];
5554 $ webformLabel = '' ;
56- if (0 < $ options ['submission_id ' ]) {
57- $ webform_submission = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->load ($ options ['submission_id ' ]);
58- if ($ webform_submission ) {
59- $ webform = $ webform_submission ->getWebform ();
60- $ webformLabel = $ webform ->label ();
61- $ submissionData = $ webform_submission ->getData ();
62- foreach ($ submissionData as $ key => $ value ) {
63- $ element = $ webform ->getElement ($ key , TRUE );
64- $ elements [] = [
65- 'name ' => $ element ['#title ' ],
66- 'value ' => isset ($ element ['#return_value ' ]) ? $ element ['#return_value ' ] : $ value ,
67- ];
68- }
69- }
70- else {
71- $ this ->output ()->writeln ('Submission id: ' . $ options ['submission_id ' ] . ' not found. An empty ' . $ template . ' template was created. ' );
55+ $ webform_submission = $ this ->entityTypeManager ->getStorage ('webform_submission ' )->load ($ options ['submission_id ' ]);
56+ if ($ webform_submission ) {
57+ $ webform = $ webform_submission ->getWebform ();
58+ $ webformLabel = $ webform ->label ();
59+ $ submissionData = $ webform_submission ->getData ();
60+ foreach ($ submissionData as $ key => $ value ) {
61+ $ element = $ webform ->getElement ($ key , TRUE );
62+ $ elements [] = [
63+ 'name ' => $ element ['#title ' ],
64+ 'value ' => $ element ['#return_value ' ] ?? $ value ,
65+ ];
7266 }
7367 }
68+ else {
69+ $ this ->output ()->writeln ('Submission id: ' . $ options ['submission_id ' ] . ' not found. An empty ' . $ template . ' template was created. ' );
70+ }
7471
7572 $ recipient = [
76- 'name ' => 'Test testersen ' ,
73+ 'name ' => 'Test Testersen ' ,
7774 'streetName ' => 'Testervej ' ,
7875 'streetNumber ' => '1 ' ,
7976 'floor ' => '2 ' ,
@@ -90,6 +87,8 @@ public function create($template, $options = ['submission_id'=> 0]) {
9087
9188 $ pathToTemplate = $ template ;
9289 $ pdf = $ this ->templateManager ->renderPdf ($ pathToTemplate , $ context );
93- file_put_contents ('test.pdf ' , $ pdf );
90+ $ filePath = dirname (DRUPAL_ROOT ) . $ options ['file_location ' ] . '/ ' . $ options ['file_name ' ];
91+ file_put_contents ($ filePath , $ pdf );
92+ $ this ->output ()->writeln (sprintf ('Pdf written to %s ' , $ filePath ));
9493 }
9594}
0 commit comments