RelatedTermString.php in Salesforce Suite 5.0.x
File
modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedTermString.php
View source
<?php
namespace Drupal\salesforce_mapping\Plugin\SalesforceMappingField;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\salesforce\SObject;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase;
use Drupal\salesforce\Exception as SalesforceException;
use Drupal\taxonomy\Entity\Term;
class RelatedTermString 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 taxonomy 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 taxonomy reference field.<br />If more than one term is referenced, the term at delta zero will be used.<br />A taxonomy reference field will be used to sync to the term name.<br />If a term with the given string does not exist one will be created.'),
];
}
return $pluginForm;
}
public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
$field_name = $this
->config('drupal_field_value');
$instances = $this->entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
if (empty($instances[$field_name])) {
return;
}
$field = $entity
->get($field_name);
if (empty($field
->getValue()) || is_null($field->entity)) {
return;
}
return $field->entity
->getName();
}
public function pullValue(SObject $sf_object, EntityInterface $entity, SalesforceMappingInterface $mapping) {
if (!$this
->pull() || empty($this
->config('salesforce_field'))) {
throw new SalesforceException('No data to pull. Salesforce field mapping is not defined.');
}
$field_name = $this
->config('drupal_field_value');
$instance = FieldConfig::loadByName($this->mapping
->getDrupalEntityType(), $this->mapping
->getDrupalBundle(), $field_name);
if (empty($instance)) {
return;
}
$value = $sf_object
->field($this
->config('salesforce_field'));
if (empty($value)) {
return NULL;
}
$vocabs = $instance
->getSetting('handler_settings')['target_bundles'];
$query = \Drupal::entityQuery('taxonomy_term');
$query
->condition('vid', $vocabs, 'IN');
$query
->condition('name', $value);
$tids = $query
->execute();
if (!empty($tids)) {
$term_id = reset($tids);
}
if (empty($term_id)) {
$vocab = reset($vocabs);
$term = Term::create([
'name' => $value,
'vid' => $vocab,
]);
$term
->save();
$term_id = $term
->id();
}
return $term_id;
}
private function getConfigurationOptions($mapping) {
$instances = $this->entityFieldManager
->getFieldDefinitions($mapping
->get('drupal_entity_type'), $mapping
->get('drupal_bundle'));
$options = [];
foreach ($instances as $name => $instance) {
$hand = $instance
->getSetting('handler');
if ($hand != "default:taxonomy_term") {
continue;
}
$options[$name] = $instance
->getLabel();
}
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();
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;
}
}
}
}
}