55use Drupal \bluecadet_utilities \DrupalStateTrait ;
66use Drupal \Core \Entity \EntityFieldManager ;
77use Drupal \Core \Entity \EntityTypeManager ;
8- use Drupal \Core \Form \ FormBase ;
8+ use Drupal \Core \File \ FileSystemInterface ;
99use Drupal \Core \Form \FormStateInterface ;
10+ use Drupal \Core \Form \FormBase ;
1011use Drupal \Core \Link ;
1112use Drupal \Core \Messenger \MessengerTrait ;
13+ use Drupal \Core \Url ;
14+ use Drupal \file \Entity \File ;
1215
1316/**
1417 * Bluecadet Utility Settings Form.
@@ -39,6 +42,13 @@ class EntityReferenceFieldSearch extends FormBase {
3942 */
4043 private $ entityTypeManager ;
4144
45+ /**
46+ * File system Interface for reading and writing files.
47+ *
48+ * @var \Drupal\Core\File\FileSystemInterface
49+ */
50+ protected $ fileSystem ;
51+
4252 /**
4353 * Get module handler.
4454 */
@@ -70,6 +80,16 @@ private function entityTypeManager(): EntityTypeManager {
7080 return $ this ->entityTypeManager ;
7181 }
7282
83+ /**
84+ * Get Entity Type Manager.
85+ */
86+ private function fileSystem (): FileSystemInterface {
87+ if (!$ this ->fileSystem ) {
88+ $ this ->fileSystem = \Drupal::service ('file_system ' ); // phpcs:ignore
89+ }
90+ return $ this ->fileSystem ;
91+ }
92+
7393 /**
7494 * {@inheritdoc}
7595 */
@@ -93,8 +113,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
93113
94114 $ form ['entity_type ' ] = [
95115 '#type ' => 'select ' ,
96- '#title ' => $ this ->t ("Entity Types " ),
97- '#description ' => $ this ->t ("Choose the Entity type you want to search for " ),
116+ '#title ' => $ this ->t ("Entities " ),
98117 '#options ' => $ entity_opts ,
99118 '#default_value ' => $ session_data [1 ]['entity_type ' ] ?? "" ,
100119 ];
@@ -104,8 +123,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
104123 '#type ' => 'textfield ' ,
105124 '#title ' => $ this ->t ("Entity Id " ),
106125 '#step ' => 1 ,
107- '#description ' => $ this ->t ("This is doing a full string search on the raw html of the text field values. You can use '%' as a wildcard. As most ids are ints, be careful . " ),
126+ // '#description' => $this->t("This is doing a full string search on the raw html of the text field values. You can use '%' as a wildcard."),
108127 '#default_value ' => $ session_data [1 ]['search_str ' ] ?? "" ,
128+ // '#placeholder' => "%class=\"material-icons\"% OR %<a name=\"%\"></a>%",
129+ ];
130+
131+ $ form ['sep1 ' ] = [
132+ '#markup ' => "<hr> " ,
133+ ];
134+
135+ // Include JSON output file.
136+ $ form ['json ' ] = [
137+ '#type ' => 'checkbox ' ,
138+ '#title ' => $ this ->t ("JOSN output " ),
139+ '#step ' => 1 ,
140+ '#description ' => $ this ->t ("Should the form create an output file of the raw data? " ),
141+ '#default_value ' => FALSE ,
109142 ];
110143
111144 // Actions.
@@ -115,6 +148,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
115148 '#value ' => $ this ->t ('Search ' ),
116149 ];
117150
151+ // Build outpur results from previous run.
118152 $ striper = [
119153 'transparent ' => '#eeeeee ' ,
120154 '#eeeeee ' => 'transparent ' ,
@@ -125,11 +159,21 @@ public function buildForm(array $form, FormStateInterface $form_state) {
125159 $ form ['results ' ] = [
126160 '#weight ' => -1 ,
127161 'timing ' => [
162+ '#prefix ' => "<p> " ,
128163 '#markup ' => "Results took " . $ session_data [3 ],
164+ '#suffix ' => "</p> " ,
129165 ],
166+ 'download ' => [],
130167 'results ' => [],
131168 ];
132169
170+ // Add in file download link if available.
171+ if (!empty ($ session_data [1 ]['file_link ' ])) {
172+ $ form ['results ' ]['download ' ]['#prefix ' ] = "<p> " ;
173+ $ form ['results ' ]['download ' ]['#suffix ' ] = "</p> " ;
174+ $ form ['results ' ]['download ' ]['download_data ' ] = $ session_data [1 ]['file_link ' ]->toRenderable ();
175+ }
176+
133177 foreach ($ session_data [1 ]['data ' ] as $ entity_type => $ data ) {
134178
135179 $ list = [
@@ -232,6 +276,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
232276 [],
233277 ];
234278
279+ if ($ values ['json ' ] == TRUE ) {
280+ $ batch ['operations ' ][] = [
281+ [$ this , 'createJsonOutput ' ],
282+ [],
283+ ];
284+ }
285+
235286 batch_set ($ batch );
236287 }
237288
@@ -370,6 +421,54 @@ public static function processResults_paragraph($results, $field_id, &$context)
370421
371422 // phpcs:enable Drupal.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
372423
424+ /**
425+ * Out put raw data to a JSON file.
426+ *
427+ * @todo need a cron job to clean up old files.
428+ */
429+ public static function createJsonOutput (&$ context ) {
430+
431+ if ($ context ['results ' ]['raw ' ]) {
432+ $ file_system = \Drupal::service ('file_system ' );
433+ // @todo add to a settings config page.
434+ $ destination = "public://data_search_exports/ " ;
435+ // Set json data from the raw results.
436+ $ data = json_encode ($ context ['results ' ]['raw ' ]);
437+
438+ $ search_str_sanatized = mb_ereg_replace ("([^\w\s\d\-_~,;\[\]\(\).]) " , '' , $ context ['results ' ]['search_str ' ]);
439+ $ search_str_sanatized = mb_ereg_replace ("([^\w\s\d\-_~,;\[\]\(\).]) " , '' , $ search_str_sanatized );
440+
441+ $ filename = date ("YmdHis " ) . "-- " . $ search_str_sanatized . "-- " . "ent_ref_search.json " ;
442+
443+ if (!$ file_system ->prepareDirectory ($ destination , FileSystemInterface::CREATE_DIRECTORY )) {
444+ // @todo Log an error.
445+ return FALSE ;
446+ }
447+
448+ $ finale_file = $ file_system ->saveData ($ data , $ destination . $ filename , FileSystemInterface::EXISTS_REPLACE );
449+
450+ if ($ finale_file ) {
451+ // Create temporary File entity.
452+ // We do this so Drupal will clean up the file eventually.
453+ $ new_file = File::create (['uri ' => $ finale_file ]);
454+ $ new_file ->setOwnerId (1 );
455+ $ new_file ->setTemporary ();
456+ $ new_file ->save ();
457+
458+ $ new_file_url = URL ::fromUserInput ($ new_file ->createFileUrl (), [
459+ 'attributes ' => [
460+ 'download ' => TRUE ,
461+ ],
462+ ]);
463+
464+ $ link = Link::fromTextAndUrl ("Download JSON Data " , $ new_file_url );
465+
466+ $ context ['results ' ]['file_file ' ] = $ new_file ;
467+ $ context ['results ' ]['file_link ' ] = $ link ;
468+ }
469+ }
470+ }
471+
373472 /**
374473 * Batch finished callback.
375474 */
0 commit comments