View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\ListDataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\salesforce\Rest\RestClientInterface;
use Drupal\salesforce\SObject;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PropertiesExtended extends SalesforceMappingFieldPluginBase {
protected $moduleHandler;
protected $dataFetcher;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, RestClientInterface $rest_client, EntityManagerInterface $entity_manager, EntityTypeManagerInterface $etm, DateFormatterInterface $dateFormatter, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_bundle_info, $entity_field_manager, $rest_client, $entity_manager, $etm, $dateFormatter, $event_dispatcher);
$this->moduleHandler = $moduleHandler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.bundle.info'), $container
->get('entity_field.manager'), $container
->get('salesforce.client'), $container
->get('entity.manager'), $container
->get('entity_type.manager'), $container
->get('date.formatter'), $container
->get('event_dispatcher'), $container
->get('module_handler'));
}
public function getDependencies(SalesforceMappingInterface $mapping) {
return [
'module' => [
'typed_data',
],
];
}
public static function isAllowed(SalesforceMappingInterface $mapping) {
return \Drupal::service('module_handler')
->moduleExists('typed_data');
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$pluginForm = parent::buildConfigurationForm($form, $form_state);
if (!$this->moduleHandler
->moduleExists('typed_data')) {
$this
->messenger()
->addError('Install Typed Data module to use Extended Properties fields.');
return $this
->buildBrokenConfigurationForm($pluginForm, $form_state);
}
$mapping = $form['#entity'];
$context_name = 'drupal_field_value';
$mode = $form_state
->get('context_' . $context_name);
if (!$mode) {
$mode = 'selector';
$form_state
->set('context_' . $context_name, $mode);
}
$title = $mode == 'selector' ? $this
->t('Data selector') : $this
->t('Value');
$pluginForm[$context_name]['setting'] = [
'#type' => 'textfield',
'#title' => $title,
'#attributes' => [
'class' => [
'drupal-field-value',
],
],
'#default_value' => $this
->config('drupal_field_value'),
];
$element =& $pluginForm[$context_name]['setting'];
if ($mode == 'selector') {
$element['#description'] = $this
->t("The data selector helps you drill down into the data available.");
$element['#autocomplete_route_name'] = 'salesforce_mapping.autocomplete_controller_autocomplete';
$element['#autocomplete_route_parameters'] = [
'entity_type_id' => $mapping
->get('drupal_entity_type'),
'bundle' => $mapping
->get('drupal_bundle'),
];
}
$value = $mode == 'selector' ? $this
->t('Switch to the direct input mode') : $this
->t('Switch to data selection');
$pluginForm[$context_name]['switch_button'] = [
'#type' => 'submit',
'#name' => 'context_' . $context_name,
'#attributes' => [
'class' => [
'drupal-field-switch-button',
],
],
'#parameter' => $context_name,
'#value' => $value,
'#submit' => [
static::class . '::switchContextMode',
],
'#limit_validation_errors' => [],
];
return $pluginForm;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$vals = $form_state
->getValues();
$config = $vals['config'];
if (empty($config['salesforce_field'])) {
$form_state
->setError($form['config']['salesforce_field'], t('Salesforce field is required.'));
}
if (empty($config['drupal_field_value'])) {
$form_state
->setError($form['config']['drupal_field_value'], t('Drupal field is required.'));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$config_value = $form_state
->getValue('config');
$config_value['drupal_field_value'] = $config_value['drupal_field_value']['setting'];
$form_state
->setValue('config', $config_value);
}
public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
$describe = $this->salesforceClient
->objectDescribe($mapping
->getSalesforceObjectType());
$field_definition = $describe
->getField($this
->config('salesforce_field'));
if ($field_definition['type'] == 'multipicklist') {
$values = [];
foreach ($entity
->get($this
->config('drupal_field_value')) as $value) {
$values[] = $this
->getStringValue($entity, $value);
}
return implode(';', $values);
}
else {
return $this
->getStringValue($entity, $this
->config('drupal_field_value'));
}
}
protected function getDataFetcher() {
if (!\Drupal::hasService('typed_data.data_fetcher')) {
throw new \Exception('Module typed_data must be installed to use Extended Properties');
}
if (empty($this->dataFetcher)) {
$this->dataFetcher = \Drupal::service('typed_data.data_fetcher');
}
return $this->dataFetcher;
}
public function pullValue(SObject $sf_object, EntityInterface $entity, SalesforceMappingInterface $mapping) {
$field_selector = $this
->config('drupal_field_value');
$pullValue = parent::pullValue($sf_object, $entity, $mapping);
try {
$data = $this
->getDataFetcher()
->fetchDataByPropertyPath($entity
->getTypedData(), $field_selector);
$data
->setValue($pullValue);
return $data;
} catch (MissingDataException $e) {
} catch (\Drupal\typed_data\Exception\InvalidArgumentException $e) {
}
$parts = explode('.', $field_selector, 4);
switch (count($parts)) {
case 1:
$entity
->set($field_selector, $pullValue);
return $entity
->getTypedData()
->get($field_selector);
case 2:
$field_name = $parts[0];
$delta = 0;
$property = $parts[1];
break;
case 3:
$field_name = $parts[0];
$delta = $parts[1];
$property = $parts[2];
if (!is_numeric($delta)) {
return;
}
break;
case 4:
return;
}
$list_data = $entity
->get($field_name);
if (!$list_data
->get($delta) instanceof TypedDataInterface) {
$list_data
->set($delta, []);
}
$typed_data = $list_data
->get($delta);
if ($typed_data instanceof ComplexDataInterface && $property) {
if (!$typed_data
->get($property) instanceof TypedDataInterface) {
$typed_data
->set($property, []);
}
$typed_data = $typed_data
->get($property);
}
if (!$typed_data instanceof TypedDataInterface) {
return;
}
$typed_data
->setValue($pullValue);
return $typed_data
->getParent();
}
protected function getStringValue(EntityInterface $entity, $drupal_field_value) {
try {
return $this
->getDataFetcher()
->fetchDataByPropertyPath($entity
->getTypedData(), $drupal_field_value)
->getString();
} catch (\Exception $e) {
return NULL;
}
}
protected function getFieldDataDefinition(EntityInterface $entity) {
$data_definition = $this
->getDataFetcher()
->fetchDefinitionByPropertyPath($entity
->getTypedData()
->getDataDefinition(), $this
->config('drupal_field_value'));
if ($data_definition instanceof ListDataDefinitionInterface) {
$data_definition = $data_definition
->getItemDefinition();
}
return $data_definition;
}
protected function getDrupalFieldType(DataDefinitionInterface $data_definition) {
$field_main_property = $data_definition;
if ($data_definition instanceof ComplexDataDefinitionInterface) {
$field_main_property = $data_definition
->getPropertyDefinition($data_definition
->getMainPropertyName());
}
return $field_main_property ? $field_main_property
->getDataType() : NULL;
}
public static function switchContextMode(array &$form, FormStateInterface $form_state) {
$element_name = $form_state
->getTriggeringElement()['#name'];
$mode = $form_state
->get($element_name);
$switched_mode = $mode == 'selector' ? 'input' : 'selector';
$form_state
->set($element_name, $switched_mode);
$form_state
->setRebuild();
}
}