You are here

function _salesforce_example_entity_manage in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_example/salesforce_example.module \_salesforce_example_entity_manage()
  2. 8.3 modules/salesforce_example/salesforce_example.module \_salesforce_example_entity_manage()

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.

3 calls to _salesforce_example_entity_manage()
salesforce_example_entity_delete in modules/salesforce_example/salesforce_example.module
Implements hook_entity_delete().
salesforce_example_entity_insert in modules/salesforce_example/salesforce_example.module
Implements hook_entity_insert().
salesforce_example_entity_update in modules/salesforce_example/salesforce_example.module
Implements hook_entity_update().

File

modules/salesforce_example/salesforce_example.module, line 77
Contains salesforce_example.module.

Code

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();
        }
      }
    }
  }
}