You are here

class MigrateDestinationMedia in Migrate Extras 7.2

Destination class implementing migration into media entities.

Hierarchy

Expanded class hierarchy of MigrateDestinationMedia

File

./media.inc, line 6

View source
class MigrateDestinationMedia extends MigrateDestinationFile {

  /**
   * Call this from the prepare() method of a migration that contains media
   * image files, if you want to rewrite the IMG tags into media references.
   *
   * @param $entity
   *  Entity object being built.
   * @param $field
   *  Name of the text field within the entity to be modified. Defaults to 'body'.
   */
  public static function rewriteImgTags($entity, $field = 'body') {
    if (is_array($entity->{$field})) {
      migrate_instrument_start('MigrateDestinationMedia rewriteImgTags');
      foreach ($entity->{$field} as $language => $values) {
        $body = $values[0]['value'];
        break;
      }

      // Quickly skip any non-candidates
      if (!stristr($body, '<img')) {
        migrate_instrument_stop('MigrateDestinationMedia rewriteImgTags');
        return;
      }

      // Pass full img tags into the callback
      $new_body = preg_replace_callback('|<img [^>]*>|i', array(
        self,
        'replaceCallback',
      ), $body);
      $entity->{$field}[$language][0]['value'] = $new_body;
      migrate_instrument_stop('MigrateDestinationMedia rewriteImgTags');
    }
  }

  /**
   * If a referenced image can be found in the files table, replace the <img> tag
   * with a media JSON reference.
   *
   * @param array $matches
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::array_flatten public static function Flattens an array of allowed values.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::fieldAttachValidate public static function Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString
MigrateDestinationFile::$fileClass protected property File class (MigrateFileUri etc.) doing the dirty wrk.
MigrateDestinationFile::$preserveFiles protected property Boolean indicating whether we should avoid deleting the actual file on rollback.
MigrateDestinationFile::fields public function Returns a list of fields available to be mapped for the entity type (bundle) Overrides MigrateDestination::fields
MigrateDestinationFile::fileDelete protected function Delete database references to a file without deleting the file itself.
MigrateDestinationFile::getKeySchema public static function Implementation of MigrateDestination::getKeySchema().
MigrateDestinationFile::import public function Import a single file record. Overrides MigrateDestination::import
MigrateDestinationFile::rollback public function Delete a file entry.
MigrateDestinationFile::setFileClass public function
MigrateDestinationFile::__construct public function Basic initialization Overrides MigrateDestinationEntity::__construct
MigrateDestinationMedia::replaceCallback protected static function If a referenced image can be found in the files table, replace the <img> tag with a media JSON reference.
MigrateDestinationMedia::rewriteImgTags public static function Call this from the prepare() method of a migration that contains media image files, if you want to rewrite the IMG tags into media references.