Migrate taxonomy images from Drupal 5

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

The following class was used to migrate taxonomy term images from Drupal 5's Taxonomy Image module into a proper Drupal 7 entity field. You'll want to adjust class and field names appropriately for your use case.

Note: Taxonomy Image in Drupal 5 did not make use of the files table, so you need to specify a path to your taxonomy images in the prepareRow() method.

class GWJTermImageMigration extends DrupalTerm5Migration {

  public function __construct(array $arguments) {
    $this->sourceFields['field_image'] = t('Constructed source image field');
    $this->sourceFields['field_image:title'] = t('Constructed source image title field');
    $this->sourceFields['field_image:alt'] = t('Constructed source image alt field');
    $this->sourceFields['field_image:source_dir'] = t('Constructed source image source directory field');
    $this->sourceFields['field_image:destination_dir'] = t('Constructed source image destination directory field');
    $this->sourceFields['field_image:destination_file'] = t('Constructed source image destination file field');
    parent::__construct($arguments);

    $this->addFieldMapping('field_image', 'field_image');
    $this->addFieldMapping('field_image:file_class')->defaultValue('MigrateFileUri');
    $this->addFieldMapping('field_image:title', 'field_image:title')->defaultValue('');
    $this->addFieldMapping('field_image:alt', 'field_image:alt')->defaultValue('');
    $this->addFieldMapping('field_image:language')->defaultValue(LANGUAGE_NONE);
    $this->addFieldMapping('field_image:source_dir', 'field_image:source_dir')->defaultValue('');
    $this->addFieldMapping('field_image:destination_dir', 'field_image:destination_dir')->defaultValue('');
    $this->addFieldMapping('field_image:destination_file', 'field_image:destination_file')->defaultValue('');
    $this->addFieldMapping('field_image:file_replace')->defaultValue(FILE_EXISTS_REPLACE);
    $this->addFieldMapping('field_image:preserve_files')->defaultValue(FALSE);
  }

  protected function query() {
    $query = parent::query();
    $query->leftJoin('term_image', 'ti', 'td.tid = ti.tid');
    $query->addField('ti', 'path', 'term_image_path');
    return $query;
  }

  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
       return FALSE;
    }
    $file_base_path = variable_get('gwj_migrate_file_path');
    if (!empty($row->term_image_path)) {
      $path = $file_base_path .'/'. preg_replace('/^files\//', '', $row->term_image_path);
      $pathinfo = pathinfo($path);
      $row->{'field_image'} = $pathinfo['basename'];
      $row->{'field_image:source_dir'} = $pathinfo['dirname'];
      $row->{'field_image:destination_dir'} = 'public://term_images';
      $row->{'field_image:destination_file'} = $pathinfo['basename'];
      $row->{'field_image:title'} = $row->name;
      $row->{'field_image:alt'} = $row->name;
    }
  }
}

Help improve this page

Page status: Not set

You can: