From 2fea75d3ae47101b4b91cf9f234a04e6d92c4373 Mon Sep 17 00:00:00 2001
From: Salvador Molina <inthorinrain@gmail.com>
Date: Tue, 16 Apr 2019 14:12:35 +0200
Subject: [PATCH] JavaScript refactor and support for migrate upgrade.

---
 js/soft_length_limit.js  | 105 +++++++++++++++++++++------------------
 soft_length_limit.module |  32 ++++++++++++
 2 files changed, 90 insertions(+), 47 deletions(-)

diff --git a/js/soft_length_limit.js b/js/soft_length_limit.js
index 32fb557..b835081 100644
--- a/js/soft_length_limit.js
+++ b/js/soft_length_limit.js
@@ -31,15 +31,14 @@
       });
 
       // Shows the relevant info to the user in the tooltip.
-      $softLengthElement.bind('textchange', sll.refreshTooltip);
+      $softLengthElement.bind('textchange', sll.recalculateTooltip);
     }
   };
 
-  sll.refreshTooltip = function(event, prevText) {
+  sll.recalculateTooltip = function(event, prevText) {
     let limit = $(this).attr('data-soft-length-limit');
     let minimum = $(this).attr('data-soft-length-minimum');
     let val = $(this).val();
-    let remaining = limit - val.length;
     let $tooltip = $(this).parent().find('.soft-length-limit-tooltip');
     let styleSelect = $(this).attr('data-soft-length-style-select');
 
@@ -76,64 +75,76 @@
       }
     }
 
+    sll.refreshTooltipText(styleSelect, $tooltip, val, minimum, limit);
+  };
+
+  sll.refreshTooltipText = function (styleSelect, $tooltip, currentValue, minimum, limit) {
     // The minimal / enhanced version of character limits.
     if (styleSelect === '1') {
       // No minimum treatment.
       $tooltip.html(Drupal.t('@val/@limit', {
-        '@val': val.length,
+        '@val': currentValue.length,
         '@limit': limit
       }));
+
       // Add class to tooltip for different CSS treatment (icons, text alignment, etc).
       $tooltip.parent().children('.soft-length-limit-tooltip').addClass('min-style-tooltip');
+      return;
     }
 
     // Original character limit treatment.
     if ((styleSelect === '0') || (styleSelect === undefined)) {
-      // No minimum value is set.
-      if (minimum < 1) {
-        if (val.length === 0) {
-          $tooltip.html(Drupal.t('Content limited to @limit characters',{
-            '@limit': limit
-          }));
-        }
-        else if (remaining < 0) {
-          $tooltip.html(Drupal.t('@limit character limit exceeded by @exceed characters.',{
-            '@limit': limit,
-            '@exceed': -remaining
-          }));
-        }
-        else {
-          $tooltip.html(Drupal.t('Content limited to @limit characters. Remaining: @remaining',{
-            '@limit': limit,
-            '@remaining': remaining
-          }));
-        }
+      sll.updateLimitTreatmentText($tooltip, currentValue, minimum, limit);
+    }
+  };
+
+  sll.updateLimitTreatmentText = function($tooltip, currentValue, minimum, limit) {
+    let remaining = limit - currentValue.length;
+
+    // No minimum value is set.
+    if (minimum < 1) {
+      if (currentValue.length === 0) {
+        $tooltip.html(Drupal.t('Content limited to @limit characters',{
+          '@limit': limit
+        }));
+      }
+      else if (remaining < 0) {
+        $tooltip.html(Drupal.t('@limit character limit exceeded by @exceed characters.',{
+          '@limit': limit,
+          '@exceed': -remaining
+        }));
+      }
+      else {
+        $tooltip.html(Drupal.t('Content limited to @limit characters. Remaining: @remaining',{
+          '@limit': limit,
+          '@remaining': remaining
+        }));
+      }
+    }
+    // There is a minimum length set.
+    else {
+      if (currentValue.length === 0) {
+        $tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val.  Content limited to @limit characters. ',{
+          '@limit': limit,
+          '@minimum': minimum,
+          '@val': currentValue.length
+        }));
+      }
+      else if (remaining < 0) {
+        $tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val.  @limit character limit exceeded by @exceed characters.',{
+          '@limit': limit,
+          '@exceed': -remaining,
+          '@minimum': minimum,
+          '@val': currentValue.length
+        }));
       }
-      // There is a minimum length set.
       else {
-        if (val.length === 0) {
-          $tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val.  Content limited to @limit characters. ',{
-            '@limit': limit,
-            '@minimum': minimum,
-            '@val': val.length
-          }));
-        }
-        else if (remaining < 0) {
-          $tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val.  @limit character limit exceeded by @exceed characters.',{
-            '@limit': limit,
-            '@exceed': -remaining,
-            '@minimum': minimum,
-            '@val': val.length
-          }));
-        }
-        else {
-          $tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val.  Content limited to @limit characters. Remaining: @remaining.',{
-            '@limit': limit,
-            '@remaining': remaining,
-            '@minimum': minimum,
-            '@val': val.length
-          }));
-        }
+        $tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val.  Content limited to @limit characters. Remaining: @remaining.',{
+          '@limit': limit,
+          '@remaining': remaining,
+          '@minimum': minimum,
+          '@val': currentValue.length
+        }));
       }
     }
   };
diff --git a/soft_length_limit.module b/soft_length_limit.module
index 8504b24..c2d59d1 100644
--- a/soft_length_limit.module
+++ b/soft_length_limit.module
@@ -10,6 +10,9 @@ declare(strict_types = 1);
 use Drupal\Core\Field\FieldDefinitionInterface;
 use Drupal\Core\Field\WidgetInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\migrate\Plugin\MigrateSourceInterface;
+use Drupal\migrate\Plugin\MigrationInterface;
+use Drupal\migrate\Row;
 
 /**
  * Returns the widget settings that can be used for a soft_length widget.
@@ -157,3 +160,32 @@ function soft_length_limit_field_widget_form_alter(&$element, FormStateInterface
 
   $element['#attached']['library'][] = 'soft_length_limit/soft_length_limit';
 }
+
+/**
+ * Implements hook_migrate_prepare_row().
+ */
+function soft_length_limit_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
+  // Add migrate support to migrate D7 settings into D8 fields.
+  $text_fields = [
+    'text_textarea',
+    'text_textfield',
+    'text_textarea_with_summary',
+    'text_long',
+  ];
+
+  if ($source->getPluginId() !== 'd7_field_instance_per_form_display' || !in_array($row->getSource()['type'], $text_fields)) {
+    return;
+  }
+
+  $widget_settings = $row->getSourceProperty('widget')['settings'];
+
+  if (!empty($widget_settings['soft_length_limit']) || !empty($widget_settings['soft_length_minimum'])) {
+    $constants = $row->getSourceProperty('constants');
+    $constants['third_party_settings']['soft_length_limit'] = [
+      'max_limit' => $widget_settings['soft_length_limit'],
+      'minimum_limit' => $widget_settings['soft_length_minimum'],
+      'style_select' => $widget_settings['soft_length_style_select'],
+    ];
+    $row->setSourceProperty('constants', $constants);
+  }
+}
-- 
GitLab