Problem/Motivation
In \Drupal\feeds\Feeds\Processor\EntityProcessorBase::clearTarget() a field on the entity is cleared in the following way:
unset($entity_target->{$target_name});
This implies that the field name is always equal to the target name. This is not true when you have multiple FeedsTarget plugins for the same field.
For example, in my sandbox project "Feeds DEV" there is FeedsTarget plugin called "file_base64" that provides an alternative file target and converts a base64 string to a file. The target name is then for example "field_file:base64".
In this case Feeds tries to clear $entity->field_file:base64 which makes no sense. It should clear $entity->field_file instead.
Steps to reproduce
Implement a FeedsTarget plugin where the target name is different from the field name.
Proposed resolution
Keep EntityProcessorBase::clearTarget(), but move the task from clearing the actual field on the entity to the target plugin.
Remaining tasks
- Review.
- Commit.
User interface changes
More than one target for a particular field can become visible in the UI, depending if multiple FeedsTarget plugins for the same field are implemented. Feeds itself doesn't have this.
API changes
\Drupal\feeds\Plugin\Type\Target\TargetInterfacewill get a new method calledclearTarget():
/** * Clears the target on an object. * * @param \Drupal\feeds\FeedInterface $feed * The feed object. * @param \Drupal\Core\Entity\EntityInterface $entity * The target object. * @param string $target * The name of the target to unset. */ public function clearTarget(FeedInterface $feed, EntityInterface $entity, string $target);- The signature for
EntityProcessorBase::clearTarget()changes so the feed can be passed to the target:
/** * Clears the target on the entity. * * @param \Drupal\feeds\FeedInterface $feed * The feed object. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to clear the target on. * @param \Drupal\feeds\Plugin\Type\Target\TargetInterface $target * The target plugin. * @param string $target_name * The property to clear on the entity. */ protected function clearTarget(FeedInterface $feed, EntityInterface $entity, TargetInterface $target, $target_name) {
Data model changes
None.
Patch will follow.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | feeds-clear-target.patch | 3.81 KB | megachriz |
Comments
Comment #2
megachrizLet's see if this breaks any tests.
Comment #3
andypostAny reason to pass a feed object if it's unused?
Comment #4
megachrizYes, it is for consistency with other methods in
\Drupal\feeds\Plugin\Type\Target\TargetInterface, most notablysetTarget()andisEmpty():Comment #5
andypostthen it look great)
Comment #7
megachrizThanks for looking, @andypost!
Committed #2.