Commit 190ef279 authored by git's avatar git Committed by Clayton Dewey
Browse files

Issue #2916687 by Renrhaf, joshua.boltz, DrupalHack, Hydra, cedewey, dunot,...

Issue #2916687 by Renrhaf, joshua.boltz, DrupalHack, Hydra, cedewey, dunot, ricovandevin, Publishing Future, showardclark, dalin, srdtwc: Soft limit does not work on plain text fields because the Maxlength HTML attribute is used
parent 545440d1
Loading
Loading
Loading
Loading
+4 −0
Changes for config/schema/maxlength.schema.yml: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -15,3 +15,7 @@ field.widget.third_party.maxlength:
      type: boolean
    maxlength_js_truncate_html:
      type: boolean
    maxlength_js_enforce_attribute:
      type: boolean
    maxlength_js_summary_enforce_attribute:
      type: boolean
+3 −3
Changes for js/maxlength.js: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
    },
    detach: function(context, settings) {
      var $context = $(context);
      $context.find('.maxlength').removeOnce('maxlength').each(function () {
      $context.find('.maxlength').removeOnce('data-maxlength').each(function () {
        $(this).charCount({
          action: 'detach'
        });
@@ -52,7 +52,7 @@
   */
  ml.calculate = function(obj, options, count, wysiwyg, getter, setter) {
    var counter = $('#' + obj.attr('id') + '-' + options.css);
    var limit = parseInt(obj.attr('maxlength'));
    var limit = parseInt(obj.attr('data-maxlength'));

    if (count == undefined) {
      if (options.truncateHtml) {
@@ -258,7 +258,7 @@
    ml.options[$(this).attr('id')] = options;

    if (options.action == 'detach') {
      $(this).removeOnce('maxlength');
      $(this).removeOnce('data-maxlength');
      $('#' + $(this).attr('id') + '-' + options.css).remove();
      delete ml.options[$(this).attr('id')];
      return 'removed';
+12 −6
Changes for maxlength.module: 12 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -47,9 +47,10 @@ function maxlength_element_info_alter(&$cache) {
 */
function maxlength_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
  $plugin_id = $plugin->getPluginId();
  // Depeding on the widget, we may have different settings.
  // Depending on the widget, we may have different settings.
  $widget_settings = \Drupal::service('maxlength.widget_settings');
  $allowed_settings = $widget_settings->getAllowedSettings($plugin_id);

  if (!empty($allowed_settings)) {
    $element = array();
    if (!empty($allowed_settings['maxlength_setting'])) {
@@ -136,7 +137,7 @@ function maxlength_field_widget_form_alter(&$element, FormStateInterface $form_s

    if (isset($thirdPartySettings['maxlength']['maxlength_js_summary']) && $thirdPartySettings['maxlength']['maxlength_js_summary'] > 0) {
      $maxlength_js = $thirdPartySettings['maxlength']['maxlength_js_summary'];
      $element['summary']['#attributes']['maxlength'] = $maxlength_js;
      $element['summary']['#attributes']['data-maxlength'] = $maxlength_js;

      $maxlength_js_label = !empty($thirdPartySettings['maxlength']['maxlength_js_label_summary']) ? $thirdPartySettings['maxlength']['maxlength_js_label_summary'] : t('Content limited to @limit characters, remaining: <strong>@remaining</strong>');
      $element['summary']['#attributes']['maxlength_js_label'][] = $maxlength_js_label;
@@ -144,8 +145,8 @@ function maxlength_field_widget_form_alter(&$element, FormStateInterface $form_s

    if (isset($thirdPartySettings['maxlength']['maxlength_js']) && $thirdPartySettings['maxlength']['maxlength_js'] > 0) {
      $maxlength_js = $thirdPartySettings['maxlength']['maxlength_js'];
      $element['value']['#attributes']['maxlength'] = $maxlength_js;
      $element['#attributes']['maxlength'] = $maxlength_js;
      $element['value']['#attributes']['data-maxlength'] = $maxlength_js;
      $element['#attributes']['data-maxlength'] = $maxlength_js;

      $maxlength_js_label = !empty($thirdPartySettings['maxlength']['maxlength_js_label']) ? $thirdPartySettings['maxlength']['maxlength_js_label'] : t('Content limited to @limit characters, remaining: <strong>@remaining</strong>');
      $element['#attributes']['maxlength_js_label'][] = $maxlength_js_label;
@@ -160,10 +161,15 @@ function maxlength_field_widget_form_alter(&$element, FormStateInterface $form_s
        // Set title maxlength attributes.
        $element['title']['#attributes']['class'][] = 'maxlength';
        $element['title']['#attached']['library'][] = 'maxlength/maxlength';
        $element['title']['value']['#attributes']['maxlength'] = $maxlength_js;
        $element['title']['value']['#attributes']['data-maxlength'] = $maxlength_js;
        $element['title']['value']['#attributes']['maxlength_js_label'][] = $maxlength_js_label;
        $element['title']['#attributes']['maxlength'] = $maxlength_js;
        $element['title']['#attributes']['data-maxlength'] = $maxlength_js;
        $element['title']['#attributes']['maxlength_js_label'][] = $maxlength_js_label;

        if ($enforceAttribute) {
          $element['title']['value']['#attributes']['maxlength'] = $maxlength_js;
          $element['title']['#attributes']['maxlength'] = $maxlength_js;
        }
      }
    }
    if (isset($thirdPartySettings['maxlength']['maxlength_js_enforce']) && $thirdPartySettings['maxlength']['maxlength_js_enforce']) {
+2 −2
Changes for src/MaxLengthCallbacks.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -35,11 +35,11 @@ class MaxLengthCallbacks implements TrustedCallbackInterface {
    }

    if (isset($element['#maxlength_js']) && $element['#maxlength_js'] === TRUE) {
      if (((isset($element['#attributes']['maxlength']) && $element['#attributes']['maxlength'] > 0))) {
      if (isset($element['#attributes']['data-maxlength']) && $element['#attributes']['data-maxlength'] > 0) {
        $element['#attributes']['class'][] = 'maxlength';
        $element['#attached']['library'][] = 'maxlength/maxlength';
      }
      if (((isset($element['summary']['#attributes']['maxlength']) && $element['summary']['#attributes']['maxlength'] > 0))) {
      if (isset($element['summary']['#attributes']['data-maxlength']) && $element['summary']['#attributes']['data-maxlength'] > 0) {
        $element['summary']['#attributes']['class'][] = 'maxlength';
        $element['summary']['#attached']['library'][] = 'maxlength/maxlength';
      }