• Development

    Skip Empty Values During Concatenation in a Drupal 10 Migration

    UPDATE. You can do the same as below without the need for a custom module function. (source)


    This quick example shows one method to ignore empty values in a Drupal 10 migration concatenate process.

    Code

    Migrate code:

    Module code:

    Result

    Example data:

    Resulting field_address_full plain text value

     

  • Development

    Drupal Migrate API skip_on_multiple Process Plugin

    Here’s a Migrate API process plugin I wrote in Drupal 9 that skips a property or row when more than one value exists. My use case:

    1. My source data has locations; each location has multiple associated organizations
    2. My new Drupal site has locations; each location has a parent organization
    3. I want to only populate the field_parent_org field (an entity reference) if there is a single organization value in the source data for the location
    4. I’m using a custom module called “pdms_migrate”

    I’ve stripped out all of the noise from the examples below… hopefully it’s enough to help you understand the example:

  • Development

    Migrating Into Existing Nodes in Drupal 8, Including Rollback Missing from Source

    Please read this entire post (including the disclaimer at the bottom) before you put any of it to use.

    The site I’m basing this off had an existing set of nodes and a new migration that had the same nodes (and many more) in the data source. I wanted to map the existing nodes to their migration-based counterparts and treat every node as if it originated from the migration.

    As of today, using a few patches makes it easy (thanks contributors!) to rollback items that no longer exist in a D8 migration’s source data. See Migrate support for deleting items no longer in the incoming data and Implement rollback of items no longer in source data

    With these patches you can import and rollback in two commands:

    What if the data that you’re migrating (and rolling back if removed) already existed in Drupal before you started using the migration? If you attempt to migrate content, then rollback removed items, you will find that the items that no longer exist in the source will not be deleted if they existed before the migration.

    First, I should describe how I’m pulling data into existing nodes.

    This is as simple as populating the node ID in the process section of your migration YML, like this:

  • Development

    Modifying Rows During a Drupal 8 CSV Migration

    Migrate Source CSV is currently the source plugin of choice for doing a CSV-to-Drupal migration with the Migrate API in Drupal 8. In this post I will demonstrate how to manipulate the CSV data in realtime during the migrate:import operation. You can think of this as the equivalent to prepareRow() that you have seen elsewhere, like my blog post Extending the Migrate Plus JSON Parser in Drupal 8.

    Please make sure you have a working migration before you begin; it’ll make things easier to troubleshoot if you know you had a good starting point.

  • Development

    Migrating Specific Fields in a D8 Migration

    I recently needed a way to update the value of a single field of a D7 to D8 node migration.

    The client was already updating the migrated content so I had to be careful with what I migrated.

    The entity destination plugin has a wonderful configuration option that allows you to specify which fields you want to migrate. Search the code here for overwrite_properties.

    Here’s how I used this to ignore every field except two:

    I tested this by editing a node’s title, subtitle, field_tax_groups, and field_tags values. I ran the migration and the edits were preserved on the title and subtitle fields as expected.

  • Development

    Migrating a Drupal 7 User Profile2 Image Field to a Drupal 8 User Image Field

    Consider this scenario:

    • Drupal 7 site contains Profile2 profile called person with image field called field_oldsite_person_image.
      • Users each have a profile2 profile associated with their account.
    • Drupal 8 site has an image field called field_newsite_user_photo on the user entity itself.

    If you need to migrate the image field from the Drupal 7 profile to the Drupal 8 user entity you can extend the d7_user plugin and query for the additional field data. I’m executing an extra query for each user (because the code is in the prepareRow() method), rather than attempting to pull all of the profile data as part of the query in the query() method of the class.

    First, create a file in your module like /modules/custom/mysite_migrate/src/Plugin/migrate/source/MysiteUser.php that extends the d7_user plugin:

  • Development

    Quick Tips for a Drupal 7 to Drupal 8 Migration

    Here are a few quick tips for a Drupal 7 to Drupal 8 migration. I will add more over time; admittedly it’s pretty lame right now!

    For general Drupal 8 Migrate API tips, you may also want to check out my other post: Drupal 8 Migrate – Tips and Tricks

    Source Plugins

    If you’re using a source plugin like d7_user or d7_node it will help you understand what’s happening if you view the source code for the plugin. Pay close attention to the query(), prepareRow(), and fields() methods.

  • Development

    Only Migrating Published Nodes in a D7 to D8 Migration

    I’m not going into much detail here but hopefully this helps someone.

    If you need to migrate only published nodes you can extend the d7_node plugin and add a condition to the query.

    First, create a file in your module like /modules/custom/mysite_migrate/src/Plugin/migrate/source/MysiteNewsNode.php that extends the d7_node plugin:

  • Development

    Using GROUP_CONCAT to Combine Rows in a Drupal Query

    Recently I was working on a D7 to D8 migration. I was trying to import news items and their taxonomy terms (among many other things). To make things simple I wanted the query results to have (for each node) a single field that contained a comma-separated list of taxonomy terms which I could then explode during the row processing. Using GROUP_CONCAT we can achieve this! Let’s break it down:

    The Drupal 7 site has the following structure (focusing on the important bits for this blog post):

    The migration relies on the d7_node migrate source plugin, which basically queries for nodes of a specific type. The query object looks like this (simplified for this blog post):

  • Development

    Send email after Migrate Import in Drupal 8

    Here’s an example of a barebones event subscriber to send an email when a Drupal migration finishes importing. The success vs failure logic isn’t working correctly, but you’ll get the gist. If you come up with a solution for this please contact me; I don’t have time to work on it at the moment.

    The file structure looks like this:

    mymodule.services.yml