From dbd7b598021b3b200b13b62e5daa6929000e330b Mon Sep 17 00:00:00 2001 From: Dimitris Spachos Date: Wed, 18 Dec 2024 09:34:49 +0200 Subject: [PATCH] chore(slb-495): add dropzone widget to import form --- .../silverback_ai_import.module | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/packages/drupal/silverback_ai/modules/silverback_ai_import/silverback_ai_import.module b/packages/drupal/silverback_ai/modules/silverback_ai_import/silverback_ai_import.module index 4c92de17..12ca80f1 100644 --- a/packages/drupal/silverback_ai/modules/silverback_ai_import/silverback_ai_import.module +++ b/packages/drupal/silverback_ai/modules/silverback_ai_import/silverback_ai_import.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Form\FormStateInterface; +use Drupal\file\Entity\File; /** * {@inheritdoc} @@ -45,19 +46,30 @@ function silverback_ai_import_form_node_page_split_form_alter(&$form, FormStateI ], ]; + /* $form['import']['container_docx']['file'] = [ - '#type' => 'managed_file', - '#title' => t('Select a Microsoft Word file'), - '#description' => t('Select a Microsoft Word document to import content from.'), - // @todo Update this to some private file path, shared with node service. - '#upload_location' => 'public://converted/', - '#multiple' => FALSE, - '#attributes' => [ - 'class' => ['file--import'], - ], - '#upload_validators' => [ - 'file_validate_extensions' => ['doc docx DOC DOCX'], - ], + '#type' => 'managed_file', + '#title' => t('Select a Microsoft Word file'), + '#description' => t('Select a Microsoft Word document to import content from.'), + // @todo Update this to some private file path, shared with node service. + '#upload_location' => 'public://converted/', + '#multiple' => FALSE, + '#attributes' => [ + 'class' => ['file--import'], + ], + '#upload_validators' => [ + 'file_validate_extensions' => ['doc docx DOC DOCX'], + ], + ]; + */ + + $form['import']['container_docx']['file'] = [ + '#title' => t('Drag and drop a Microsoft Word file'), + '#type' => 'dropzonejs', + '#required' => TRUE, + '#dropzone_description' => 'DropzoneJS description', + '#max_filesize' => '1M', + '#extensions' => 'doc docx', ]; $form['import']['container_url'] = [ @@ -89,22 +101,32 @@ function silverback_ai_import_form_node_page_split_form_alter(&$form, FormStateI */ function _silverback_ai_import_form_submit(array $form, FormStateInterface $form_state) { $entity = $form_state->getFormObject()->getEntity(); - $fid = reset($form_state->getValue('file')); + + // $fid = reset($form_state->getValue('file')); $url_value = $form_state->getValue('url_value'); $service = \Drupal::service('silverback_ai_import.content'); $content = \Drupal::service('silverback_ai_import.batch.import'); $type = $form_state->getValue('import_type'); - if ($fid && $type == 'docx') { - $file = \Drupal::entityTypeManager()->getStorage('file')->load($fid); + + // @todo Surround with try-catch + $file = $form_state->getValue('file'); + $filepath = $file['uploaded_files'][0]['path']; + $file = File::create([ + 'filename' => basename($filepath), + 'uri' => $filepath, + 'status' => 1, + 'uid' => 1, + ]); + + if ($file && $type == 'docx') { + // $file = \Drupal::entityTypeManager()->getStorage('file')->load($fid); $ast = $service->getAstFromFilePath($file); - // $content->create($ast->content, $entity); $flatten = $service->flattenAst($ast->content); $content->create($flatten, $entity); } elseif (!empty($url_value) && $type == 'url') { $ast = $service->getAstFromUrl($url_value); - // $content->create($ast->content, $entity); $flatten = $service->flattenAst($ast->content); $content->create($flatten, $entity); }