- Issue created by @vistree
- π©πͺGermany vistree
I were able to solve this issue with a custom source plugin:
mymodule/src/Plugin/migrate/source/UrlWithActiveSourceUrl.php
<?php namespace Drupal\mymodule\Plugin\migrate\source; use Drupal\migrate_plus\Plugin\migrate\source\Url; use Drupal\migrate\Row; /** * Source plugin for retrieving data via URLs and add active URL as variable. * * @MigrateSource( * id = "url_with_active_source_url" * ) */ class UrlWithActiveSourceUrl extends Url { /** * {@inheritdoc} */ public function prepareRow(Row $row) { $migration = $this->migration->getSourcePlugin(); $dataParser = $migration->dataParserPlugin; $activeUrl = $dataParser->currentUrl(); $row->setSourceProperty('active_url', $activeUrl); return parent::prepareRow($row); } }
Now I can use in my migration config:
source: plugin: url_with_exhibit_url data_fetcher_plugin: http data_parser_plugin: json track_changes: true urls: - https://www.url1.de/data.json - https://www.url2.de/data.json - https://www.url3.de/data.json
And now within process, I can use "active_url" whereever needed (e.g.)
process: _active_source_url: active_url
- π©πͺGermany vistree
I found one problem with my implementation: if there is a source change, the last item BEFORE the source change already uses the new source-URL. Seems that $dataParser->currentUrl() does not return the correct item ;-(
Any idea? I can't access the property $dataParser->activeUrl or $dataParser->urls - as those are protected properties ;-( - π©πͺGermany vistree
As it looks we definitely have a problem using the $dataParser->currentUrl() inside a source plugin. As a solution for myself I now use a data_parser plugin - following @hctom's comment ( https://www.drupal.org/project/migrate_plus/issues/3050274#comment-13935165 β ):
1. Create a file within your custom module (mymodule/src/Plugin/migrate_plus/data_parser/CustomJson.php)
2. Add to CustomJson.php<?php namespace Drupal\mymodule\Plugin\migrate_plus\data_parser; use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json; /** * Obtain JSON data for migration. * * @DataParser( * id = "custom_json", * title = @Translation("JSON Parser for My Module") * ) */ class CustomJson extends Json { /** * {@inheritdoc} */ protected function fetchNextRow(): void { parent::fetchNextRow(); // Inject file metadata. if ($this->valid()) { $activeUrl = $this->currentUrl(); $activeUrlParts = explode('/api/', $activeUrl); $activeBaseUrl = str_replace('https://', 'https://username:userpassword@', $activeUrlParts[0]); $this->currentItem['remote_auth_url'] = $activeBaseUrl; $this->currentItem['active_source_url'] = $activeUrl; } } }
Now within your migration use:
source: plugin: url data_fetcher_plugin: http data_parser_plugin: omeka_json
Within the process section you will now be able to use "active_source_url" and other defined parameters.
process: _active_source_url: active_source_url