Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
salesforce-3170137
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
salesforce-3170137
Commits
d57c6a4c
Commit
d57c6a4c
authored
7 years ago
by
Chris Rikli
Committed by
Aaron Bauman
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2859979
by chrisrikli: Entity hook examples for salesforce_example
parent
cfd6390e
No related branches found
Branches containing commit
Tags
8.x-3.0-beta1
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/salesforce_example/salesforce_example.module
+142
-0
142 additions, 0 deletions
modules/salesforce_example/salesforce_example.module
with
142 additions
and
0 deletions
modules/salesforce_example/salesforce_example.module
+
142
−
0
View file @
d57c6a4c
...
...
@@ -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
();
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment