You are here

public function SalesforceExampleSubscriber::pullPrepull in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php \Drupal\salesforce_example\EventSubscriber\SalesforceExampleSubscriber::pullPrepull()
  2. 5.0.x modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php \Drupal\salesforce_example\EventSubscriber\SalesforceExampleSubscriber::pullPrepull()

PULL_PREPULL event subscriber example.

File

modules/salesforce_example/src/EventSubscriber/SalesforceExampleSubscriber.php, line 182

Class

SalesforceExampleSubscriber
Class SalesforceExampleSubscriber.

Namespace

Drupal\salesforce_example\EventSubscriber

Code

public function pullPrepull(SalesforcePullEvent $event) {

  // For the "contact" mapping, if the SF record is marked "Inactive", do not
  // pull the record and block the user account.
  $mapping = $event
    ->getMapping();
  switch ($mapping
    ->id()) {
    case 'contact':
      $sf_data = $event
        ->getMappedObject()
        ->getSalesforceRecord();

      /** @var \Drupal\user\Entity\User $account */
      $account = $event
        ->getEntity();
      try {
        if (!$sf_data
          ->field('Inactive__c')) {

          // If the SF record is not marked "Inactive", proceed as normal.
          return;
        }
      } catch (\Exception $e) {

        // Fall through if "Inactive" field was not found.
      }

      // If we got here, SF record is marked inactive. Don't pull it.
      $event
        ->disallowPull();
      if (!$account
        ->isNew()) {

        // If this is an update to an existing account, block the account.
        // If this is a new account, it won't be created.
        $account
          ->block()
          ->save();
      }
  }
}