Skip to content
Snippets Groups Projects
Commit d57c6a4c authored by Chris Rikli's avatar Chris Rikli Committed by Aaron Bauman
Browse files

Issue #2859979 by chrisrikli: Entity hook examples for salesforce_example

parent cfd6390e
No related branches found
Tags 8.x-3.0-beta1
No related merge requests found
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_help().
......@@ -22,3 +23,144 @@ function salesforce_example_help($route_name, RouteMatchInterface $route_match)
default:
}
}
/**
* implementation of 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
*
* @param \Drupal\Core\Entity\EntityInterface $entity
*/
function salesforce_example_entity_insert(EntityInterface $entity) {
_salesforce_example_entity_manage($entity, 'insert');
}
/**
* implementation of 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
*
* @param \Drupal\Core\Entity\EntityInterface $entity
*/
function salesforce_example_entity_update(EntityInterface $entity) {
_salesforce_example_entity_manage($entity, 'update');
}
/**
* implementation of 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
*
* @param \Drupal\Core\Entity\EntityInterface $entity
*/
function salesforce_example_entity_delete(EntityInterface $entity) {
_salesforce_example_entity_manage($entity, 'delete');
}
/**
* 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.
*
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param $op
*/
function _salesforce_example_entity_manage(\Drupal\Core\Entity\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.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();
/**
* 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 \Drupal\salesforce\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 prodctu 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);
}
/**
* Otherwise we need to add the reference to the Product Variation that was just inserted or updated
*/
else {
$product->addVariation($mapped_entity);
}
/**
* Finally we save the Commerce Product that we've manipulated
*/
$product->save();
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment