Created
May 22, 2023 13:52
-
-
Save danielmme/064133ccb1eefc787ddc5dc4967d8a1c to your computer and use it in GitHub Desktop.
Drupal 7 course variation source
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\migrate\Plugin\migrate\source; | |
use Drupal\migrate\Row; | |
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity; | |
use Drupal\paragraphs\Plugin\migrate\field\FieldCollection; | |
/** | |
* Drupal 7 course variation source. | |
* | |
* @MigrateSource( | |
* id = "course_variation", | |
* source_module = "node" | |
* ) | |
*/ | |
class CourseVariation extends FieldableEntity { | |
/** | |
* The join options | |
*/ | |
const JOIN1 = '[n].[nid] = [f].[entity_id]'; | |
/** | |
* The join options | |
*/ | |
const JOIN2 = '[fc].[item_id] = [f].[field_course_variation_value]'; | |
/** | |
* The join options | |
*/ | |
const JOIN3 = '[fcr].[revision_id] = [f].[field_course_variation_revision_id]'; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function query() { | |
$query = $this->select('node', 'n'); | |
$query->innerJoin('field_data_field_course_variation', 'f', static::JOIN1); | |
$query->innerJoin('field_collection_item', 'fc', static::JOIN2); | |
$query->innerJoin('field_collection_item_revision', 'fcr', static::JOIN3); | |
$query->condition('n.type', 'course', '=') | |
->fields('n', ['nid', 'title', 'created', 'changed', 'uid']) | |
->fields('f', ['field_course_variation_value', 'field_course_variation_revision_id', 'delta', 'entity_id']) | |
->fields('fc', ['item_id']) | |
->fields('fcr', ['revision_id']); | |
return $query; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function prepareRow(Row $row) { | |
// Remove field_ prefix for new bundle. | |
$bundle = 'field_course_variation'; | |
$bundle = substr($bundle, FieldCollection::FIELD_COLLECTION_PREFIX_LENGTH); | |
$row->setSourceProperty('bundle', $bundle); | |
// Get Field API field values. | |
$field_names = array_keys($this->getFields('field_collection_item', 'field_course_variation')); | |
$item_id = $row->getSourceProperty('item_id'); | |
$revision_id = $row->getSourceProperty('revision_id'); | |
foreach ($field_names as $field_name) { | |
$value = $this->getFieldValues('field_collection_item', $field_name, $item_id, $revision_id); | |
$row->setSourceProperty($field_name, $value); | |
} | |
return parent::prepareRow($row); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function fields() { | |
$fields = [ | |
'nid' => $this->t('Node ID'), | |
'type' => $this->t('Type'), | |
'title' => $this->t('Title'), | |
'uid' => $this->t('Node authored by (uid)'), | |
'created' => $this->t('Created timestamp'), | |
'field_course_variation_value' => $this->t('Id course variation'), | |
'field_course_variation_revision_id' => $this->t('Revision Id course variation'), | |
'entity_id' => $this->t('Nid'), | |
'item_id' => $this->t('Item_id'), | |
'revision_id' => $this->t('Revision_id'), | |
]; | |
return $fields; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIds() { | |
$ids['item_id']['type'] = 'integer'; | |
$ids['item_id']['alias'] = 'n'; | |
return $ids; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment