-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slb-495): post import plugin updates
- Loading branch information
Showing
10 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
packages/drupal/silverback_ai/modules/silverback_ai_import/silverback_ai_import.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
services: | ||
silverback_ai_import.content: | ||
class: Drupal\silverback_ai_import\ContentImportAiService | ||
arguments: ['@current_route_match', '@current_user', '@entity_type.manager', '@logger.factory', '@config.factory', '@silverback_ai.openai_http_client', '@plugin.manager.ai.import'] | ||
arguments: ['@current_route_match', '@current_user', '@entity_type.manager', '@logger.factory', '@config.factory', '@silverback_ai.openai_http_client', '@plugin.manager.ai.import', '@plugin.manager.ai.post.import'] | ||
silverback_ai_import.batch.import: | ||
class: 'Drupal\silverback_ai_import\ContentImportBatch' | ||
arguments: | ||
- '@logger.factory' | ||
plugin.manager.ai.import: | ||
class: Drupal\silverback_ai_import\AiImportPluginManager | ||
arguments: ['@container.namespaces', '@cache.default', '@module_handler'] | ||
plugin.manager.ai.post.import: | ||
class: Drupal\silverback_ai_import\AiPostImportPluginManager | ||
arguments: ['@container.namespaces', '@cache.default', '@module_handler'] |
34 changes: 34 additions & 0 deletions
34
packages/drupal/silverback_ai/modules/silverback_ai_import/src/AiPostImportPluginManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Drupal\silverback_ai_import; | ||
|
||
use Drupal\Core\Cache\CacheBackendInterface; | ||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
use Drupal\Core\Plugin\DefaultPluginManager; | ||
|
||
/** | ||
* Manages sandwich plugins. | ||
*/ | ||
class AiPostImportPluginManager extends DefaultPluginManager { | ||
|
||
/** | ||
* Creates the discovery object. | ||
* | ||
* @param \Traversable $namespaces | ||
* An object that implements \Traversable which contains the root paths | ||
* keyed by the corresponding namespace to look for plugin implementations. | ||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend | ||
* Cache backend instance to use. | ||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | ||
* The module handler to invoke the alter hook with. | ||
*/ | ||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { | ||
$subdir = 'Plugin/AiPostImport'; | ||
$plugin_interface = 'Drupal\silverback_ai_import\AiPostImportPluginManagerInterface'; | ||
$plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin'; | ||
parent::__construct($subdir, $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name); | ||
$this->alterInfo('ai_post_import_info'); | ||
$this->setCacheBackend($cache_backend, 'ai_post_import_info'); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...pal/silverback_ai/modules/silverback_ai_import/src/AiPostImportPluginManagerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Drupal\silverback_ai_import; | ||
|
||
/** | ||
* An interface for all Sandwich type plugins. | ||
*/ | ||
interface AiPostImportPluginManagerInterface { | ||
|
||
/** | ||
* If the current plugin matches the given chunk. | ||
* | ||
* @return array | ||
* An array of chunks. | ||
*/ | ||
public function convert(array $chunks); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...odules/silverback_ai_import/src/Plugin/AiPostImport/EmptyChunksRemovePostImportPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Drupal\silverback_ai_import\Plugin\AiPostImport; | ||
|
||
use Drupal\Core\Plugin\PluginBase; | ||
use Drupal\silverback_ai_import\AiPostImportPluginManagerInterface; | ||
|
||
/** | ||
* Provides a default gutenberg block converter plugin. | ||
* | ||
* @Plugin( | ||
* id = "ai_empty_chunks", | ||
* label = @Translation("Filters empty chunks"), | ||
* weight = 0, | ||
* ) | ||
*/ | ||
class EmptyChunksRemovePostImportPlugin extends PluginBase implements AiPostImportPluginManagerInterface { | ||
|
||
/** | ||
* Constructs a \Drupal\Component\Plugin\PluginBase object. | ||
* | ||
* @param array $configuration | ||
* A configuration array containing information about the plugin instance. | ||
* @param string $plugin_id | ||
* The plugin ID for the plugin instance. | ||
* @param mixed $plugin_definition | ||
* The plugin implementation definition. | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition) { | ||
$this->configuration = $configuration; | ||
$this->pluginId = $plugin_id; | ||
$this->pluginDefinition = $plugin_definition; | ||
} | ||
|
||
/** | ||
* Get a description if the plugin. | ||
*/ | ||
public function description() { | ||
return $this->t('Filters empty chunks.'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function convert(array $chunks) { | ||
$chunks = array_filter($chunks, function ($item) { | ||
if (str_starts_with(trim($item), '<!-- wp:paragraph -->')) { | ||
$cleaned = $cleaned = preg_replace('/[\r\n]+/', '', strip_tags($item)); | ||
return strlen($cleaned) > 0; | ||
} | ||
return TRUE; | ||
}); | ||
return $chunks; | ||
} | ||
|
||
} |