You are here

protected static function MigrateDestinationMedia::replaceCallback in Migrate Extras 7.2

If a referenced image can be found in the files table, replace the <img> tag with a media JSON reference.

Parameters

array $matches:

File

./media.inc, line 42

Class

MigrateDestinationMedia
Destination class implementing migration into media entities.

Code

protected static function replaceCallback(array $matches) {
  $src_matches = array();

  // Default to the original <img> tag
  $result = $matches[0];

  // Extract the src parameter
  if (preg_match('|src=[\'"]([^\'"]+)[\'"]|i', $matches[0], $src_matches)) {

    // Replace the scheme and host portions of the referenced URI with the
    // Drupal scheme as it's saved in the file_unmanaged table
    $src = $src_matches[1];
    $fid = db_select('file_managed', 'f')
      ->fields('f', array(
      'fid',
    ))
      ->condition('filename', basename($src))
      ->execute()
      ->fetchField();
    if ($fid) {
      $image_info = array(
        'type' => 'media',
        'view_mode' => 'media_large',
        'fid' => $fid,
        'attributes' => array(
          'alt' => '',
          'title' => '',
          'class' => 'media-image',
          'typeof' => 'foaf:Image',
          'wysiwyg' => 1,
        ),
      );

      // Get the height and width parameters if present
      preg_match('|width=[\'"]([^\'"]+)[\'"]|i', $matches[0], $width);
      preg_match('|height=[\'"]([^\'"]+)[\'"]|i', $matches[0], $height);

      // image width
      if ($width) {
        $image_info['attributes']['width'] = $width[1];
      }

      // image height
      if ($height) {
        $image_info['attributes']['height'] = $height[1];
      }
      $result = '[[' . drupal_json_encode($image_info) . ']]';
    }
  }
  return $result;
}