RelatedIDs.php in Salesforce Suite 5.0.x
File
modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php
View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\salesforce\SFID;
use Drupal\salesforce\SObject;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
use Drupal\salesforce\Exception as SalesforceException;
class RelatedIDs extends SalesforceMappingFieldPluginBase {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$pluginForm = parent::buildConfigurationForm($form, $form_state);
$options = $this
->getConfigurationOptions($form['#entity']);
if (empty($options)) {
$pluginForm['drupal_field_value'] += [
'#markup' => $this
->t('No available entity reference fields.'),
];
}
else {
$pluginForm['drupal_field_value'] += [
'#type' => 'select',
'#options' => $options,
'#empty_option' => $this
->t('- Select -'),
'#default_value' => $this
->config('drupal_field_value'),
'#description' => $this
->t('If an existing connection is found with the selected entity reference, the linked identifier will be used.<br />For example, Salesforce ID for Drupal to SF, or Node ID for SF to Drupal.<br />If more than one entity is referenced, the entity at delta zero will be used.'),
];
}
return $pluginForm;
}
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)) {
return;
}
$field_settings = $field
->getFieldDefinition()
->getSettings();
$referenced_mappings = $this->mappedObjectStorage
->loadByDrupal($field_settings['target_type'], $field->entity
->id());
if (!empty($referenced_mappings)) {
$mapping = reset($referenced_mappings);
return $mapping
->sfid();
}
}
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'));
if (empty($value)) {
return NULL;
}
if (!$value instanceof SFID) {
try {
$value = new SFID($value);
} catch (\Exception $e) {
return NULL;
}
}
$referenced_mappings = $this->mappedObjectStorage
->loadBySfid($value);
if (!empty($referenced_mappings)) {
$mapped_object = reset($referenced_mappings);
return $mapped_object
->getMappedEntity() ? $mapped_object
->getMappedEntity()
->id() : NULL;
}
}
private function getConfigurationOptions($mapping) {
$instances = $this->entityFieldManager
->getFieldDefinitions($mapping
->get('drupal_entity_type'), $mapping
->get('drupal_bundle'));
$options = [];
foreach ($instances as $name => $instance) {
if (!$this
->instanceOfEntityReference($instance)) {
continue;
}
$options[$name] = $instance
->getLabel();
}
asort($options);
return $options;
}
public function getPluginDefinition() {
$definition = parent::getPluginDefinition();
$definition['config_dependencies']['config'] = [];
if ($field = FieldConfig::loadByName($this->mapping
->getDrupalEntityType(), $this->mapping
->getDrupalBundle(), $this
->config('drupal_field_value'))) {
$definition['config_dependencies']['config'][] = $field
->getConfigDependencyName();
foreach ($field
->getDependencies() as $type => $dependency) {
foreach ($dependency as $item) {
$definition['config_dependencies'][$type][] = $item;
}
}
}
return $definition;
}
public function checkFieldMappingDependency(array $dependencies) {
$definition = $this
->getPluginDefinition();
foreach ($definition['config_dependencies'] as $type => $dependency) {
foreach ($dependency as $item) {
if (!empty($dependencies[$type][$item])) {
return TRUE;
}
}
}
}
}