diff -u b/core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php b/core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php --- b/core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php +++ b/core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php @@ -34,9 +34,10 @@ */ public static function defaultSettings() { return [ - 'wordsafe' => FALSE, - 'add_elipsis' => FALSE, + 'add_ellipsis' => FALSE, + 'min_wordsafe_length' => 1, 'trim_length' => '600', + 'wordsafe' => FALSE, ] + parent::defaultSettings(); } @@ -44,18 +45,6 @@ * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $element['wordsafe'] = [ - '#title' => t('Wordsafe'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('wordsafe'), - '#description' => t('Trim on word boundaries.'), - ]; - $element['add_elipsis'] = [ - '#title' => t('Add elipsis'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('add_elipsis'), - '#description' => t('Displays an elipsis after trimmed text.'), - ]; $element['trim_length'] = [ '#title' => t('Trimmed limit'), '#type' => 'number', @@ -65,6 +54,33 @@ '#min' => 1, '#required' => TRUE, ]; + $element['add_ellipsis'] = [ + '#title' => t('Add ellipsis'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('add_ellipsis'), + '#description' => t('Displays an ellipsis after trimmed text.'), + ]; + $element['wordsafe'] = [ + '#title' => t('Wordsafe'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('wordsafe'), + '#description' => t('Trim on word boundaries.'), + ]; + $field_name = $this->fieldDefinition->getName(); + $field_wordsafe_selector = "fields[$field_name][settings_edit_form][settings][wordsafe]"; + $element['min_wordsafe_length'] = [ + '#title' => t('Minimum acceptable length for truncation'), + '#type' => 'number', + '#field_suffix' => t('characters'), + '#default_value' => $this->getSetting('min_wordsafe_length'), + '#min' => 1, + '#description' => t('The minimum acceptable length for truncation, if wordsafe is TRUE.'), + '#states' => [ + 'invisible' => [ + 'input[name="'. $field_wordsafe_selector . '"]' => ['checked' => FALSE], + ], + ], + ]; return $element; } @@ -73,9 +89,16 @@ */ public function settingsSummary() { $summary = []; - $summary[] = t('Wordsafe: @wordsafe', ['@wordsafe' => $this->getSetting('wordsafe') ? 'true' : 'false']); - $summary[] = t('Add elipsis: @add_elipsis', ['@add_elipsis' => $this->getSetting('add_elipsis') ? 'true' : 'false']); $summary[] = t('Trimmed limit: @trim_length characters', ['@trim_length' => $this->getSetting('trim_length')]); + if ($this->getSetting('wordsafe')) { + $summary[] = t( + 'Truncating on a word boundary, with a minimum of @length characters', + ['@length' => $this->getSetting('min_wordsafe_length')] + ); + } + if ($this->getSetting('add_ellipsis')) { + $summary[] = t('Ellipsis add to the end of the truncated string'); + } return $summary; } @@ -91,12 +114,11 @@ $element += \Drupal::service('element_info')->getInfo($element['#type']); // Add the #pre_render callback that renders the text into a summary. $element['#pre_render'][] = [TextTrimmedFormatter::class, 'preRenderSummary']; - // Pass on the trim length to the #pre_render callback via a property. + // Pass on default settings to the #pre_render callback via a property. $element['#text_summary_trim_length'] = $this->getSetting('trim_length'); - // Pass on the wordsafe configuration: - $element['#wordsafe'] = $this->getSetting('wordsafe'); - // Pass on the add_elipsis configuration: - $element['#add_elipsis'] = $this->getSetting('add_elipsis'); + $element['#text_summary_wordsafe'] = $this->getSetting('wordsafe'); + $element['#text_summary_add_ellipsis'] = $this->getSetting('add_ellipsis'); + $element['#text_summary_min_wordsafe_length'] = $this->getSetting('min_wordsafe_length'); }; // The ProcessedText element already handles cache context & tag bubbling. @@ -132,6 +154,15 @@ * filter_fallback_format(). * - #text_summary_trim_length: the desired character length of the summary * (used by text_summary()) + * - #text_summary_wordsafe: If TRUE, attempt to truncate on a word + * boundary. + * (used by text_summary()) + * - #text_summary_add_ellipsis: If TRUE, add '...' to the end of the + * truncated string. + * (used by text_summary()) + * - #text_summary_min_wordsafe_length: the minimum acceptable length + * for truncation + * (used by text_summary()) * * @return array * The passed-in element with the filtered text in '#markup' trimmed. @@ -140,7 +171,14 @@ * @see text_summary() */ public static function preRenderSummary(array $element) { - $element['#markup'] = text_summary($element['#markup'], $element['#format'], $element['#text_summary_trim_length'], $element['#wordsafe'], $element['#add_elipsis']); + $element['#markup'] = text_summary( + $element['#markup'], + $element['#format'], + $element['#text_summary_trim_length'], + $element['#text_summary_wordsafe'], + $element['#text_summary_add_ellipsis'], + $element['#text_summary_min_wordsafe_length'] + ); return $element; } diff -u b/core/modules/text/text.module b/core/modules/text/text.module --- b/core/modules/text/text.module +++ b/core/modules/text/text.module @@ -56,11 +56,17 @@ * @param $size * The desired character length of the summary. If omitted, the default value * will be used. Ignored if the special delimiter is present in $text. + * @param bool $wordsafe + * If TRUE, attempt to truncate on a word boundary. + * @param bool $add_ellipsis + * If TRUE, add '...' to the end of the truncated string. + * @param int $min_wordsafe_length + * If $wordsafe is TRUE, the minimum acceptable length for truncation. * * @return * The generated summary. */ -function text_summary($text, $format = NULL, $size = NULL, $wordsafe = FALSE, $add_elipsis = FALSE) { +function text_summary($text, $format = NULL, $size = NULL, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) { if (!isset($size)) { $size = \Drupal::config('text.settings')->get('default_summary_length'); @@ -99,7 +105,7 @@ // sentence boundaries. // The summary may not be longer than maximum length specified. Initial slice. - $summary = Unicode::truncate($text, $size, $wordsafe, $add_elipsis); + $summary = Unicode::truncate($text, $size, $wordsafe, $add_ellipsis, $min_wordsafe_length); // Store the actual length of the UTF8 string -- which might not be the same // as $size.