- Issue created by @apotek
- Status changed to Needs review
11 months ago 5:39am 20 January 2024
According to MigrateIdMapInterface::getRowBySource() must always return an array.
Accordingly, in migrate_tools's IdMapFilter class, we define the function like so:
public function getRowBySource(array $source_id_values): array {}
However, our function wraps core Migrate module's Sql::getRowBySourceId(), which also implements the same interface but does not follow the contract, and may in fact return a boolean, in cases where a lookup fails.
public function getRowBySource(array $source_id_values) {
...
...
...
return $result->fetchAssoc();
}
fetchAssoc, as we know, can return FALSE or an array.
Which means that when IdMapFilter::getRowBySource() does this:
// ($map is under some circumstances, Drupal\migrate\Plugin\migrate\id_map\Sql)
return $map->getRowBySource($source_id_values);
our migrate_tools IdMapFilter::getRowBySource() can attempt to return a boolean, which it is not allowed to do, thus the TypeError.
While the bug actually lies in the migrate module (and I will open an issue there), since migrate is a core module, and it might take a long while if ever for a patch to be approved there, we might do well to make our implementation handle the devilry itself.
Propose something like:
public function getRowBySource(array $source_id_values): array {
$map = $this->getInnerIterator();
\assert($map instanceof MigrateIdMapInterface);
// Handle the fact that currently
// Drupal\migrate\Plugin\migrate\id_map\Sql:: getRowBySource()
// may possibly return FALSE.
- return $map->getRowBySource($source_id_values);
+ return $map->getRowBySource($source_id_values) ?? [];
}
Call IdMapFilter::getRowBySource() with a known non-existent source id:
$migrate_executable->getIdMap()->getRowBySource($key);
I wrapped this call in a try{} catch (\TypeError) {} which helped.
Convert any returned FALSE from Drupal\migrate\Plugin\migrate\id_map\Sql::getRowById() to an array before returning.
return $map->getRowBySource($source_id_values) ?? [];
Open a merge request with the suggested change.
None
None
None
Needs review
6.0
Code