Commit 00adc978 authored by Gabriel Carleton-Barnes's avatar Gabriel Carleton-Barnes Committed by Aaron Bauman
Browse files

Issue #3165707 by gcb, AaronBauman, olivier.bouwman: Allow for multiple items...

Issue #3165707 by gcb, AaronBauman, olivier.bouwman: Allow for multiple items when mapping related term strings
parent 278779be
Loading
Loading
Loading
Loading
+32 −20
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ class RelatedTermString extends SalesforceMappingFieldPluginBase {
        '#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.'),
        '#description' => $this->t('Select a taxonomy reference field.<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;
@@ -71,7 +71,10 @@ class RelatedTermString extends SalesforceMappingFieldPluginBase {
    }

    // Map the term name to the salesforce field.
    return $field->entity->getName();
    foreach ($field->referencedEntities() as $referencedEntity) {
      $referencedEntities[] = $referencedEntity->getName();
    }
    return $referencedEntities;
  }

  /**
@@ -98,29 +101,38 @@ class RelatedTermString extends SalesforceMappingFieldPluginBase {
    // Get the appropriate vocab from the field settings.
    $vocabs = $instance->getSetting('handler_settings')['target_bundles'];

    if (empty($vocabs)) {
      return;
    }

    // If this is a multi-value field, split the value from Salesforce into parts.
    $field_values = explode(";", $value);

    foreach ($field_values as $field_value) {
      // Look for a term that matches the string in the salesforce field.
      $query = \Drupal::entityQuery('taxonomy_term');
      $query->condition('vid', $vocabs, 'IN');
    $query->condition('name', $value);
      $query->condition('name', $field_value);
      $tids = $query->execute();

      if (!empty($tids)) {
      $term_id = reset($tids);
        $term_ids[] = reset($tids);
      }

      // If we cant find an existing term, create a new one.
    if (empty($term_id)) {
      if (empty($tids)) {
        $vocab = reset($vocabs);

        $term = Term::create([
        'name' => $value,
          'name' => $field_value,
          'vid' => $vocab,
        ]);
        $term->save();
      $term_id = $term->id();
        $term_ids[] = $term->id();
      }
    }

    return $term_id;
    return $term_ids;
  }

  /**
+2 −0
Original line number Diff line number Diff line
@@ -218,6 +218,8 @@ abstract class SalesforceMappingFieldPluginBase extends PluginBase implements Sa
        $value = (int) $value;
        break;

      // Picklists are single-value, but can submit their values as arrays.
      case 'picklist':
      case 'multipicklist':
        if (is_array($value)) {
          $value = implode(';', $value);
+28 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_salesforce_test_tax_singl
    - node.type.salesforce_mapping_test_content
    - taxonomy.vocabulary.salesforce_test_vocabulary
id: node.salesforce_mapping_test_content.field_salesforce_test_tax_singl
field_name: field_salesforce_test_tax_singl
entity_type: node
bundle: salesforce_mapping_test_content
label: 'Salesforce Test Taxonomy Ref - single value'
description: ''
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  handler: 'default:taxonomy_term'
  handler_settings:
    target_bundles:
      salesforce_test_vocabulary: salesforce_test_vocabulary
    sort:
      field: name
      direction: asc
    auto_create: false
    auto_create_bundle: ''
field_type: entity_reference
+19 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - node
    - taxonomy
id: node.field_salesforce_test_tax_singl
field_name: field_salesforce_test_tax_singl
entity_type: node
type: entity_reference
settings:
  target_type: taxonomy_term
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false
+13 −0
Original line number Diff line number Diff line
@@ -105,6 +105,19 @@ field_mappings:
    direction: sync
    description: ''
    id: 9
  -
    drupal_field_type: RelatedTermString
    drupal_field_value: field_salesforce_test_tax_ref
    salesforce_field: d5__Test_Multipicklist__c
    direction: drupal_sf
    id: 10
  -
    drupal_field_type: RelatedTermString
    drupal_field_value: field_salesforce_test_tax_singl
    salesforce_field: LeadSource
    direction: drupal_sf
    id: 11

push_limit: 0
push_retries: 3
push_frequency: 0
Loading