1

I'm using Drupal 8's migrate system to import data from an XML source which has this simple layout:

<books>
    <book>
        <isbn>...</isbn>
        <title>...</title>
        <author>...</author>
    </book>
</books>

My code creates 'book' and 'author' entities on the Drupal side, with each 'book' referencing its corresponding 'author'. The problem is that different books can have the same author. This ends up creating a separate 'author' entity for every occurrence of the same author in the XML file.

I've looked at the $row variable in PrepareRow to see if there are ways of manipulating the ID map table to avoid the duplicates but haven't had any luck. So I'd appreciate any suggestions or ideas!

By the way, my code has two source plugins - one for 'author' (which runs first) and another for 'book'. I'm using the 'plugin: migration' in my YAML file to make the book entity pickup the author entity's ID. It all works except that author gets duplicated.

1

I ended up not using the "plugin:migration" option because apparently it assumes one-to-one relationships. So instead, I added code to the 'author' plugin's prepareRow to rejected incoming duplicate authors. Then I added code to my 'book' plugin's prepareRow to retrieve the author entity ids from the database and add it to the book entity. I was hoping for a built-in solution in the migrate system but this will do.

0

What is the source key you're using for the author migration? If you use a unique identifier for the book, then in your author migration's prepareRow(), if you find the author already exists, map the source (book) ID to the existing author on the Drupal 8 side:

$this->idMap->saveIdMapping($row, ['author_id' => $existing_author_id], MigrateIdMapInterface::STATUS_IGNORED);
return FALSE;

Then, the author's map table maps each book ID that shares an author to the same author, and the migration plugin in the book migration looks up that book ID and gets the single author record.

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.