Skip to content

Commit

Permalink
chore(slb-495): add dropzone widget to import form
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Dec 18, 2024
1 parent 244b610 commit dbd7b59
Showing 1 changed file with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -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'] = [
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit dbd7b59

Please sign in to comment.