Problem/Motivation
In the context of dom process plugins, the general idea is to manipulate html before storing it into Drupal.
One possible use case is to extract information from a given part of the markup, and ignore the rest.
Last year at NEDCamp I suggested to write something to do this as someone was telling me his use case.
Lately I found myself needing something like this.
A nice related note: the current dom migrate process plugins inside migrate plus are more generic than html processing, since it is handling a \DOMDocument object, which can also be generated from other sources like xml; so it is useful not only for html processing.
Proposed resolution
Create a new migrate process plugin that can receive a \DOMDocument, e.g. generated with the dom plugin, and given some options extract a subset of the data and return it as a simple string.
Examples
A couple of examples.
process:
'body/value':
-
plugin: dom
method: import
source: 'body/0/value'
-
plugin: dom_extract
xpath: '/div/div'
This will extract the contents of the second nested <div> element.
process:
field_data_type:
-
plugin: dom
method: import
source: 'body/0/value'
-
plugin: dom_extract
xpath: '/h1'
target: 'attribute:data-type'
This will extract the contents of the data-type attribute on the top level h1 element.
Remaining tasks
Provide patch.
Review.
User interface changes
N.A.
API changes
New dom_extract migrate process plugin.
Data model changes
N.A.
Release notes snippet
New dom_extract migrate process plugin to retrieve a subset of a given dom as a string.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3112559-4.patch | 1.58 KB | benjifisher |
| #3 | 3112559-2.patch | 4.35 KB | marvil07 |
Comments
Comment #2
marvil07 commentedHere the first patch.
Comment #3
marvil07 commentedOK, really upload the patch.
Comment #4
benjifisherOn the DrupalSpoons version of this issue, I just added another way to provide this functionality. For convenience, I am attaching a patch to provide the
dom_selectplugin.See the doc block in the patch for usage information. See the DrupalSpoons issue for a comparison of the two plugins.
Comment #5
heddnThis is now fixed in https://gitlab.com/drupalspoons/migrate_plus/-/merge_requests/8