public function RelatedTermString::pullValue in Salesforce Suite 8.4
Same name and namespace in other branches
- 5.0.x modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedTermString.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedTermString::pullValue()
Pull callback for field plugins.
This callback is overloaded to serve 2 different use cases.
- Use case 1: primitive values If pullValue() returns a primitive value, callers will attempt to set the value directly on the parent entity.
- Use case 2: typed data If pullValue() returns a TypedDataInterface, callers will assume the implementation has set the appropriate value(s). The returned TypedData will be issued to a SalesforceEvents::PULL_ENTITY_VALUE event, but will otherwise be ignored.
Parameters
\Drupal\salesforce\SObject $sf_object: The SFObject being pulled.
\Drupal\Core\Entity\EntityInterface $entity: The entity being pulled.
\Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping: The mapping.
Return value
\Drupal\Core\TypedData\TypedDataInterface|mixed If a TypedDataInterface is returned, validate constraints and use TypedDataManager to set the value on the root entity. Otherwise, set the value directly via FieldableEntityInterface::set
Overrides SalesforceMappingFieldPluginBase::pullValue
File
- modules/
salesforce_mapping/ src/ Plugin/ SalesforceMappingField/ RelatedTermString.php, line 80
Class
- RelatedTermString
- Adapter for entity Reference and fields.
Namespace
Drupal\salesforce_mapping\Plugin\SalesforceMappingFieldCode
public function pullValue(SObject $sf_object, EntityInterface $entity, SalesforceMappingInterface $mapping) {
if (!$this
->pull() || empty($this
->config('salesforce_field'))) {
throw new SalesforceException('No data to pull. Salesforce field mapping is not defined.');
}
$field_name = $this
->config('drupal_field_value');
$instance = FieldConfig::loadByName($this->mapping
->getDrupalEntityType(), $this->mapping
->getDrupalBundle(), $field_name);
if (empty($instance)) {
return;
}
$value = $sf_object
->field($this
->config('salesforce_field'));
// Empty value means nothing to do here.
if (empty($value)) {
return NULL;
}
// Get the appropriate vocab from the field settings.
$vocabs = $instance
->getSetting('handler_settings')['target_bundles'];
// Look for a term that matches the string in the salesforce field.
$query = \Drupal::entityQuery('taxonomy_term');
$query
->condition('vid', $vocabs, 'IN');
$query
->condition('name', $value);
$tids = $query
->execute();
if (!empty($tids)) {
$term_id = reset($tids);
}
// If we cant find an existing term, create a new one.
if (empty($term_id)) {
$vocab = reset($vocabs);
$term = Term::create([
'name' => $value,
'vid' => $vocab,
]);
$term
->save();
$term_id = $term
->id();
}
return $term_id;
}