Implement the XML Xpath parser UI
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #2917924. -->
Reported by: [megachriz](https://www.drupal.org/user/654114)
>>>
<h3>Goal</h3>
<p>Have an usable XML Xpath parser UI without errors.</p>
<p>The requirements should all be met in order for this issue to be fixed. The optionals should be aimed for, but may be postponed to a follow up issue.</p>
<h3 id="requirements">Requirements</h3>
<ul>
<li>On the mapping page, the user must be able to configure xpaths</li>
<li>On the mapping page, the user must be able to provide a context xpath</li>
<li>The user must be able to remove mappings</li>
<li>The user must be able to import a simple XML file without errors when mapping to guid, title, body and a plain text field, using the default configuration</li>
</ul>
<h3 id="optional">Optional</h3>
<ul>
<li>The user should be able to remove configured xpaths</li>
<li>The requirements should be backed up with automated tests</li>
</ul>
<h3 id="ui">UI</h3>
<p>How the UI should look like depends on how the UI for the CSV parser is implemented. Currently we are aiming for an UI something like this:<br>
<img src="https://www.drupal.org/files/issues/feeds-xml-mapping-2.png"></p>
<ul>
<li>Xpaths are defined in the source column, in the same field as were CSV sources are defined.</li>
<li>The source's machine name may appear be underneath the text field, instead of in a separate column.</li>
<li>The first field (enter url of xml field) should be ignored, these belong to the Feeds Preview feature, to be developed at a later stage.</li>
</ul>
<p>For alternative designs, see <a href="https://www.drupal.org/node/2917405#feeds-xml">https://www.drupal.org/node/2917405#feeds-xml</a>.</p>
<h3 id="sample-file">Sample XML file</h3>
<pre><?xml version="1.0"><br><root><br> <item><br> <guid>1</guid><br> <title>Lorem ipsum</title><br> <body>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</body><br> <alpha>Lorem</alpha><br> </item><br> <item><br> <guid>2</guid><br> <title>Ut wisi enim ad minim veniam</title><br> <body>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</body><br> <alpha>Ut wisi</alpha><br> </item><br></root></pre><p>XML xpaths:</p>
<ul>
<li>Context: <code>//item</code></li>
<li>Title: <code>title</code></li>
<li>Body: <code>body</code></li>
<li>Alpha: <code>alpha</code></li>
</ul>
<h3 id="coding">Coding</h3>
<p>Files related to this issue (the accented ones are likely to need changes, the others are for reference):</p>
<ul>
<li>
<strong>feeds/src/Form/MappingForm.php<br>
Contains the code for the mapping UI.</strong>
</li>
<li>
<strong>feeds/src/Plugin/Type/Parser/ParserInterface.php<br>
Interface for parser plugins. Methods needs to be added of interaction with the mapping UI.</strong>
</li>
<li>
<strong>feeds/src/Feeds/Parser/ParserBase.php<br>
To be created, should be the base class for all parsers.</strong>
</li>
<li>
<strong>feeds/src/Feeds/Parser/CsvParser.php<br>
Should extend the newly added parser base class.</strong>
</li>
<li>
<strong>feeds/src/Feeds/Parser/OpmlParser.php<br>
Should extend the newly added parser base class.</strong>
</li>
<li>
<strong>feeds/src/Feeds/Parser/SitemapParser.php<br>
Should extend the newly added parser base class.</strong>
</li>
<li>
<strong>feeds/src/Feeds/Parser/SyndicationParser.php<br>
Should extend the newly added parser base class.</strong>
</li>
<li>
<strong>feeds_ex/src/Feeds/Parser/ParserBase.php<br>
Base class for Feeds extensible parsers. Should contain code that interacts with the mapping UI.</strong>
</li>
<li>
feeds_ex/src/Feeds/Parser/XmlParser.php<br>
Contains code specific to XML Xpath parser.
</li>
</ul>
<h3 id="tests">The automated test</h3>
<p>The automated test should be a functional one. Note that there is some overlap with <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/feeds/issues/2938500" title="Status: Closed (fixed)">#2938500: Add UI tests for the CSV parser</a></span>. It may be easier if that issue is fixed first.</p>
<ol>
<li><del>Add a file called XmlParserTest.php to feeds_ex/tests/src/functional/Feeds/Parser.</del></li>
<li><del>In this file, add a class that extends the base class for Feeds functional tests.</del></li>
<li>Add a method called <code>test()</code>.</li>
<li>
In the test:
<ol type="a">
<li>Add two fields to this content type: 'body' and a plain text field called 'alpha'.</li>
<li>Add a feed type using the UI. Give it a name and a label. Set parser to XML and content type to 'article'.</li>
<li>Go to the mappings page and add mappings for guid, title, body, alpha.</li>
<li>Save the mappings.</li>
<li>Assert that mappings exist for the feed type.</li>
<li>Add a new feed using the UI. Provide a source (see <a href="https://www.drupal.org#sample-file">Sample XML file</a> above).</li>
<li>Save and import.</li>
<li>Assert that two articles have been created with the expected values.</li>
</ol>
</li>
</ol>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ol>
<li><del>Fix the CSV parser UI: <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/feeds/issues/2669974" title="Status: Closed (fixed)">#2669974: Implement the CSV parser UI</a></span></del></li>
<li><del>Add a context field on the top of the mapping page.</del> <strong>DONE</strong>
<ul>
<li><del>Add a method to \Drupal\feeds\Plugin\Type\Parser\ParserInterface that allows interaction with the mapping UI.</del> Solved differently, see <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/feeds/issues/2954067" title="Status: Closed (fixed)">#2954067: Allow Feeds plugins to hook into the Mapping form</a></span></li>
<li><del>Add a base class for parser plugins in Feeds.</del> Not done, solved differently.</li>
<li><del>In this base class, define the method added to the interface, though with no contents.</del> Not done, solved differently.</li>
<li><del>In Drupal\feeds\Form\MappingForm, call this method.</del> <strong>DONE</strong>, see <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/feeds/issues/2954067" title="Status: Closed (fixed)">#2954067: Allow Feeds plugins to hook into the Mapping form</a></span></li>
<li><del>Implement the method in \Drupal\feeds_ex\Feeds\Parser\ParserBase.</del> <strong>DONE</strong></li>
<li><del>Save the context value as parser configuration: <code>$parser->configuration['context']['value']</code>.</del> <strong>DONE</strong></li>
</ul>
</li>
<li>Allow Xpaths to be entered in the source fields.
<ul>
<li>Save the xpath sources as parser configuration: <code>$parser->configuration['sources']</code>.</li>
</ul>
</li>
<li>Add automated tests for the UI.</li>
</ol>
issue
GitLab AI Context
Project: project/feeds_ex
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/feeds_ex/-/raw/8.x-1.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/feeds_ex
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD