class MigrateDestinationMedia in Migrate Extras 7.2
Destination class implementing migration into media entities.
Hierarchy
- class \MigrateDestination
- class \MigrateDestinationEntity
- class \MigrateDestinationFile
- class \MigrateDestinationMedia
- class \MigrateDestinationFile
- class \MigrateDestinationEntity
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateDestination:: |
protected | property | Maintain stats on the number of destination objects created or updated. | |
MigrateDestination:: |
protected | property | ||
MigrateDestination:: |
public | function | ||
MigrateDestination:: |
public | function | ||
MigrateDestination:: |
public | function | Reset numCreated and numUpdated back to 0. | |
MigrateDestinationEntity:: |
protected | property | The bundle (node type, vocabulary, etc.) of the destination. | |
MigrateDestinationEntity:: |
protected | property | The entity type (node, user, taxonomy_term, etc.) of the destination. | |
MigrateDestinationEntity:: |
protected | property | Default language for text fields in this destination. | |
MigrateDestinationEntity:: |
protected | property | Default input format for text fields in this destination. | |
MigrateDestinationEntity:: |
public static | function | Flattens an array of allowed values. | |
MigrateDestinationEntity:: |
public | function | Give handlers a shot at modifying the object (or taking additional action) after saving it. | |
MigrateDestinationEntity:: |
public | function | Give handlers a shot at cleaning up after an entity has been rolled back. | |
MigrateDestinationEntity:: |
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:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | ||
MigrateDestinationEntity:: |
public | function | Give handlers a shot at modifying the object before saving it. | |
MigrateDestinationEntity:: |
public | function | Give handlers a shot at cleaning up before an entity has been rolled back. | |
MigrateDestinationEntity:: |
public | function |
Derived classes must implement __toString(). Overrides MigrateDestination:: |
|
MigrateDestinationFile:: |
protected | property | File class (MigrateFileUri etc.) doing the dirty wrk. | |
MigrateDestinationFile:: |
protected | property | Boolean indicating whether we should avoid deleting the actual file on rollback. | |
MigrateDestinationFile:: |
public | function |
Returns a list of fields available to be mapped for the entity type
(bundle) Overrides MigrateDestination:: |
|
MigrateDestinationFile:: |
protected | function | Delete database references to a file without deleting the file itself. | |
MigrateDestinationFile:: |
public static | function | Implementation of MigrateDestination::getKeySchema(). | |
MigrateDestinationFile:: |
public | function |
Import a single file record. Overrides MigrateDestination:: |
|
MigrateDestinationFile:: |
public | function | Delete a file entry. | |
MigrateDestinationFile:: |
public | function | ||
MigrateDestinationFile:: |
public | function |
Basic initialization Overrides MigrateDestinationEntity:: |
|
MigrateDestinationMedia:: |
protected static | function | If a referenced image can be found in the files table, replace the <img> tag with a media JSON reference. | |
MigrateDestinationMedia:: |
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. |