3

Given a source that has a field with the corresponding ID which outputs the id in the field special like the field nid gives back a node id.

I want to map it to a taxonomy term that is already existing on the new site and not migrated.

This is the process part of my custom yml file:

process:
  nid: nid
  vid: vid
  # there were some more fields here that I left out.
  # hardcode destination node type as publication
  type:
    plugin: default_value
    default_value: publication
  # custom fields
  field_special:
    plugin: static_map
    source: special
    map:
      1: 1
      2: 2
      3: 3

I have a field_special as taxonomy term reference field in the node I want to migrate to.

How can I map the source field to be used in the destination field which is a taxonomy term reference field?

I'm using Drupal 8.1.2 with migrate_plus migrate_tools migrate_drupal enabled.

  • Can you add snippets to sample data you are using? – Eyal Jun 19 '16 at 18:56
  • And are you using Drupal 8.1 or 8.0 (or 8.2)? – Eyal Jun 19 '16 at 18:57
  • Also, are you using the migrate_plus module? – Eyal Jun 19 '16 at 19:39
2

I've rewritten my yml to use:

field_special:
  plugin: iterator
  source: field_special
  process:
    target_id: tid

And written a preparerow function in my class to output an array as source property:

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $nid = $row->getSourceProperty('nid');

    $result = $this->getDatabase()->query(
      'SELECT 
        GROUP_CONCAT(field_special_tid) as tids
      FROM
        {field_data_field_special} fs
      WHERE
        fs.entity_id = :nid
      ',
      array(':nid' => $nid)
    );
    foreach ($result as $record) {
      if (!is_null($record->tids)) {
        $row->setSourceProperty('special', explode(',', $record->tids));
      }
    }
    return parent::prepareRow($row);
  }

This workaround will do for now, but if you have a better answer please post so I can accept your answer.

  • 1
    Your solution seems the way to go. The only issue is the format in which you queried the database. – Eyal Jun 19 '16 at 19:54
  • The query works, Tough it could be nicer but since it's a migration script I didn't care to use a entity field query. – FLY Jun 20 '16 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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