You are here

public function RelatedProperties::value in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedProperties::value()
  2. 8.3 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedProperties::value()

Given a Drupal entity, return the outbound value.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity being mapped.

\Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping: The parent SalesforceMapping to which this plugin config belongs.

Overrides SalesforceMappingFieldPluginInterface::value

File

modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php, line 53

Class

RelatedProperties
Adapter for entity Reference and fields.

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField

Code

public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
  [
    $field_name,
    $referenced_field_name,
  ] = explode(':', $this
    ->config('drupal_field_value'), 2);

  // Since we're not setting hard restrictions around bundles/fields, we may
  // have a field that doesn't exist for the given bundle/entity. In that
  // case, calling get() on an entity with a non-existent field argument
  // causes an exception during entity save. Probably a bug, but I haven't
  // found it in the issue queue. So, just check first to make sure the field
  // exists.
  $instances = $this->entityFieldManager
    ->getFieldDefinitions($mapping
    ->get('drupal_entity_type'), $mapping
    ->get('drupal_bundle'));
  if (empty($instances[$field_name])) {
    return;
  }
  $field = $entity
    ->get($field_name);
  if (empty($field->entity)) {

    // This reference field is blank.
    return;
  }
  try {
    $describe = $this->salesforceClient
      ->objectDescribe($mapping
      ->getSalesforceObjectType());
    $field_definition = $describe
      ->getField($this
      ->config('salesforce_field'));
    if ($field_definition['type'] == 'multipicklist') {
      $values = [];
      foreach ($field as $ref_entity) {
        if (!$ref_entity->entity
          ->get($referenced_field_name)
          ->isEmpty()) {
          $values[] = $ref_entity->entity
            ->get($referenced_field_name)->value;
        }
      }
      return implode(';', $values);
    }
    else {
      return $field->entity
        ->get($referenced_field_name)->value;
    }
  } catch (\Exception $e) {
    return NULL;
  }
}