Commit 76fcb943 authored by Christian Fritsch's avatar Christian Fritsch Committed by Stephan Zeidler
Browse files

Issue #3265174 by chr.fritsch: Remove jQuery dependency

parent fdf8d8cf
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
name: 'Autofill'
type: module
description: 'A field can automatically be autofilled while typing into another one.'
core_version_requirement: ^8 || ^9
core_version_requirement: ^9.2 || ^10
+1 −1
Original line number Diff line number Diff line
@@ -3,4 +3,4 @@ autofill:
    js/autofill.js: {}
  dependencies:
    - core/drupal
    - core/jquery.once
    - core/once
+2 −2
Original line number Diff line number Diff line
@@ -125,9 +125,9 @@ function _autofill_get_available_source_fields_as_options(array $form, string $c
}

/**
 * Implements hook_field_widget_form_alter().
 * Implements hook_field_widget_single_element_form_alter().
 */
function autofill_field_widget_form_alter(array &$element, FormStateInterface $form_state, array $context): void {
function autofill_field_widget_single_element_form_alter(array &$element, FormStateInterface $form_state, array $context): void {
  /** @var \Drupal\Core\Field\WidgetInterface $widget */
  $widget = $context['widget'];
  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
+39 −43
Original line number Diff line number Diff line
(function ($, Drupal) {
((Drupal, once) => {

  Drupal.behaviors.autofillFromAnotherField = {
    attach: function (context, settings) {
      if (typeof settings.autofill.field_mapping !== 'undefined') {
        var field_mapping = settings.autofill.field_mapping;
        var target_field_was_manipulated = [];
        for (var target_field in field_mapping) {
          var source_field = field_mapping[target_field];
    attach: (context, settings) => {
      let target_field_was_manipulated = [];
      Object.keys(settings.autofill.field_mapping || {}).forEach((target_field) => {
        const source_field = settings.autofill.field_mapping[target_field];

          var $source_field = $('[name="' + source_field + '[0][value]"]', context);
          var $target_field = $('[name="' + target_field + '[0][value]"]', context);
        // Only process if source field and target field are present.
        const [source_field_element] = once('autofill_' + source_field + '_' + target_field, context.querySelector('[name="' + source_field + '[0][value]"]'));
        if (!source_field_element) {
          return;
        }

        const target_field_element = context.querySelector('[name="' + target_field + '[0][value]"]');
        if (!target_field_element) {
          return;
        }
        target_field_was_manipulated[target_field] = false;

          // Only process if source field and target field are present.
          if ($source_field.length > 0 && $target_field.length > 0) {
        // Automatically fill target field with value of the source
        // field, when it's empty or values are identical.
            if (!$target_field.val() || $source_field.val() === $target_field.val()) {
              var unique_process_name = 'autofill_' + source_field + '_' + target_field;
              $source_field.once(unique_process_name).on('input', {
                source_field: source_field,
                target_field: target_field
              }, function (e) {
        if (!source_field_element.value || source_field_element.value === target_field_element.value) {
          source_field_element.addEventListener('input', () => {
            // Autofill the target field only when it was not manipulated
            // before.
                if (!target_field_was_manipulated[e.data.target_field]) {
                  var $source_field = $('[name="' + e.data.source_field + '[0][value]"]', context);
                  var $target_field = $('[name="' + e.data.target_field + '[0][value]"]', context);
                  $target_field.val($source_field.val());
            if (!target_field_was_manipulated[target_field]) {
              target_field_element.value = source_field_element.value;
              // Trigger input event, to fire additional events, like
              // length indicator.
                  $target_field.trigger('input');
              target_field_element.dispatchEvent(new Event('input'));
            }
          });
        }
@@ -40,13 +38,11 @@

        // Store, when target field was manipulated manually. Then we
        // should not process the autofill again.
            $target_field.on('keypress', {target_field: target_field}, function (e) {
              target_field_was_manipulated[e.data.target_field] = true;
        target_field_element.addEventListener('keypress', () => {
          target_field_was_manipulated[target_field] = true;
        });
      });
          }
        }
      }
    }
  };

})(jQuery, Drupal);
})(Drupal, once);