Skip to content
Snippets Groups Projects
Commit 2fea75d3 authored by Salvador Molina's avatar Salvador Molina
Browse files

JavaScript refactor and support for migrate upgrade.

parent 69641f9d
No related branches found
No related tags found
No related merge requests found
...@@ -31,15 +31,14 @@ ...@@ -31,15 +31,14 @@
}); });
// Shows the relevant info to the user in the tooltip. // 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 limit = $(this).attr('data-soft-length-limit');
let minimum = $(this).attr('data-soft-length-minimum'); let minimum = $(this).attr('data-soft-length-minimum');
let val = $(this).val(); let val = $(this).val();
let remaining = limit - val.length;
let $tooltip = $(this).parent().find('.soft-length-limit-tooltip'); let $tooltip = $(this).parent().find('.soft-length-limit-tooltip');
let styleSelect = $(this).attr('data-soft-length-style-select'); let styleSelect = $(this).attr('data-soft-length-style-select');
...@@ -76,64 +75,76 @@ ...@@ -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. // The minimal / enhanced version of character limits.
if (styleSelect === '1') { if (styleSelect === '1') {
// No minimum treatment. // No minimum treatment.
$tooltip.html(Drupal.t('@val/@limit', { $tooltip.html(Drupal.t('@val/@limit', {
'@val': val.length, '@val': currentValue.length,
'@limit': limit '@limit': limit
})); }));
// Add class to tooltip for different CSS treatment (icons, text alignment, etc). // Add class to tooltip for different CSS treatment (icons, text alignment, etc).
$tooltip.parent().children('.soft-length-limit-tooltip').addClass('min-style-tooltip'); $tooltip.parent().children('.soft-length-limit-tooltip').addClass('min-style-tooltip');
return;
} }
// Original character limit treatment. // Original character limit treatment.
if ((styleSelect === '0') || (styleSelect === undefined)) { if ((styleSelect === '0') || (styleSelect === undefined)) {
// No minimum value is set. sll.updateLimitTreatmentText($tooltip, currentValue, minimum, limit);
if (minimum < 1) { }
if (val.length === 0) { };
$tooltip.html(Drupal.t('Content limited to @limit characters',{
'@limit': limit sll.updateLimitTreatmentText = function($tooltip, currentValue, minimum, limit) {
})); let remaining = limit - currentValue.length;
}
else if (remaining < 0) { // No minimum value is set.
$tooltip.html(Drupal.t('@limit character limit exceeded by @exceed characters.',{ if (minimum < 1) {
'@limit': limit, if (currentValue.length === 0) {
'@exceed': -remaining $tooltip.html(Drupal.t('Content limited to @limit characters',{
})); '@limit': limit
} }));
else { }
$tooltip.html(Drupal.t('Content limited to @limit characters. Remaining: @remaining',{ else if (remaining < 0) {
'@limit': limit, $tooltip.html(Drupal.t('@limit character limit exceeded by @exceed characters.',{
'@remaining': remaining '@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 { 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. Remaining: @remaining.',{
$tooltip.html(Drupal.t('Suggested minimum number of characters is @minimum, current count is @val. Content limited to @limit characters. ',{ '@limit': limit,
'@limit': limit, '@remaining': remaining,
'@minimum': minimum, '@minimum': minimum,
'@val': val.length '@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': 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
}));
}
} }
} }
}; };
......
...@@ -10,6 +10,9 @@ declare(strict_types = 1); ...@@ -10,6 +10,9 @@ declare(strict_types = 1);
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface; use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface; 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. * 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 ...@@ -157,3 +160,32 @@ function soft_length_limit_field_widget_form_alter(&$element, FormStateInterface
$element['#attached']['library'][] = 'soft_length_limit/soft_length_limit'; $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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment