View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
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' => $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('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) {
[
$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) {
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;
}
}
public function getPluginDefinition() {
$definition = parent::getPluginDefinition();
$definition['config_dependencies']['config'] = [];
$field_name = $this
->config('drupal_field_value');
if (strpos($field_name, ':')) {
[
$field_name,
$dummy,
] = explode(':', $field_name, 2);
}
if ($field = FieldConfig::loadByName($this->mapping
->getDrupalEntityType(), $this->mapping
->getDrupalBundle(), $field_name)) {
$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;
}
}
}
}
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 = $instance
->getSettings();
$entity_type_id = $settings['target_type'];
$properties = [];
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
foreach ($this->entityTypeBundleInfo
->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
if (!empty($settings['handler_settings']['target_bundles']) && !in_array($bundle, $settings['handler_settings']['target_bundles'])) {
continue;
}
$properties += $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
}
}
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;
}
}