You are here

public function RelatedIDs::pullValue in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedIDs::pullValue()
  2. 8.3 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedIDs::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/RelatedIDs.php, line 86

Class

RelatedIDs
Adapter for entity Reference and fields.

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField

Code

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.');
  }
  $value = $sf_object
    ->field($this
    ->config('salesforce_field'));

  // Empty value means nothing to do here.
  if (empty($value)) {
    return NULL;
  }

  // If value is not an SFID, make it one.
  if (!$value instanceof SFID) {
    try {
      $value = new SFID($value);
    } catch (\Exception $e) {
      return NULL;
    }
  }

  // Convert SF Id to Drupal Id.
  $referenced_mappings = $this->mappedObjectStorage
    ->loadBySfid($value);
  if (!empty($referenced_mappings)) {
    $mapped_object = reset($referenced_mappings);
    return $mapped_object
      ->getMappedEntity() ? $mapped_object
      ->getMappedEntity()
      ->id() : NULL;
  }
}