Skip to content

Instantly share code, notes, and snippets.

@dustinleblanc
Created October 20, 2024 19:05
Show Gist options
  • Save dustinleblanc/49f6715a45ae3c8af8e5c3ecd51d621b to your computer and use it in GitHub Desktop.
Save dustinleblanc/49f6715a45ae3c8af8e5c3ecd51d621b to your computer and use it in GitHub Desktop.
WIP Bard Content migration from Statamic to Drupal. Not complete or robust, but working for my use case right now
<?php
namespace Drupal\migrate_statamic\Plugin\migrate\process;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a migrate_statamic_bard_content plugin.
*
* Usage:
*
* @code
* process:
* bar:
* plugin: migrate_statamic_bard_content
* source: foo
* type_map:
* centered_content:
* type: centered_content
* fields:
* sub_heading: field_sub_heading
* main_content: field_main_content
* text:
* type: basic_text
* fields:
* text: field_text
* @endcode
*
* @MigrateProcessPlugin(
* id = "migrate_statamic_bard_content"
* )
*
* @DCG
* ContainerFactoryPluginInterface is optional here. If you have no need for
* external services just remove it and all other stuff except transform()
* method.
*/
class BardContent extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* Constructs a BardContent plugin.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* The transliteration service.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
private readonly EntityTypeManagerInterface $entity_type_manager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$type = $value['type'];
$type_info = $this->configuration['type_map'][$type];
$stub = $this->entity_type_manager->getStorage('paragraph')->create([
'type' => $type_info['type'],
]);
foreach ($type_info['fields'] as $source => $destination) {
if (array_key_exists('allowed_formats', $stub->get($destination)->getSettings())) {
$data = [
'format' => 'markdown',
'value' => $value[$source],
];
} else {
$data = $value[$source];
}
$stub->set($destination, $data);
}
$stub->save();
return [
'target_id' => $stub->id(),
'target_revision_id' => $stub->getRevisionId(),
];
}
}
langcode: en
status: true
dependencies:
enforced:
module:
- migrate_plus
id: statamic_article
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags:
- 'statamic content'
migration_group: 'Statamic Site'
label: 'Statamic Articles'
source:
plugin: url
data_fetcher_plugin: http
data_parser_plugin: json
urls:
- 'your_website_url'
item_selector: data
fields:
-
name: id
label: Id
selector: id
-
name: title
label: Title
selector: title
-
name: content
label: 'Main content'
selector: content
-
name: excerpt
label: Excerpt
selector: excerpt
-
name: url
label: 'Source URL'
selector: permalink
-
name: author
label: Author
selector: author/0/id
-
name: bard_content
label: 'Bard Content'
selector: bard_content
ids:
id:
type: uuid
process:
type:
plugin: default_value
default_value: article
title: title
body/format:
plugin: default_value
default_value: markdown
body/value: content
body/summary: excerpt
sticky:
plugin: default_value
default_value: 0
path/alias:
plugin: str_replace
source: url
search: 'http://yourdomainhere'
replace: ''
path/pathauto:
plugin: default_value
default_value: false
uid:
plugin: migration_lookup
migration: statamic_users
source_ids:
statamic_users:
- author
field_remixables:
plugin: migrate_statamic_bard_content
source: bard_content
type_map:
centered_content:
type: centered_content
fields:
sub_heading: field_subheading
heading: field_heading
lead_copy: field_lead_copy
main_content: field_main_content
text:
type: text
fields:
text: field_text
basic:
type: text
fields:
body: field_text
figure:
type: figure
fields:
figure_caption: field_figure_caption
figure_image: field_figure_image
destination:
plugin: 'entity:node'
migration_dependencies:
required:
- statamic_users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment