You are here

public function PropertiesExtended::pullValue in Salesforce Suite 8.3

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/PropertiesExtended.php, line 239

Class

PropertiesExtended
Adapter for entity properties and fields.

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField

Code

public function pullValue(SObject $sf_object, EntityInterface $entity, SalesforceMappingInterface $mapping) {
  $field_selector = $this
    ->config('drupal_field_value');
  $pullValue = parent::pullValue($sf_object, $entity, $mapping);
  try {

    // Fetch the TypedData property and set its value.
    $data = $this
      ->getDataFetcher()
      ->fetchDataByPropertyPath($entity
      ->getTypedData(), $field_selector);
    $data
      ->setValue($pullValue);
    return $data;
  } catch (MissingDataException $e) {
  } catch (\Drupal\typed_data\Exception\InvalidArgumentException $e) {
  }

  // Allow any other exception types to percolate.
  // If the entity doesn't have any value in the field, data fetch will
  // throw an exception. We must attempt to create the field.
  // Typed Data API doesn't provide any good way to initialize a field value
  // given a selector. Instead we have to do it ourselves.
  // We descend only to the first-level fields on the entity. Cascading pull
  // values to entity references is not supported.
  $parts = explode('.', $field_selector, 4);
  switch (count($parts)) {
    case 1:
      $entity
        ->set($field_selector, $pullValue);
      return $entity
        ->getTypedData()
        ->get($field_selector);
    case 2:
      $field_name = $parts[0];
      $delta = 0;
      $property = $parts[1];
      break;
    case 3:
      $field_name = $parts[0];
      $delta = $parts[1];
      $property = $parts[2];
      if (!is_numeric($delta)) {
        return;
      }
      break;
    case 4:
      return;
  }

  /** @var \Drupal\Core\TypedData\ListInterface $list_data */
  $list_data = $entity
    ->get($field_name);

  // If the given delta has not been initialized, initialize it.
  if (!$list_data
    ->get($delta) instanceof TypedDataInterface) {
    $list_data
      ->set($delta, []);
  }

  /** @var \Drupal\Core\TypedData\TypedDataInterface|\Drupal\Core\TypedData\ComplexDataInterface $typed_data */
  $typed_data = $list_data
    ->get($delta);
  if ($typed_data instanceof ComplexDataInterface && $property) {

    // If the given property has not been initialized, initialize it.
    if (!$typed_data
      ->get($property) instanceof TypedDataInterface) {
      $typed_data
        ->set($property, []);
    }

    /** @var \Drupal\Core\TypedData\TypedDataInterface $typed_data */
    $typed_data = $typed_data
      ->get($property);
  }
  if (!$typed_data instanceof TypedDataInterface) {
    return;
  }
  $typed_data
    ->setValue($pullValue);
  return $typed_data
    ->getParent();
}