I've created a subscriber to the SalesforceEvents::PULL_PRESAVE event.
When an entity is being inserted for the first time, there does not seem to be an entity available for modification in the SalesforcePullEvent parameter passed into the function.
So in this class:
/**
* Class SfdcPullSubscriber
*
* @package Drupal\gb_sfdc\EventSubscriber
*/
class SfdcPullSubscriber implements EventSubscriberInterface {
/**
* @param \Drupal\salesforce_mapping\SalesforcePullEvent $event
*/
public function pullEntityPresaveAlter(SalesforcePullEvent $event) {
/** @var MappedObject $mapped_object */
$mapped_object = $event->getMappedObject();
/** @var SObject $sf */
$sf = $mapped_object->getSalesforceRecord();
/** @var Entity $entity */
$entity = $event->getEntity();
}
/** TRUNCATED **/
}
On an insert, $entity is NULL. On an update, it will be a Drupal entity as expected.
Comments
Comment #2
chrisrikli commentedI believe this is happening because the contructor for SalesForcePullEvent calls the getMappedEntity() method of MappedObject.
But the getMappedEntity() method presumes that there is an entity_id, which there wouldn't be on an insert as $entity->save() hasn't yet been executed:
However, we know that the drupal_entity member variable gets populated by the foreach that loops over the fields in the pull method of MappedObject. So I've modified the getMappedEntity() function as follows. I don't know if this is the best way to do this or I'd submit a patch with the changes.
Comment #3
aaronbaumanLooks like a reasonable approach to me.
Please submit a patch, and we'll see if testbot likes it.
Comment #4
chrisrikli commentedWill do Aaron. I'll post it in this thread however there are also some other changes I've made.
Comment #5
aaronbaumanGreat, thank you for your contributions.
Patches are more than welcome - we're actively working on better test coverage, porting existing D7 stuff, and adding some new features for D8. I'll try to get them reviewed quickly.
Comment #6
chrisrikli commentedAaron, attached is a patch that does several things:
Comment #7
aaronbaumanI think we have some test coverage for this.
Let's see what testbot says.
Comment #9
aaronbauman3 points:
1. You should be able to use Mapped Object entity save event, coupled with $mapped_object->drupal_entity->isNew() to achieve equivalent functionality to PULL_POSTSAVE. Maybe we can include documentation to explicitly point contrib to this, and/or an example in salesforce_example module?
2. what's with SalesforceMapping::getIterator ? is this cruft from the other work you mentioned? I don't see this used anywhere in the patch.
3. Please re-roll against latest dev. The example subscriber has been updated, which is preventing the patch from applying.
Comment #10
aaronbaumanComment #11
chrisrikli commented1. So you're saying used a hook_entity_update/hook_entity_insert to look for the MappedObject entity save and react to that? A couple things I like about having a SubscriberEvent for PULL_POSTSAVE is that it keeps all my code in the same Subscriber file and able to use a Trait I'd developed for other activities. As opposed to having some code in the Subscriber class and some in a .module file.
2. Ah, yes, I forgot to mention that. When I try to use kint() or ksm() to take a look at the object, it was failing due to the lack of a getIterator() method. It never really worked so I'll remove it from the code.
3. Will do.
Comment #12
aaronbauman1. re PULL_PRESAVE: take a look at #2551893: Add events for matching entity hooks. While I appreciate your position as a developer, as a maintainer I want to maintain as little code as possible. The more hooks i can eliminate, the better. Another implementation idea: you could use your .module file to emit your own PULL_PRESAVE event, and delegate that back to your subscriber. It's slightly more overhead, but would allow you to encapsulate your logic in the subscriber. And you can ditch the .module altogether when 2551893 lands.
2. i've had no end of issues with kint. There's a poorly documented "max-level" setting that may help you debugging:
http://drupal.stackexchange.com/questions/211928/how-can-i-make-kint-loa...
I've also had some luck using var-dumper instead of kint (no module needed, just install with composer and enable via devel settings):
https://www.drupal.org/project/vardumper
Comment #13
chrisrikli commented1. Fair enough. I removed the PULL_POSTSAVE modification from the attached patch.
2. Yes, I've got that max level set. Thank you for the steer to vardumper.
Attached patch has:
Comment #14
aaronbaumanThis looks great to me.
One more run through testbot.
Comment #16
aaronbaumanOK, this is in.
Thanks again for the contrib