1

I have the following case: I'm trying to migrate Drupal 6 profile pictures into Drupal 8 file_managed table. All goes fine except that I'm unable to set new destination URI for column file_managed.uri.

The old picture is stored as full path to file in column users.picture as following: "sites/default/files/pictures/picture1.jpg".

However, I want to change it when importing into the D8 site.

I tried using $row->setDestinationProperty method, but it fails to register and the migration continues as usual and I just get full image location instead.

So the question: How can I set the new destination URI during migration?

Full code:

 <?php

/**
 * @file
 * Contains \Drupal\md_migrate\Plugin\migrate\source\UserProfilePicture.
 */

namespace Drupal\md_migrate\Plugin\migrate\source;

use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;

/**
 * Extract files from database.
 *
 * @MigrateSource(
 *   id = "md_migrate_user_profile_picture"
 * )
 */
class UserProfilePicture extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    return $this->select('users', 'u')
      ->fields('u', array('picture'))
      ->condition('u.picture', '', '!=')
      ->orderBy('u.uid', 'ASC')
      ->range(0,1);

  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields['picture'] = $this->t('File path');
    return $fields;
  }



  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return array(
      'picture' => array(
        'type' => 'string',
        'alias' => 'u',
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    parent::prepareRow($row);
    // Get filename property value.
    $filename = $row->getSourceProperty('picture');

    $new_filename = explode('/', $filename);
    $new_filename2 = end($new_filename);
    // Prepare base path.
    $base_path = trim($this->configuration['source_base_path'], '/');
    // Set "full_path" property to point to the full path of the image.
    $base_module_path = drupal_get_path('module', 'md_migrate');
    $row->setSourceProperty('full_path', $base_module_path . '/' . $base_path . '/' . $filename);
    $row->setDestinationProperty('uri', 'public://pictures/'.$new_filename2);
  }


}

Your Answer

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

Browse other questions tagged or ask your own question.