RelatedProperties.php in Salesforce Suite 8.3
File
modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php
View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
class RelatedProperties 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' => 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('Select a property from the referenced field.<br />If more than one entity is referenced, the entity at delta zero will be used.<br />An entity reference field will be used to sync an identifier, e.g. Salesforce ID and Node ID.'),
];
}
return $pluginForm;
}
public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
list($field_name, $referenced_field_name) = explode(':', $this
->config('drupal_field_value'), 2);
$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)) {
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) {
$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;
}
}
protected function getConfigurationOptions($mapping) {
$instances = $this->entityFieldManager
->getFieldDefinitions($mapping
->get('drupal_entity_type'), $mapping
->get('drupal_bundle'));
if (empty($instances)) {
return;
}
$options = [];
foreach ($instances as $instance) {
if (!$this
->instanceOfEntityReference($instance)) {
continue;
}
$settings = $this
->selectionPluginManager()
->getSelectionHandler($instance)
->getConfiguration();
$entity_type = $settings['target_type'];
$properties = [];
try {
if (!empty($settings['handler_settings']['target_bundles'])) {
foreach ($settings['handler_settings']['target_bundles'] as $bundle) {
$properties += $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle);
}
}
else {
$properties += $this->entityFieldManager
->getBaseFieldDefinitions($entity_type);
$bundles = array_keys($this->entityTypeBundleInfo
->getBundleInfo($entity_type));
foreach ($bundles as $bundle) {
$properties += $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle);
}
}
} catch (\Exception $e) {
continue;
}
foreach ($properties as $key => $property) {
$options[(string) $instance
->getLabel()][$instance
->getName() . ':' . $key] = $property
->getLabel();
}
}
if (empty($options)) {
return;
}
foreach ($options as $group => &$option_set) {
asort($option_set);
}
asort($options);
return $options;
}
}