You are here

salesforce_example.module in Salesforce Suite 8.4

File

modules/salesforce_example/salesforce_example.module
View source
<?php

/**
 * @file
 * Contains salesforce_example.module.
 */
use Drupal\salesforce\SFID;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_help().
 */
function salesforce_example_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the salesforce_example module.
    case 'help.page.salesforce_example':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Salesforce Examples') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_insert().
 *
 * For this example we are simply calling a "manage" function and passing a
 * parameter to indicate what type of operaiton is taking place.
 */
function salesforce_example_entity_insert(EntityInterface $entity) {
  _salesforce_example_entity_manage($entity, 'insert');
}

/**
 * Implements hook_entity_update().
 *
 * For this example we are simply calling a "manage" function and passing a
 * parameter to indicate what type of operaiton is taking place.
 */
function salesforce_example_entity_update(EntityInterface $entity) {
  _salesforce_example_entity_manage($entity, 'update');
}

/**
 * Implements hook_entity_delete().
 *
 * For this example we are simply calling a "manage" function and passing a
 * parameter to indicate what type of operaiton is taking place.
 */
function salesforce_example_entity_delete(EntityInterface $entity) {
  _salesforce_example_entity_manage($entity, 'delete');
}

/**
 * Handler for entity operations.
 *
 * This function is called by the three hook_entity_OPERATION functions
 * - salesforce_example_entity_insert (hook_entity_insert)
 * - salesforce_example_entity_update (hook_entity_update)
 * - salesforce_example_entity_delete (hook_entity_delete)
 *
 * In this example we are doing some work with Drupal Commerce entities,
 * specifically products (commerce_product) and product variations
 * (commerce_product_variation)
 *
 * If you're not familiar with Drupal Commerce object relationships, all you
 * need to know is that Products are umbrella entities around Product
 * Variations.  Products have Product Variatinos. So if you have a Drupal
 * T-Shirt in sizes S,M,L,XL, the Product is "Drupal T-Shirt" and you will have
 * four Product Variations, one for each size.  The Product object will have
 * entity references to each Product Variation.
 */
function _salesforce_example_entity_manage(EntityInterface &$entity, $op) {

  /** @var \Drupal\salesforce_mapping\MappedObjectStorage $mapped_object_storage */
  $mapped_object_storage =& drupal_static(__FUNCTION__);
  if (!isset($mapped_object_storage)) {
    $mapped_object_storage = \Drupal::service('entity_type.manager')
      ->getStorage('salesforce_mapped_object');
  }

  // We're performing a check to see if we're dealing with a Salesforce Mapped
  // Object.  The reason we do this instead of just checking to see if we're
  // dealing with a Commerce Product Variation is that we could very well have
  // product variations that are not being managed by the SFDC integration. If
  // this is the case, we do not want to be programmatically manipulating these
  // objects.
  if ($entity
    ->getEntityTypeId() == 'salesforce_mapped_object') {

    /** @var \Drupal\salesforce_mapping\Entity\MappedObject $entity */
    $mapped_entity = $entity
      ->getMappedEntity();
    if (!$mapped_entity) {
      return;
    }

    // Here we check to see if the entity that the SFDC Mapping Object dealt
    // with was a Commerce Product
    // Variation.
    if ($mapped_entity
      ->getEntityTypeId() == 'commerce_product_variation') {

      /** @var \Drupal\salesforce\SObject $sf */
      $sf = $entity
        ->getSalesforceRecord();
      if ($sf != NULL) {

        // We are operating under the assumption that the parent Commerce
        // Product object is also being managed by the SFDC integration.
        // Therefore we will be able to load the object by leveraging the
        // Salesforce Modules storage service and using the SDFC ID as the key.
        // Get the SFDC ID for the parent Product (the Product -> Product
        // Variation already exists in SFDC,  conveniently mirroring the Drupal
        // Commerce model).
        $course_sfdc_id = $sf
          ->field('Parent_Product__c');

        // Create an SFID object using the vlaue.
        $sfid = new SFID($course_sfdc_id);

        // Use the storage object to load the mapped object(s) that correspond
        // to the SFID.
        $mapped_objects = $mapped_object_storage
          ->loadBySfid($sfid);
        if (is_array($mapped_objects)) {

          // We are lazily assuming that there will only be one corresponding
          // prodct object and that it will be the first item in the returned
          // array.
          $mapped_object = current($mapped_objects);

          /** @var \Drupal\commerce_product\Entity\Product $course */
          $course = $mapped_object
            ->getMappedEntity();
          $mgr = \Drupal::entityTypeManager()
            ->getStorage('commerce_product');

          /** @var \Drupal\commerce_product\Entity\Product $product */
          $product = $mgr
            ->load($course
            ->id());

          // If this is a deletion of a Product Variation, we need to remove the
          // reference to the variation  from the Commerce Product.
          if ($op == 'delete') {
            $product
              ->removeVariation($mapped_entity);
          }
          else {
            $product
              ->addVariation($mapped_entity);
          }

          // Finally we save the Commerce Product that we've manipulated.
          $product
            ->save();
        }
      }
    }
  }
}