I am migrating content from Joomla to Drupal and need to pull one img tag out of intro and body fields, then use the src attribute value of the img tag to populate an image field on the node in Drupal. Everything is working except creating the image. There are no errors thrown, the image field is just empty.

I have read through every issue that contains the word 'image', I have looked at both examples. I have tried numerous declarations and have checked permissions. I can't get it to work.

In prepareRow, I check if an image exists in the intro. If not, then I use the first image in the body.

public function prepareRow($row) {
    $intro = $row->introtext;
    $body = $row->body;
    $image_in_intro = FALSE;

    if(!empty($intro)) {
      //Remove image fields and create object for use by import maps
      $html_obj = new simple_html_dom();
      $html_obj->load($intro);
      $image = $html_obj->find('img', 0);

      if ($image) {
        $image_in_intro = TRUE;
        $original_src = $image->attr['src'];
        $src = str_replace('images/stories/', 'field/image/import/', $original_src);

        //remove image from text
        $html_obj->find('img', 0)->outertext='';
        $str = $html_obj->save();
        $row->introtext = $str;
        $intro = $str;

        $row->url[] = $src;
        $row->image_alt[] = $image->attr['alt'];
        $row->image_title[] = $image->attr['title'];
      }

      // destroy object
      $html_obj->clear();
      unset($html_obj);
    }

    if(!empty($body)) {
      $html_obj = new simple_html_dom();
      $html_obj->load($body);
      $image = $html_obj->find('img', 0);

      if ($image) {
        if(!$image_in_intro) {
          $original_src = $image->attr['src'];
          $src = str_replace('images/stories/', 'field/image/import/', $original_src);
          $row->url[] = $src;
          $row->image_alt[] = $image->attr['alt'];
          $row->image_title[] = $image->attr['title'];
        }

        //remove image from text
        $html_obj->find('img', 0)->outertext='';
        $str = $html_obj->save();
        $body = $str;
      }

      // destroy object
      $html_obj->clear();
      unset($html_obj);
    }

    //Join intro and body fields to create full body text
    $row->body = $intro . $body;
  }

And here is the relevant code from my class, which extends an abstract class JoomlaMigration that itself extends DynamicMigration

    $this->addFieldMapping('field_image', 'url');
    $this->addFieldMapping('field_image:file_class')
      ->defaultValue('MigrateFileUri');
    $this->addFieldMapping('field_image:source_dir')
      ->defaultValue('public://');
    $this->addFieldMapping('field_image:destination_dir')
      ->defaultValue('public://');
    $this->addFieldMapping('field_image:alt', 'image_alt');
    $this->addFieldMapping('field_image:title', 'image_title');

My understanding is that 'field_image:source_dir' combined with 'field_image' should point
to the directory containing the image I want attached to the field on the node. In my case,
the 'url' value of row (which is created in prepareRow) holds a path to the image which will be inside a sub directory of 'field/image/import/'. As an example, my test value is 'field/image/import/tech/groves/hmyu78.jpg'. So it is that value 'field/image/import/tech/groves/hmyu78.jpg' which is the value of 'field_image'. So, 'field_image:source_dir' joined with 'field_image' would produce:

public://field/image/import/tech/groves/hmyu78.jpg

I wonder if

        $row->url[] = $src;
        $row->image_alt[] = $image->attr['alt'];
        $row->image_title[] = $image->attr['title'];

in prepareRow actually does what I intend, which is to make url, image_alt, image_title properties available to the migrations class? Since the image field seems to be ignored, I wonder if those values are lost between prepareRow and the class.

Thank you, in advance, for your help.

Comments

bletch’s picture

Issue summary: View changes
bletch’s picture

Issue summary: View changes
bletch’s picture

Issue summary: View changes
bletch’s picture

Issue summary: View changes
bletch’s picture

I've implemented the following in the complete hook so that I could get this import done. But it is not ideal, am still interested to know how to make this work within the class instead of calling another hook and messing with node_save a second time.

  public function complete($entity, stdClass $row) {
    if (!empty($row->url[0])) {
      $path = './sites/default/files/' . $row->url[0];
      $filename = basename($path);
      $file_temp = file_get_contents($path);
      $file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);
      $entity->field_image = array(
        'und' => array(
          0 => array(
            'fid' => $file_temp->fid,
            'filename' => $file_temp->filename,
            'filemime' => $file_temp->filemime,
            'uid' => 1,
            'uri' => $file_temp->uri,
            'status' => 1,
            'alt' => $row->image_alt[0],
            'title' => $row->image_title[0],
          )
        )
      );

      node_save($entity);
    }
  }
mikeryan’s picture

Category: Bug report » Support request
Status: Active » Postponed (maintainer needs more info)

Where exactly do the Joomla files reside? Normally you would set source_dir to point at the source site, e.g. http://www.example.com/files - 'public://' means to look in Drupal's files directory.

mikeryan’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)