Properties.php in Salesforce Suite 8.4
File
modules/salesforce_mapping/src/Plugin/SalesforceMappingField/Properties.php
View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\ListDataDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
class Properties extends PropertiesBase {
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 properties.'),
];
}
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 Drupal field or property to map to a Salesforce field.<br />Entity Reference fields should be handled using Related Entity Ids or Token field types.'),
];
}
return $pluginForm;
}
protected function getConfigurationOptions(SalesforceMappingInterface $mapping) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($mapping
->get('drupal_entity_type'), $mapping
->get('drupal_bundle'));
$options = [];
foreach ($field_definitions as $field_name => $field_definition) {
$label = $field_definition
->getLabel();
if ($this
->instanceOfEntityReference($field_definition)) {
continue;
}
else {
$property_definitions = $field_definition
->getFieldStorageDefinition()
->getPropertyDefinitions();
if (count($property_definitions) > 1) {
foreach ($property_definitions as $property => $property_definition) {
$options[(string) $label][$field_name . '.' . $property] = $label . ': ' . $property_definition
->getLabel();
}
}
else {
$options[$field_name] = $label;
}
}
}
asort($options);
return $options;
}
public function getPluginDefinition() {
$definition = parent::getPluginDefinition();
if ($field = FieldConfig::loadByName($this->mapping
->getDrupalEntityType(), $this->mapping
->getDrupalBundle(), $this
->config('drupal_field_value'))) {
$definition['config_dependencies']['config'][] = $field
->getConfigDependencyName();
}
return $definition;
}
}