Problem/Motivation

By default we see remaining length as "Remaining : 10", but for some people will be need see, how many symbols he have now and which maxlength for current field.

Proposed resolution

I propose add support custom template for remaining length row with "tokens", @maxlength, @remaining_count, @length.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nabiyllin created an issue. See original summary.

r.nabiullin’s picture

r.nabiullin’s picture

Status: Active » Needs review
FileSize
11.52 KB

With this patch will be opportunity use custom template for each field.

r.nabiullin’s picture

FileSize
13.62 KB

Same patch with schema.

Oleksiy’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/Field/FieldWidget/TextFieldCounterWidgetTrait.php
@@ -59,7 +71,16 @@ trait TextFieldCounterWidgetTrait {
+  public function addFieldFormElement(&$element, $entity, $fieldDefinition, $delta, $maxlength, $position, $template) {
+    $template = str_replace(
+      [
+        '@length',
+        '@remaining_count',
+      ],
+      [
+        '<span class="length">@length</span>',
+        '<span class="remaining_count">@remaining_count</span>',
+      ], $template);

These replace will override the template which will be used in the Drupal.t() later, that may create confusion with translations. Because a user will want to translate his template without any HTML code.
Maybe will be better to add this tags directly in Drupal.l() when the placeholders set, if they really need here. What do you think?

Jaypan’s picture

Assigned: Unassigned » Jaypan

  • Jaypan committed a28d432 on 8.x-1.x
    Issue #2971557 by nabiyllin, Oleksiy, Jaypan: Add support custom...
Jaypan’s picture

Status: Needs work » Fixed

I went at this a different way. In the interests of keeping all settings in a single location, I added this as a setting for the widget, so it can be configured on a per-instance basis. Available message variables are below. Note that the remaining count and current count must be wrapped in an element with the HTML class in the brackets, in order for the value to be auto updated as the user types.

@remaining_count (.remaining_count)
@current_count (.current_count)
@maxlength

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.