Skip to content

Instantly share code, notes, and snippets.

@opi
Last active February 21, 2024 09:47
Show Gist options
  • Save opi/391be1e3252783930510625be9ec6bef to your computer and use it in GitHub Desktop.
Save opi/391be1e3252783930510625be9ec6bef to your computer and use it in GitHub Desktop.
iea_migrate_partners.yml
<?php
namespace Drupal\iea_migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Provides a 'getWithFallback' migrate process plugin.
*
* @MigrateProcessPlugin(
* id = "get_fallback",
* )
*
* @code
* field_my_field:
* plugin: get_fallback
* source: my_src_field
* fallback: my_src_fallback_field
* @endcode
*
*/
class getWithFallback extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($value)) {
$fallback = $this->configuration['fallback'];
if (!empty($fallback)) {
$value = $row->getSourceProperty($fallback);
if (empty($value) && str_starts_with($fallback, '@')) {
$value = $row->get($fallback);
}
}
}
return $value;
}
}
id: iea_migrate_partners
label: IEA Migrate Partners (Nodes)
source:
plugin: partners
key: migrate
process:
title: partenaire_titre_fr
field_partner_link: partenaire_lien_fr
field_partner_logo/target_id:
-
plugin: migration_lookup
migration: iea_migrate_partners_media
source: partenaire_id
langcode:
plugin: default_value
default_value: fr
destination:
plugin: 'entity:node'
default_bundle: partner
migration_dependencies:
required:
- iea_migrate_partners_media
id: iea_migrate_partners_translations
label: IEA Migrate Partners (Translations)
source:
plugin: partners
key: migrate
process:
# This is IMPORTANT for Drupal to match this node with the original one
nid:
plugin: migration_lookup
migration: iea_migrate_partners
source: partenaire_id
# title: partenaire_titre_fr
title:
plugin: get_fallback
source: partenaire_titre_en
fallback: partenaire_titre_fr
# field_partner_link: partenaire_lien_fr
field_partner_link:
plugin: get_fallback
source: partenaire_lien_en
fallback: partenaire_lien_fr
# Do not translate this field...
# field_partner_logo/target_id:
# -
# plugin: migration_lookup
# migration: iea_migrate_partners_media
# source: partenaire_id
langcode:
plugin: default_value
default_value: en
destination:
plugin: 'entity:node'
default_bundle: partner
# Required for content translation to work
translations: true
migration_dependencies:
required:
- iea_migrate_partners_media
- iea_migrate_partners
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment