diff --git a/src/DataParserPluginBase.php b/src/DataParserPluginBase.php index 3e8fd47a2cd8d1895cdc7f42779243b5066b85cf..7d187f31ddc662c203e5307225edd23da0dc0c82 100644 --- a/src/DataParserPluginBase.php +++ b/src/DataParserPluginBase.php @@ -181,6 +181,15 @@ abstract class DataParserPluginBase extends PluginBase implements DataParserPlug return $this->currentItem; } + /** + * {@inheritdoc} + */ + public function currentUrl(): ?string { + $index = $this->activeUrl ?: \array_key_first($this->urls); + + return $this->urls[$index] ?? NULL; + } + /** * {@inheritdoc} */ diff --git a/src/DataParserPluginInterface.php b/src/DataParserPluginInterface.php index 838ca9d3631b62c9a36f93ab810c237775343375..fb6ed69ba31312a162e3816651473c3b16563b4b 100644 --- a/src/DataParserPluginInterface.php +++ b/src/DataParserPluginInterface.php @@ -12,4 +12,12 @@ namespace Drupal\migrate_plus; */ interface DataParserPluginInterface extends \Iterator, \Countable { + /** + * Returns current source URL. + * + * @return string|null + * The URL currently parsed on success, otherwise NULL. + */ + public function currentUrl(): ?string; + } diff --git a/tests/data/simple_xml_current_url1.xml b/tests/data/simple_xml_current_url1.xml new file mode 100644 index 0000000000000000000000000000000000000000..0cffd2890844911620445a73e0c9dc815d238fce --- /dev/null +++ b/tests/data/simple_xml_current_url1.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<items> + <item id="1"> + <values title="Values"> + <value>Value 1</value> + <value>Value 2</value> + </values> + </item> + <item id="2"> + <values title="Values"> + <value>Value 1 (single)</value> + </values> + </item> +</items> diff --git a/tests/data/simple_xml_current_url2.xml b/tests/data/simple_xml_current_url2.xml new file mode 100644 index 0000000000000000000000000000000000000000..6d4b8e0ae7af63c49300618b68ce47674eb6ccc1 --- /dev/null +++ b/tests/data/simple_xml_current_url2.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<items> + <item id="3"> + <values title="Values"> + <value>Value 1</value> + <value>Value 2</value> + </values> + </item> +</items> diff --git a/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php b/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php index 6505075ae1ba214be376f062b38b1bfd841193c5..77ab9768307f0cdd8da1e0cf9590458f96c04354 100644 --- a/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php +++ b/tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php @@ -86,6 +86,29 @@ class SimpleXmlTest extends KernelTestBase { ]; } + /** + * Tests current URL of parsed XML item. + */ + public function testCurrentUrl() { + $urls = [ + $this->path . '/tests/data/simple_xml_current_url1.xml', + $this->path . '/tests/data/simple_xml_current_url2.xml', + ]; + $this->configuration['urls'] = $urls; + /** @var \Drupal\migrate_plus\DataParserPluginInterface $parser */ + $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); + + // First 2 items available in the first URL. + $parser->rewind(); + $this->assertEquals($urls[0], $parser->currentUrl()); + $parser->next(); + $this->assertEquals($urls[0], $parser->currentUrl()); + + // Third item available in the second URL. + $parser->next(); + $this->assertEquals($urls[1], $parser->currentUrl()); + } + /** * Tests reducing single values. */