Created
May 13, 2021 21:40
-
-
Save chriscalip/69b613f44eda3b757262a0b2f77264ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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\rl_migrates\Plugin\migrate\source; | |
use Drupal\migrate_drupal\Plugin\migrate\source\ContentEntity; | |
/** | |
* ContentEntity migration source that accepts multiple bundles. | |
* | |
* This is a stop gap measure for: | |
* https://www.drupal.org/project/drupal/issues/2746541?page=1#comment-13715441 | |
* | |
* @MigrateSource( | |
* id = "bundles_kontent_entity", | |
* source_module = "migrate_drupal", | |
* deriver = "\Drupal\migrate_drupal\Plugin\migrate\source\ContentEntityDeriver", | |
* ) | |
*/ | |
class BundlesKontentEntity extends ContentEntity { | |
/** | |
* Query to retrieve the entities. | |
* | |
* @return \Drupal\Core\Entity\Query\QueryInterface | |
* The query. | |
*/ | |
public function query() { | |
$query = $this->entityTypeManager | |
->getStorage($this->entityType->id()) | |
->getQuery() | |
->accessCheck(FALSE); | |
if (!empty($this->configuration['bundles'])) { | |
$query->condition($this->entityType->getKey('bundle'), $this->configuration['bundles'], 'IN'); | |
} | |
if (!empty($this->configuration['include_allrevisions'])) { | |
$query->allRevisions(); | |
} | |
return $query; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIds() { | |
$id_key = $this->entityType->getKey('id'); | |
$ids[$id_key] = $this->getDefinitionFromEntity($id_key); | |
if ($this->entityType->isRevisionable()) { | |
$revision_key = $this->entityType->getKey('revision'); | |
$ids[$revision_key] = $this->getDefinitionFromEntity($revision_key); | |
} | |
if ($this->entityType->isTranslatable()) { | |
$langcode_key = $this->entityType->getKey('langcode'); | |
$ids[$langcode_key] = $this->getDefinitionFromEntity($langcode_key); | |
} | |
return $ids; | |
} | |
/** | |
* Initializes the iterator with the source data. | |
* | |
* @return \Generator | |
* A data generator for this source. | |
*/ | |
protected function initializeIterator() { | |
$ids = $this->query()->execute(); | |
$highWater = $this->getHighWater(); | |
$highWaterProperty = $this->getHighWaterProperty(); | |
$highWaterField = $this->getHighWaterField(); | |
$shouldUseHighWater = !empty($highWaterProperty); | |
$targetVariable = (in_array($highWaterField, ['revision_id', 'vid'])) ? 'key' : 'element'; | |
$parsedIds = ($shouldUseHighWater) ? array_filter($ids, function ($key, $element) use ($highWater, $targetVariable) { | |
$target = ${$targetVariable}; | |
$isHigher = $highWater <= $target; | |
return $isHigher; | |
}, ARRAY_FILTER_USE_BOTH) : $ids; | |
$outputYield = (!empty($this->configuration['include_allrevisions']) && method_exists($this, 'yieldEntityRevisions')) ? $this->yieldEntityRevisions($parsedIds) : $this->yieldEntities($parsedIds); | |
return $outputYield; | |
} | |
/** | |
* Loads and yields entities, one at a time. | |
* | |
* @param array $ids | |
* The entity IDs. | |
* | |
* @return \Generator | |
* An iterable of the loaded entities. | |
*/ | |
protected function yieldEntities(array $ids) { | |
$storage = $this->entityTypeManager | |
->getStorage($this->entityType->id()); | |
foreach ($ids as $id) { | |
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ | |
$entity = $storage->load($id); | |
yield $this->toArray($entity); | |
if ($this->configuration['include_translations']) { | |
foreach ($entity->getTranslationLanguages(FALSE) as $language) { | |
yield $this->toArray($entity->getTranslation($language->getId())); | |
} | |
} | |
} | |
} | |
/** | |
* Loads and yields entities-revision sensitive., one at a time. | |
* | |
* @param array $ids | |
* The entity vid and entity ids. | |
* | |
* @return \Generator | |
* An iterable of the loaded entities. | |
*/ | |
protected function yieldEntityRevisions(array $ids) { | |
$storage = $this->entityTypeManager | |
->getStorage($this->entityType->id()); | |
foreach ($ids as $vid => $id) { | |
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ | |
$entity = $storage->loadRevision($vid); | |
yield $this->toArray($entity); | |
if ($this->configuration['include_translations']) { | |
foreach ($entity->getTranslationLanguages(FALSE) as $language) { | |
yield $this->toArray($entity->getTranslation($language->getId())); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment