There are still 17k sites using img_assist, and there's no upgrade path (image.module still doesn't have one), so it'd probably be incredibly helpful for a lot of people to have a migration directly from an img_assist-based site straight to a media.module-based site. I was wondering how hard it would be to implement this conversion as part of a mapping?

img_assist tags look like:

[img_assist|nid=49|title=|desc=|link=none|align=middle|width=500|height=526]

while media module tags look like this (link tag and style attributes added by CKeditor in WYSIWYG API):

<a href="blah">[[{"type":"media","view_mode":"media_large","fid":"13","attributes":{"alt":"","class":"media-image","style":"width: 349px; height: 480px; float: right;","typeof":"foaf:Image"}}]]</a>

Some of this is probably pretty straight forward and I can just rip the parsing/encoding code directly from the associated modules. But the file id would obviously depend on the migration from image.module to what ever other storage (I was assuming a similar setup - "image" content type with image field).

I will have a look further into it and re-open, but as I haven't jumped into migrate.module much yet, I'd be interested in any advice anyone can give.

Comments

mikeryan’s picture

Project: Migrate » Migrate Extras
Component: Code » Media
Category: support » feature

The right place to add this would be in Migrate Extra's media module support.

mariagwyn’s picture

I would love to hear if anyone has made progress on this.
Maria

akejoha’s picture

Desperately needed..

albertczyk’s picture

media.inc file has a pretty good example that I've modified to do exactly what's described here. This is a working example, but I cannot get the JSON markup translated into HTML, so when displaying the node is shown in source format:

[[{"type":"media","view_mode":"media_large","fid":"410","attributes":{"alt":"En la casa-cuna Llar Natalis. Tarragona","title":"En la casa-cuna Llar Natalis. Tarragona","class":"media-image","typeof":"foaf:Image","wysiwyg":1}}]]

It must be something silly, like a missing module or so... Does anybody have a clue?


  public function prepare($entity, stdClass $row) {
     $this->RewriteImgTags($entity);
  }

  static public function RewriteImgTags($entity, $field = 'body') {
    if (is_array($entity->$field)) {
      foreach ($entity->$field as $language => $values) {
        $body = $values[0]['value'];
        break;
      }
      // Quickly skip any non-candidates

      if (!stristr($body, 'img_assist')) {
        return;
      }

      // Pass full img tags into the callback
      $new_body = preg_replace_callback('|\[img_assist[^\]]*\]|i', array(self, 'replaceCallback'),
        $body);

      $entity->{$field}[$language][0]['value'] = $new_body;
    }
  }

  static protected function replaceCallback(array $matches) {
    $param_str = substr($matches[0],1,strlen($matches[0])-2); // Remove [..]
    $src_matches = explode('|', $param_str);
    $img_params = array();
// [img_assist|nid=46865|title=Manifestaciones y fiestas tradicionales|desc=|link=popup|align=center|width=400|height=271]
    foreach ( $src_matches as $pair ) 
       if ( strpos($pair,'=') ) {
          list($param, $value) = explode('=',$pair);
          $img_params[$param]=$value;
       }

  
   $fid = db_select('migrate_map_imagen', 'mmi')
             ->fields('mmi', array('destid1'))
             ->condition('sourceid1', $img_params['nid'])
             ->execute()
             ->fetchField();

   if ($fid) {
        $image_info = array(
          'type' => 'media',
          'view_mode' => 'media_large',
          'fid' => $fid,
          'attributes' => array(
            'alt' => $img_params['title'],
            'title' => $img_params['title'],
            'class' => 'media-image',
            'typeof' => 'foaf:Image',
            'wysiwyg' => 1,
            /*
            'width' => $img_params['width'],
            'height' => $img_params['height'],
            'align' => $img_params['align'],
             */
          ),
        );
        $result = '<a href="">[[' . drupal_json_encode($image_info) . ']]</a>';
    }
    return $result;
  }

albertczyk’s picture

Yes, it was really silly: forgot to check "Convert Media tags to markup" in text formats config...

mikeryan’s picture

Status: Postponed » Active

The place to do this would be MigrateDestinationMedia::rewriteImgTags(), add a line like:

      $new_body = preg_replace_callback('/\[image_assist|[^\]]*\]/i', array(self, 'replaceImgassistCallback'),
        $new_body);

after the existing preg_replace_callback call.

Implementation of replaceImgassistCallback is left as an exercise to the reader, patches welcome;).

mikeryan’s picture

Issue summary: View changes

formatting