You are here

public function SalesforceMappingFieldPluginBase::pullValue in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/src/SalesforceMappingFieldPluginBase.php \Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase::pullValue()
  2. 5.0.x modules/salesforce_mapping/src/SalesforceMappingFieldPluginBase.php \Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase::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 SalesforceMappingFieldPluginInterface::pullValue

1 call to SalesforceMappingFieldPluginBase::pullValue()
PropertiesExtended::pullValue in modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php
Pull callback for field plugins.
2 methods override SalesforceMappingFieldPluginBase::pullValue()
PropertiesExtended::pullValue in modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php
Pull callback for field plugins.
RelatedIDs::pullValue in modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php
Pull callback for field plugins.

File

modules/salesforce_mapping/src/SalesforceMappingFieldPluginBase.php, line 249

Class

SalesforceMappingFieldPluginBase
Defines a base Salesforce Mapping Field Plugin implementation.

Namespace

Drupal\salesforce_mapping

Code

public function pullValue(SObject $sf_object, EntityInterface $entity, SalesforceMappingInterface $mapping) {

  // @TODO to provide for better extensibility, this would be better implemented as some kind of constraint or plugin system. That would also open new possibilities for injecting business logic into he mapping layer.
  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'));

  // objectDescribe can throw an exception, but that's outside the scope of
  // being handled here. Allow it to percolate.
  $describe = $this->salesforceClient
    ->objectDescribe($mapping
    ->getSalesforceObjectType());
  $data_definition = $this
    ->getFieldDataDefinition($entity);
  $drupal_field_type = $this
    ->getDrupalFieldType($data_definition);
  $drupal_field_settings = $data_definition
    ->getSettings();
  $field_definition = $describe
    ->getField($this
    ->config('salesforce_field'));
  switch (strtolower($field_definition['type'])) {
    case 'boolean':
      if (is_string($value) && strtolower($value) === 'false') {
        $value = FALSE;
      }
      $value = (bool) $value;
      break;
    case 'datetime':
      if ($drupal_field_type === 'datetime_iso8601') {
        $value = substr($value, 0, 19);
      }
      break;
    case 'double':
      $value = (double) $value;
      break;
    case 'integer':
      $value = (int) $value;
      break;
    case 'multipicklist':
      if (!is_array($value)) {
        $value = explode(';', $value);
        $value = array_map('trim', $value);
      }
      break;
    case 'id':
    case 'reference':
      if (empty($value)) {
        break;
      }

      // If value is an SFID, cast to string.
      if ($value instanceof SFID) {
        $value = (string) $value;
      }
      else {
        $value = (string) new SFID($value);
      }
      break;
    default:
      if (is_string($value)) {
        if (isset($drupal_field_settings['max_length']) && $drupal_field_settings['max_length'] > 0 && $drupal_field_settings['max_length'] < strlen($value)) {
          $value = substr($value, 0, $drupal_field_settings['max_length']);
        }
      }
      break;
  }
  return $value;
}