Last active
November 12, 2021 08:15
-
-
Save rosemarystanley/008628b975f4db3413c162a5a0ff3461 to your computer and use it in GitHub Desktop.
Process Plugin to capture Scald Atom embeds in Wysiwyg Fields to Media Entity Embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\your_module\Plugin\migrate\process; | |
use \Drupal\Core\Database\Database; | |
use \Drupal\file\Entity\File; | |
use \Drupal\media\Entity\Media; | |
use Drupal\migrate\MigrateExecutableInterface; | |
use Drupal\migrate\ProcessPluginBase; | |
use Drupal\migrate\Row; | |
use Drupal\paragraphs\Entity\Paragraph; | |
/** | |
* This processes text fields that contain scald embed codes in the wysiwyg. | |
* It converts them to drupal-media embed strings | |
* | |
* @MigrateProcessPlugin( | |
* id = "scald_wysiwyg_replace" | |
* ) | |
*/ | |
class ScaldWysiwyg extends ProcessPluginBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
return $this->query($value); | |
} | |
/** | |
* Checks for the existence of some value. | |
* | |
* @param mixed $value | |
* The body or text field value. | |
* | |
* @return mixed|null | |
* Entity id if the queried entity exists. Otherwise NULL. | |
*/ | |
protected function query($value) { | |
$sids = $this->getSIDs($value); | |
$media = $file = NULL; | |
foreach ($sids as $sid) { | |
$media = $this->getMedia($sid); | |
if ($media && $media->uuid->value) { | |
//Scaled Media Embed: | |
// <div class="dnd-atom-wrapper" data-scald-align="left" data-scald-context="thumbnail" data-scald-options="%7B%22hideCaption%22%3Afalse%2C%22usesSlideshow%22%3A%220%22%2C%22additionalClasses%22%3A%22%22%7D" data-scald-sid="1234" data-scald-type="image"><!-- scald embed --></div> | |
// Media Library Embed: | |
// <drupal-media data-entity-type="media" data-entity-uuid="0000000-0000-0000-0000-0000000000000" data-view-mode="thumbnail"></drupal-media> | |
$value = str_replace('<div class="dnd-atom-wrapper"', '<drupal-media data-entity-type="media" ', $value); | |
$value = str_replace('<!-- scald embed --></div>', '</drupal-media>', $value); | |
$value = str_replace('data-scald-align="', 'data-align="', $value); | |
$value = str_replace('data-scald-sid="' . $sid . '"', ' data-entity-uuid="' . $media->uuid->value . '"', $value); | |
// Replace scald-context (old view mode) with a specific new view-mode | |
$value = str_replace('data-scald-context="original_size_wysiwyg"', 'data-view-mode="wysiwyg_original"', $value); | |
} | |
} | |
return $value; | |
} | |
function getSIDs($value) { | |
$sids = []; | |
// Find matching markup for scald ids. Match them to our media field_sid | |
preg_match_all('/data-scald-sid="([0-9])*"/', $value, $matches); | |
if (isset($matches[0])) { | |
foreach ($matches[0] as $v) { | |
if (strpos($v, 'data-scald-sid') !== false) { | |
$sid = (int) str_replace(['data-scald-sid="', '"'], '', $v); | |
$sids[] = $sid; | |
} | |
} | |
} | |
return $sids; | |
} | |
function getMedia($sid) { | |
$media = $file = $fieldName = NULL; | |
// See if we already imported media with matching sid. | |
// We're using a field we created on migration but you could also query the migration_map_* table | |
$query = \Drupal::entityQuery('media'); | |
$query->condition('field_sid', $sid); | |
$entity_ids = $query->execute(); | |
if ($entity_ids && count($entity_ids) > 0) { | |
$media = Media::load(array_values($entity_ids)[0]); | |
} | |
return $media; | |
} | |
function getSidData($sid) { | |
// Find our media from the migrate db. | |
Database::setActiveConnection('migrate'); | |
$migrateDB = Database::getConnection(); | |
$query = $migrateDB->select('scald_atoms', 's') | |
->fields('s', array( | |
'sid', | |
'provider', | |
'type', | |
'base_id', | |
'language', | |
'publisher', | |
'actions', | |
'title', | |
'data', | |
'created', | |
'changed', | |
'publisher', | |
'data', | |
)) | |
->fields('st', array( | |
'scald_thumbnail_alt' | |
)) | |
->fields('cred', array( | |
'field_credit_value' | |
)) | |
->fields('url', array( | |
'field_photo_credit_url_url' | |
)) | |
->fields('cap', array( | |
'field_caption_long_value' | |
)) | |
->fields('cop', array( | |
'field_copyright_value' | |
)) | |
->fields('fm', array( | |
'uri' | |
)); | |
// Left Join to your own fields. Here's a few examples of what fields we are using. | |
$query->leftJoin('field_data_scald_thumbnail', 'st', 'st.entity_id = s.sid'); | |
$query->leftJoin('field_data_field_credit', 'cred', 'cred.entity_id = s.sid'); | |
$query->leftJoin('field_data_field_photo_credit_url', 'url', 'url.entity_id = s.sid'); | |
$query->leftJoin('field_data_field_caption_long', 'cap', 'cap.entity_id = s.sid'); | |
$query->leftJoin('field_data_field_copyright', 'cop', 'cop.entity_id = s.sid'); | |
$query->leftJoin('file_managed', 'fm', 'fm.fid = s.base_id'); | |
$query->condition('s.sid', $sid); | |
$scalds = $query->execute()->fetchAll(); | |
// Reset the connection to our default db. | |
Database::setActiveConnection(); | |
return $scalds && count($scalds) > 0 ? $scalds[0] : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment