View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
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\SObject;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
use Drupal\typed_data\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PropertiesBase extends SalesforceMappingFieldPluginBase {
protected $dataFetcher;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin->dataFetcher = $container
->get('typed_data.data_fetcher');
return $plugin;
}
public function dataFetcher() {
if (empty($this->dataFetcher)) {
$this->dataFetcher = \Drupal::service('typed_data.data_fetcher');
}
return $this->dataFetcher;
}
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'], $this
->t('Salesforce field is required.'));
}
if (empty($config['drupal_field_value'])) {
$form_state
->setError($form['config']['drupal_field_value'], $this
->t('Drupal field is required.'));
}
}
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;
}
}
}
}
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') {
$data = $this
->getDataValue($entity, $this
->config('drupal_field_value'));
if (!empty($data)) {
$strings = [];
foreach ($data as $item) {
$strings[] = $item
->getString();
}
return implode(';', $strings);
}
}
else {
return $this
->getStringValue($entity, $this
->config('drupal_field_value'));
}
}
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
->dataFetcher()
->fetchDataByPropertyPath($entity
->getTypedData(), $field_selector);
$data
->setValue($pullValue);
return $data;
} catch (MissingDataException $e) {
} catch (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 getFieldDataDefinition(EntityInterface $entity) {
if (!strpos($this
->config('drupal_field_value'), '.')) {
return parent::getFieldDataDefinition($entity);
}
$data_definition = $this
->dataFetcher()
->fetchDefinitionByPropertyPath($entity
->getTypedData()
->getDataDefinition(), $this
->config('drupal_field_value'));
if ($data_definition instanceof ListDataDefinitionInterface) {
$data_definition = $data_definition
->getItemDefinition();
}
return $data_definition;
}
protected function getStringValue(EntityInterface $entity, $drupal_field_value) {
try {
$data = $this
->getDataValue($entity, $drupal_field_value);
return empty($data) ? NULL : $data
->getString();
} catch (\Exception $e) {
return NULL;
}
}
protected function getDataValue(EntityInterface $entity, $drupal_field_value) {
try {
return $this
->dataFetcher()
->fetchDataByPropertyPath($entity
->getTypedData(), $drupal_field_value);
} catch (\Exception $e) {
return NULL;
}
}
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;
}
}