Skip to content

Commit

Permalink
feat(slb-495): preserve initial template for nodes on import
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Jan 17, 2025
1 parent 00dbee4 commit f30ebaf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ function _silverback_ai_import_form_submit(array $form, FormStateInterface $form
case ContentImportAiService::URL:
$file = $url_value;
break;
case ContentImportAiService::NONE:
break;
default:
$file = $form_state->getValue('file');
}

if ($type != ContentImportAiService::URL) {
if (in_array($type, [ContentImportAiService::DOCX, ContentImportAiService::PDF])) {
$file = $service->createFileEntityFromDropzoneData($file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class ContentImportAiService {
public const DOCX = 'docx';
public const URL = 'url';
public const PDF = 'pdf';
public const NONE = 'none';

/**
* Constructs a ContentImportAiService object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,28 @@ public static function finish(bool $success, array $results, array $operations)
// @todo Improve that to respect also templates
$implode = implode(PHP_EOL, $results['content']);
$content = <<<EOD
<!-- wp:custom/content -->
<!-- wp:custom/content -->
$implode
<!-- /wp:custom/content -->
EOD;

// @todo Add post import process here
try {
$node->body->value = $content;

// @todo
$config = \Drupal::service('config.factory')->get('gutenberg.settings');
$node_type = $node->type->getString();
$gutenberg_template = $config->get($node_type . '_template') ? json_decode($config->get($node_type . '_template')) : NULL;
$init = '';
// @todo We can do better than this
foreach ($gutenberg_template as $item) {
$init .= "<!-- wp:{$item[0]} /-->\n";
}

$cleaned = str_replace('<!-- wp:custom/content /-->', $content, $init);
$node->body->value = $cleaned;
$node->save();
} catch (\Exception $e) {
// @todo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,13 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
case ContentImportAiService::URL:
$file = $url_value;
break;
case ContentImportAiService::NONE:
break;
default:
$file = $form_state->getValue('file');
}

if ($type != ContentImportAiService::URL) {
if (in_array($type, [ContentImportAiService::DOCX, ContentImportAiService::PDF])) {
$file = $service->createFileEntityFromDropzoneData($file);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\silverback_ai_test\Unit;

use Drupal\Tests\UnitTestCase;

/**
* Test description.
*
* @group silverback_ai_test
*/
final class ExampleTest extends UnitTestCase {

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// @todo Mock required classes here.
}

/**
* Tests something.
*/
public function testSomething(): void {
self::assertTrue(TRUE, 'This is TRUE!');
}

}

0 comments on commit f30ebaf

Please sign in to comment.