You are here

public function RelatedIDs::value 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::value()
  2. 8.3 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedIDs::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/RelatedIDs.php, line 55

Class

RelatedIDs
Adapter for entity Reference and fields.

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField

Code

public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
  $field_name = $this
    ->config('drupal_field_value');
  $instances = $this->entityFieldManager
    ->getFieldDefinitions($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  if (empty($instances[$field_name])) {
    return;
  }
  $field = $entity
    ->get($field_name);
  if (empty($field
    ->getValue()) || is_null($field->entity)) {

    // This reference field is blank or the referenced entity no longer
    // exists.
    return;
  }

  // Now we can actually fetch the referenced entity.
  $field_settings = $field
    ->getFieldDefinition()
    ->getSettings();

  // @TODO this procedural call will go away when sf mapping object becomes a service or field
  $referenced_mappings = $this->mappedObjectStorage
    ->loadByDrupal($field_settings['target_type'], $field->entity
    ->id());
  if (!empty($referenced_mappings)) {
    $mapping = reset($referenced_mappings);
    return $mapping
      ->sfid();
  }
}