Skip to content

Instantly share code, notes, and snippets.

@rosemarystanley
Last active February 19, 2021 12:45
Show Gist options
  • Save rosemarystanley/a1db2773baf14c78d0d44df4ab5810b8 to your computer and use it in GitHub Desktop.
Save rosemarystanley/a1db2773baf14c78d0d44df4ab5810b8 to your computer and use it in GitHub Desktop.
Drupal 7 Scald Atom Source Plugin for Drupal 8 Migration
<?php
namespace Drupal\your_module\Plugin\migrate\source;
use Drupal\Core\Database\Query\Condition;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* Processes Scald Media items to Drupal 8 Media entities.
*
* @MigrateSource(
* id = "d7_scald_atom",
* source_provider = "scald",
* source_module = "scald",
* )
*/
class ScaldAtom extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('scald_atoms', 's')
->fields('s', array(
'sid',
'provider',
'type',
'base_id',
'language',
'publisher',
'actions',
'title',
'data',
'created',
'changed',
'publisher',
));
if (isset($this->configuration['atom_type'])) {
if ($this->configuration['atom_type'] === 'image') {
$query->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'
));
$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');
} else if ($this->configuration['atom_type'] === 'video') {
$query->fields('cred', array(
'field_credit_value'
))
->fields('cap', array(
'field_caption_long_value'
))
->fields('cop', array(
'field_copyright_value'
));
$query->leftJoin('field_data_field_credit', 'cred', 'cred.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');
} else if ($this->configuration['atom_type'] === 'file') {
// Exclude JS files for now.
$query->fields('fm', array(
'filemime',
'uri',
'uid'
));
$query->leftJoin('file_managed', 'fm', 'fm.fid = s.base_id');
$query->condition('fm.filemime', ['application/octet-stream', 'application/x-shockwave-flash', 'application/x-javascript', 'text/css', 'application/zip', 'text/html', 'application/xml', 'text/x-component', 'text/calendar', 'application/vnd.google-earth.kmz'], 'NOT IN');
}
$query->condition('s.type', $this->configuration['atom_type']);
}
if (isset($this->configuration['provider'])) {
$query->condition('s.provider', $this->configuration['provider']);
}
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$sid = $row->getSourceProperty('sid');
$type = $row->getSourceProperty('type');
// Get Field API field values.
foreach (array_keys($this->getFields('scald_atoms', $type)) as $field) {
$row->setSourceProperty($field, $this->getFieldValues('scald_atoms', $field, $sid));
}
if ($type === 'audio') {
$data = unserialize($row->getSourceProperty('data'));
$row->setSourceProperty('permalink_url', isset($data['permalink_url']) ? $data['permalink_url'] : 'somthing');
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return array(
'sid' => $this->t('Scald Atom ID'),
'provider' => $this->t('Provider module name'),
'type' => $this->t('Scald Atom type'),
'base_id' => $this->t('Scald Atom base ID'),
'language' => $this->t('Scald Atom language'),
'publisher' => $this->t('Scald Atom publisher (User ID)'),
'actions' => $this->t('Available Scald actions'),
'title' => $this->t('Scald Atom title'),
'data' => $this->t('Scald Atom data'),
'created' => $this->t('Created timestamp'),
'changed' => $this->t('modified timestamp'),
'scald_thumbnail_alt' => $this->t('Alternative text for images'),
'field_credit_value' => $this->t('Credit text value'),
'field_photo_credit_url_url' => $this->t('photo credit text value'),
'field_caption_long_value' => $this->t('Caption text value'),
'field_copyright_value' => $this->t('Copyright text value'),
'publisher' => $this->t('the user id'),
'uri' => $this->t('File uri'),
);
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['sid']['type'] = 'integer';
$ids['sid']['alias'] = 's';
return $ids;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment