diff --git a/modules/salesforce_mapping/js/autocomplete.js b/modules/salesforce_mapping/js/autocomplete.js deleted file mode 100644 index 73f982ebd69fbef10c66100e6a86ea5078085c2c..0000000000000000000000000000000000000000 --- a/modules/salesforce_mapping/js/autocomplete.js +++ /dev/null @@ -1,171 +0,0 @@ -/** - * @file - * Copy of Rules module's autocomplete.js. - */ - -(function ($, Drupal) { - - 'use strict'; - - var autocomplete; - - /** - * JQuery UI autocomplete source callback. - * - * @param {object} request - * The request object. - * @param {function} response - * The function to call with the response. - */ - function sourceData(request, response) { - var elementId = this.element.attr('id'); - - if (!(elementId in autocomplete.cache)) { - autocomplete.cache[elementId] = {}; - } - - /** - * Transforms the data object into an array and update autocomplete results. - * - * @param {object} data - * The data sent back from the server. - */ - function sourceCallbackHandler(data) { - autocomplete.cache[elementId][term] = data; - - response(data); - } - - // Get the desired term and construct the autocomplete URL for it. - var term = request.term; - - // Check if the term is already cached. - if (autocomplete.cache[elementId].hasOwnProperty(term)) { - response(autocomplete.cache[elementId][term]); - } - else { - var options = $.extend({success: sourceCallbackHandler, data: {q: term}}, autocomplete.ajax); - $.ajax(this.element.attr('data-autocomplete-path'), options); - } - } - - /** - * Handles an autocompletefocus event. - * - * @return {bool} - * Always returns false. - */ - function focusHandler() { - return false; - } - - /** - * Handles the autocomplete selection event. - * - * Restarts autocompleting when the selection ends in a dot, for nested data - * selectors. - * - * @param {object} event - * The event object. - * @param {object} ui - * The UI object holding the selected value. - */ - function selectHandler(event, ui) { - var input_value = ui.item.value; - if (input_value.substr(input_value.length - 1) === '.') { - $(event.target).trigger('keydown'); - } - } - - /** - * Override jQuery UI _renderItem function to output HTML by default. - * - * @param {jQuery} ul - * jQuery collection of the ul element. - * @param {object} item - * The list item to append. - * - * @return {jQuery} - * jQuery collection of the ul element. - */ - function renderItem(ul, item) { - return $('<li>') - .append($('<a>').html(item.label)) - .appendTo(ul); - } - - /** - * Attaches the autocomplete behavior to all required fields. - * - * @type {Drupal~behavior} - * - * @prop {Drupal~behaviorAttach} attach - * Attaches the autocomplete behaviors. - * @prop {Drupal~behaviorDetach} detach - * Detaches the autocomplete behaviors. - */ - Drupal.behaviors.salesforce_mapping_autocomplete = { - attach: function (context) { - // Act on textfields with the "salesforce-mapping-autocomplete" class. - var $autocomplete = $(context).find('input.salesforce-mapping-autocomplete').once('autocomplete'); - if ($autocomplete.length) { - - var closing = false; - - $.extend(autocomplete.options, { - close: function() { - // Avoid double-pop-up issue. - closing = true; - setTimeout(function() { - closing = false; - }, 300); - } - }); - // Use jQuery UI Autocomplete on the textfield. - $autocomplete.autocomplete(autocomplete.options) - .each(function() { - $(this).data('ui-autocomplete')._renderItem = autocomplete.options.renderItem; - // Immediately pop out the autocomplete when the field gets focus. - $(this).focus(function() { - if (!closing) { - $(this).autocomplete('search'); - } - }); - }); - } - }, - detach: function (context, settings, trigger) { - if (trigger === 'unload') { - $(context).find('input.salesforce-mapping-autocomplete') - .removeOnce('autocomplete') - .autocomplete('destroy'); - } - } - }; - - /** - * Autocomplete object implementation. - * - * @namespace Drupal.autocomplete - */ - autocomplete = { - cache: {}, - - /** - * JQuery UI option object. - * - * @name Drupal.autocomplete.options - */ - options: { - source: sourceData, - focus: focusHandler, - select: selectHandler, - renderItem: renderItem, - minLength: 0 - }, - ajax: { - dataType: 'json' - } - }; - -})(jQuery, Drupal); diff --git a/modules/salesforce_mapping/salesforce_mapping.libraries.yml b/modules/salesforce_mapping/salesforce_mapping.libraries.yml deleted file mode 100644 index f8ea41a0eeea96b66f76ef1a0f34ec181e78ccf7..0000000000000000000000000000000000000000 --- a/modules/salesforce_mapping/salesforce_mapping.libraries.yml +++ /dev/null @@ -1,9 +0,0 @@ -salesforce_mapping.autocomplete: - js: - js/autocomplete.js: { weight: -1 } - dependencies: - - core/jquery - - core/drupal - - core/drupalSettings - - core/drupal.ajax - - core/jquery.ui.autocomplete diff --git a/modules/salesforce_mapping/src/Form/SalesforceMappingFieldsForm.php b/modules/salesforce_mapping/src/Form/SalesforceMappingFieldsForm.php index c9b8e0f27e0501e3989bbd1c0a9e2f2f14dbf299..13c23e404e03dbad5da39737ec9b951321ada1a4 100644 --- a/modules/salesforce_mapping/src/Form/SalesforceMappingFieldsForm.php +++ b/modules/salesforce_mapping/src/Form/SalesforceMappingFieldsForm.php @@ -22,6 +22,9 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase { $form['#entity'] = $this->entity; $form['#attached']['library'][] = 'salesforce/admin'; + // This needs to be loaded now as it can't be loaded via AJAX for the AC enabled fields. + $form['#attached']['library'][] = 'core/drupal.autocomplete'; + // For each field on the map, add a row to our table. $form['overview'] = ['#markup' => 'Field mapping overview goes here.']; diff --git a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php index 9c09ca1596b3ea815d7df9fab598e0b182ae4eab..ff494daa14bc3822c98f8ec58ac1c406153d1417 100644 --- a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php +++ b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/PropertiesExtended.php @@ -16,7 +16,6 @@ use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\Exception\MissingDataException; use Drupal\Core\TypedData\ListDataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; -use Drupal\Core\Url; use Drupal\salesforce\Rest\RestClientInterface; use Drupal\salesforce\SObject; use Drupal\salesforce_mapping\SalesforceMappingFieldPluginBase; @@ -145,10 +144,8 @@ class PropertiesExtended extends SalesforceMappingFieldPluginBase { $element = &$pluginForm[$context_name]['setting']; if ($mode == 'selector') { $element['#description'] = $this->t("The data selector helps you drill down into the data available."); - $url = Url::fromRoute('salesforce_mapping.autocomplete_controller_autocomplete', ['entity_type_id' => $mapping->get('drupal_entity_type'), 'bundle' => $mapping->get('drupal_bundle')]); - $element['#attributes']['class'][] = 'salesforce-mapping-autocomplete'; - $element['#attributes']['data-autocomplete-path'] = $url->toString(); - $element['#attached']['library'][] = 'salesforce_mapping/salesforce_mapping.autocomplete'; + $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'] = [ @@ -364,7 +361,7 @@ class PropertiesExtended extends SalesforceMappingFieldPluginBase { } /** - * Submit callback: switch a context to data selecor or direct input mode. + * Submit callback: switch a context to data selector or direct input mode. */ public static function switchContextMode(array &$form, FormStateInterface $form_state) { $element_name = $form_state->getTriggeringElement()['#name'];