9

I'm running a multilingual migration of events (which are stored as nodes) that have keywords on them where there are keywords (stored as terms). The keywords should be stored in a multilingual tags vocabulary.

The source is a REST API, and what I have for the keyword data looks like this:

array (
 'id' => 'yso:p360',
 'alt_labels' => 
 array (
 ),
 'created_time' => '2014-06-23T11:37:29.198000Z',
 'last_modified_time' => '2014-06-23T11:37:29.198000Z',
 'aggregate' => false,
 'deprecated' => false,
 'n_events' => 11709,
 'data_source' => 'yso',
 'image' => NULL,
 'name' => 
 array (
   'fi' => 'kulttuuritapahtumat',
   'en' => 'cultural events',
   'sv' => 'kulturevenemang',
 ),
 '@id' => 'https://api.hel.fi/linkedevents/v1/keyword/yso:p360/',
 '@context' => 'http://schema.org',
 '@type' => 'Keyword',
)

Is there some way to use for example EntityGenerate to create all the translations of the tags when importing the original languages of the events that has these tags?

Note:

I would like to avoid migrating all the keywords first. We only import a subset of the events, and we are only interested in the keywords that are used by those events.

The normal way of D8 migrations with multilingual sources is to create a configuration for each translation. This doesn't work in our case, as I chose to use entity_generate for the tags. If you have another suggestion, I am interested.

2 Answers 2

1

I'm missing some information to fully understand your problem, but to migrate an object which has many translation you need: Create the initial entity in the source language of your choice and add translations to this entity. For example, for a taxonomy term:

$default_language = "en";
$other_languages = ["fi", "sv"];

$term = \Drupal\taxonomy\Entity\Term::create([
  'vid' => 'my_vocabulary',
  'name' => $my_importer_object['name'][$default_language],
]);

$term->save();

foreach ($other_languages as $language) {
  $term_object = [
    'vid' => 'my_vocabulary',
    'name' => $my_importer_object['name'][$language],
  ];
  $term->addTranslation($language, $term_object);

  $term->save();
}

If it's a taxonomy term, it will be referenced in your object like an entity reference, with its tid. The link between the translated version of the term and the translated version of the node will be the language. If the term exists in the language of the node, Drupal 8 will used it automatically. If the term isn't translated in the language, Drupal will use the original language of the term, in which it had been initially created.

0

Here is an example media migration config file that demonstrates creating translations of media entities based on the original default (English) entity. Keep in mind this relies on other migrations (files, default media entities in default language) and is not a complete example. I just wanted to highlight the actual config I used to create the translations.

The key piece is in the destination plugin, the "translations: true" attribute. Combined with the process plugin for looking up "mid" so we get the right source entity to translate:

langcode: en # Langcode of the config, nothing to do with the entities
status: true

id: file_json_to_media_es
migration_group: files_to_media
label: 'Import files to Media entities (ES)'
source:
  plugin: url
  data_fetcher_plugin: http
  urls:
    - 'https://example.com/files.json' # A JSON file as source, in my example
  data_parser_plugin: json
  item_selector: '[*]'
  fields:
    -
      name: id
      label: 'ID'
      selector: id
    -
      name: path_name
      label: 'Path Name'
      selector: /language/es/path_name
    -
      name: title
      label: Title
      selector: /language/es/title
  ids:
    id:
      type: integer

  # Use the ID as the key unique migration identifier.
  keys:
    - id

process:
  mid:
    - plugin: migration_lookup
      migration: file_to_media_english
      source: id #this is a custom id field in the source JSON, this can be any unique key you have in your setup
  langcode:
    - plugin: default_value
      default_value: es
  name: title
  field_media_file/target_id:
    - plugin: migration_lookup
      migration: file_import_es # a migration that created the files
      source: id # same, just needs to be your unique id field as needed
destination:
  plugin: 'entity:media'
  default_bundle: resource # Whatever your media bundle is
  translations: true # this is the important attribute!
migration_dependencies:
  required:
    - file_to_media_english # the default media entities
    - file_import_es # the file migration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.