Skip to content

Instantly share code, notes, and snippets.

@rocketeerbkw
Last active June 23, 2023 18:42
Show Gist options
  • Save rocketeerbkw/239219a6493c2adee34fb7ceb5af2072 to your computer and use it in GitHub Desktop.
Save rocketeerbkw/239219a6493c2adee34fb7ceb5af2072 to your computer and use it in GitHub Desktop.
How to migrate multi-value link field in Drupal 8
<?php
class Example extends SourcePluginBase {
public function prepareRow(Row $row) {
parent::prepareRow($row);
// I do some data manipulation to end up with an array that looks like this,
// which I want to import into multi-value link field.
$links = [
[
'title' => 'Link 1',
'uri' => 'URI 1',
],
[
'title' => 'Link 2',
'uri' => 'URI 2',
],
[
'title' => 'Link 3',
'uri' => 'URI 3',
],
];
$row->setSourceProperty('links', $links);
}
}
id: example
label: 'Example'
source:
plugin: example
keys:
- iid
process:
type:
plugin: default_value
default_value: example
title: title
# This is a multi-value drupal core link field.
# I want to migrate title and uri for each link.
# See example.php source plugin in this gist.
# This does not work.
field_links: links
# This also does not work.
'field_links/title': 'links/title'
'field_links/uri': 'links/uri'
# This worked!
field_links:
plugin: iterator
source: links
process:
title: title
uri: uri
destination:
plugin: 'entity:node'
@PabloGMZILLI
Copy link

PabloGMZILLI commented Jun 23, 2023

For those who need to import from a CSV file, here's an example of how it worked for me:

I created a preprocess so that the links were in this format:

CSV file:

ExampleId,Links
17327,"Link Text 1,www.google.com|Link Text 2,www.google.com|Link Text 3,www.google.com|Link Text 4,www.google.com"

And in migrate I did it this way:

  link_items:
    - plugin: skip_on_empty
      source: Links
      method: process
    - plugin: explode
      delimiter: '|'
  field_buttons:
    - plugin: explode
      source: '@link_items'
      delimiter: ','
    - plugin: sub_process
      process:
        title: '0'
        uri: '1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment