Skip to content

Instantly share code, notes, and snippets.

@vladdancer
Created January 28, 2022 18:26
Show Gist options
  • Save vladdancer/5ad9581bcd26a1690aa73a1e215f2ed3 to your computer and use it in GitHub Desktop.
Save vladdancer/5ad9581bcd26a1690aa73a1e215f2ed3 to your computer and use it in GitHub Desktop.
Add extra source records to migrate #drupal8 #drupal9 #migrate
<?php
final class Redirect extends SqlBase {
/**
* Return list of additional redirects that doesn't exist in source db.
*
* Some redirects are not in db, but we need to add them.
*
* @return array[]
*/
private function getExtraRedirects(): array {
return [
[
// Let's use "virtual" rid, physical rids ends on 1800.
'rid' => 2000,
'redirect_source__path' => 'visitor',
'redirect_redirect__uri' => 'internal:/node/1111',
'status_code' => 301,
// Let's mark this is a new redirect.
'created' => time(),
'migrate_map_sourceid1' => null,
'migrate_map_source_row_status' => null
],
[
// Let's use "virtual" rid, physical rids ends on 1800.
'rid' => 2001,
'redirect_source__path' => 'view',
'redirect_redirect__uri' => 'internal:/node/2222',
'status_code' => 301,
// Let's mark this is a new redirect.
'created' => time(),
'migrate_map_sourceid1' => null,
'migrate_map_source_row_status' => null
]
];
}
/**
* Add extra redirects from static list into db results.
*/
protected function initializeIterator(): \Iterator {
$iterator = parent::initializeIterator();
$extraRedirects = new \ArrayObject($this->getExtraRedirects());
$extraItemsIterator = new \IteratorIterator($extraRedirects);
$allRedirectsIterator = new \AppendIterator();
$allRedirectsIterator->append($iterator);
$allRedirectsIterator->append($extraItemsIterator);
return $allRedirectsIterator;
}
/**
* Make records counting work with our extra items.
*/
protected function doCount(): int {
$count = parent::doCount();
return $count + count($this->getExtraRedirects());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment