Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In some cases, busyness import rules for the XML data are very hard to implement just with the XPath expression.
It is common to have lots of conditions about some import values, comming from more than one child or attribute, that are very hard to implement in the XPath 1.0. It should be a nice feature to create a hook that will get the currently parsed DOMNode and process it to determine wether it is required or not (additional filter ontop the XPath expression).
Comments
Comment #1
ndobromirov CreditAttribution: ndobromirov commentedAdded the hook implementation.
Comment #2
ndobromirov CreditAttribution: ndobromirov commentedAdded more context information to the filtering hook. The previous patch can be ignored.
Comment #3
ndobromirov CreditAttribution: ndobromirov commentedThe last patch generates variable name conflicts within the parse method. Adding a new patch to fix the issue.
Comment #4
twistor CreditAttribution: twistor commentedI'd be curious to know what your use case is, have you seen Feeds Tamper?
Why is this being changed? I don't follow.
checked
TRUE
Comment #5
twistor CreditAttribution: twistor commentedComment #6
ndobromirov CreditAttribution: ndobromirov commentedThanks for the review.
Answer of Q1:
I am using feeds tamper, but it is a tool for additional processing of mapped field(s). Correct me if something more can be done with it.
My use case is that I want to filter the context nodes before passing them for processing with feeds tamper.
Normally this is implemented with the "Context" XPath expression in the parser settings, but in my case the expression is becoming too large, complex with a lot of sub-expression repetitions within it - hard to maintain/change. In some of the use cases I need to be able to use node set manipulations like intersection and subtraction when XPath 1.0 allows only union with the "|" syntax.
This is the reason that a hook was implemented, with some PHP code all of the above was solved in a matter of 2-3 screens of code.
Answer of Q2:
The parameter name $source in the original implementation needs to be of type FeedsSource and it is passed as additional context information to the filtering hook. Part of the process method without rename of the variable would look like this:
The inner loop defines iteration key variable with the same name as the parameter, replacing the FeedsSource with a string on the first iteration, making the call to the filtering hook with wrong source context in the second iteration of the outer loop.
Answer of Q3:
Thanks, for spotting the typo.
Answer of Q4:
Will fix the constants to the convention specs.
Comment #7
twistor CreditAttribution: twistor commented1. That is true. Feeds Tamper can do filtering, but you lose the context of the XML. I am familiar with how unruly the XPath expressions can get. Could we call the hook 'feeds_xpathparser_filter_domnode' or something so that it's not confused with node.module? Maybe pass in the DOMDocument as well so you could do more than just filter, like append children or something.
2. Good catch. I'm surprised this hasn't caused trouble already. Not to be picky, but we should leave $source in the function signature, and change
foreach ($source_config['sources'] as $source => $query) {
toforeach ($source_config['sources'] as $element_key => $query) {
. The $source in the function is pretty standard.You might also be interested in #1846956: Allow using safe PHP functions.
Comment #8
ndobromirov CreditAttribution: ndobromirov commentedThanks for the fast response.
1. I will rename the hook and add the document parameter.
2. I will do the refactoring and upload a patch with all the discussed fixes.
Best regards,
Nikolay D.
Comment #9
ndobromirov CreditAttribution: ndobromirov commentedAdding the patch fixing the issues discussed.
Comment #10
twistor CreditAttribution: twistor commentedThanks.
http://drupalcode.org/project/feeds_xpathparser.git/commit/fbbc659
Comment #11
osopolarTODO: The hook hook_feeds_xpathparser_filter_domnode() needs to be documented somewhere - for example in a README.txt.
Comment #12
jenlamptonAlso could be useful for documentation...
If you want to actually *see* the DOMNode you need to do this...
Comment #13
twistor CreditAttribution: twistor commentedWould something like this work?
Words are hard.
Comment #14
osopolarLooks nice, thanks @twistor.
Comment #16
twistor CreditAttribution: twistor commentedComment #17
twistor CreditAttribution: twistor commented6.x usage is declining slowly. If anyone feels like backporting go ahead.