geerlingguy / jeffgeerling-com

Drupal Codebase for JeffGeerling.com
https://www.jeffgeerling.com
GNU General Public License v2.0
41 stars 2 forks source link

Update content to use 'sites/default/files' instead of 'sites/jeffgeerling.com/files' #88

Closed geerlingguy closed 4 years ago

geerlingguy commented 4 years ago

I just realized that all the embedded images on the site are pointing to sites/jeffgeerling.com/files (for example the image embedded in http://localhost/blog/2020/i-gave-away-my-books-free-and-sales-increased-4x). I need to make sure the migration accounts for that and rewrites all the content to make it sites/default/files instead. Probably in custom.module.

geerlingguy commented 4 years ago

In the custom module, it's kinda hacky, but it works:

/**
 * Implements hook_migrate_prepare_row().
 */
function custom_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
  $migration_id = $migration->id();

  // Rewrite paths in node bodies.
  if (in_array($migration_id, ['upgrade_d7_node_blog_post', 'upgrade_d7_node_page', 'upgrade_d7_node_project'])) {
    $body = $row->getSourceProperty('body');
    $body[0]['value'] = str_replace('sites/jeffgeerling.com/files', 'sites/default/files', $body[0]['value']);
    $row->setSourceProperty('body', $body);
  }
}