Problem/Motivation

#2983197: Catch TamperException to prevent import from crashing tries to prevent an import from crashing if an unapplicable tamper plugin gets applied to a source. A tamper plugin is for example not applicable to a source if the tamper plugin expects the source to be in certain format when the source isn't. For example, the tamper plugin "implode" expects the source to be an array and throws an exception if it doesn't.

The patch from the issue noted above now catches an exception and records an error message for it. The problem is that the import of the item goes through, only just without the tampers applied. This can possible lead to unintended values being imported.

Removing the item from the parser result is also not an option, because then it might be removed from the site if the feed type is configured to delete previously imported items.

Proposed resolution

A possible solution is to be able to mark an item as invalid, by adding new methods to \Drupal\feeds\Feeds\Item\ItemInterface.

Then, during processing, Feeds needs to detect that the item is marked invalid. And if so, don't import it. And report which item is invalid.

Remaining tasks

  • Implement a solution.
  • Add test coverage.
  • Review.
  • Commit.

User interface changes

None.

API changes

\Drupal\feeds\Feeds\Item\ItemInterface gets new methods.

Data model changes

None.

Release notes snippet

New methods are added to \Drupal\feeds\Feeds\Item\ItemInterface: items can now be marked as invalid.

Issue fork feeds-3063055

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

MegaChriz created an issue. See original summary.

megachriz’s picture

Assigned: megachriz » Unassigned
Status: Active » Needs review
StatusFileSize
new7.15 KB

This is a possible implementation. I'm not sure about the approach yet. Maybe it should be implemented using a constraint instead? It feels a bit dirty to have two places where an error message for the failing entity is composed.

megachriz’s picture

StatusFileSize
new7.15 KB
new1.09 KB

Fixing PHP syntax errors.

Status: Needs review » Needs work

The last submitted patch, 3: feeds-item-validation-3063055-3.patch, failed testing. View results

florianmuellerch’s picture

@MegaChriz thank you very much for that valuable patch!

I applied it and also added an issue with patch to feeds_tamper in #3109509: Mark item as invalid when tampering data does not meet expectations so we can use setInvalid() in a tamper.

florianmuellerch’s picture

StatusFileSize
new7.87 KB

I extended the #3 patch with a message which can be optionally set on markInvalid(), so we can see in the error message what lead to the invalidity of the element. @MegaChriz maybe you want to check if that still meets coding guidelines.

gaele’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: feeds-item-validation-3063055-6.patch, failed testing. View results

webdrips’s picture

StatusFileSize
new6.64 KB

Re-rolling #6 with latest dev

gaele’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: feeds-item-validation-3063055-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

megachriz’s picture

Status: Needs work » Needs review

For backwards compatibility I have moved the new methods to a new interface called ValidatableItemInterface. Feeds Tamper can then check if the item implements that interface before trying to call the new methods. This way, Feeds can still be compatible with older Feeds versions.

Let's see if the test passes now.

megachriz’s picture

Status: Needs review » Needs work

I see that the new properties are also exported when calling BaseItem::toArray(), which I think we don't want. So these properties should not be exported. So that should get fixed, but perhaps before we do that it makes sense to finalize #3452563: Creation of dynamic property Drupal\feeds\Feeds\Item\SyndicationItem::$parent:fid is deprecated first, because in that issue BaseItem::toArray() gets also changed. And I think that issue is good to go, just waiting a few more days for possible feedback on that one.

megachriz’s picture

Status: Needs work » Needs review

This is now ready to be tested in combination with #2983197: Catch TamperException to prevent import from crashing for Feeds Tamper.

  • megachriz committed f241db3e on 8.x-3.x
    Issue #3063055 by megachriz, florianmuellerch, webdrips: Allow parsers...
megachriz’s picture

Status: Needs review » Fixed

I merged the changes!

I will merge #2983197: Catch TamperException to prevent import from crashing shortly.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.