public static function FieldPluginBase::trimText

Trims the field down to the specified length.

Parameters

array $alter: The alter array of options to use.

  • max_length: Maximum lenght of the string, the rest gets truncated.
  • word_boundary: Trim only on a word boundary.
  • ellipsis: Show an ellipsis (...) at the end of the trimmed string.
  • html: Take sure that the html is correct.

string $value: The string which should be trimmed.

Return value

string The trimmed string.

2 calls to FieldPluginBase::trimText()
FieldPluginBase::renderTrimText in drupal/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php
Trim the field down to the specified length.
FieldUnitTest::testTrimText in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php
Tests the trimText method.

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php, line 1644
Definition of Drupal\views\Plugin\views\field\FieldPluginBase.

Class

FieldPluginBase
Base field handler that has no options and renders an unformatted field.

Namespace

Drupal\views\Plugin\views\field

Code

public static function trimText($alter, $value) {
  if (drupal_strlen($value) > $alter['max_length']) {
    $value = drupal_substr($value, 0, $alter['max_length']);
    if (!empty($alter['word_boundary'])) {
      $regex = "(.*)\\b.+";
      if (function_exists('mb_ereg')) {
        mb_regex_encoding('UTF-8');
        $found = mb_ereg($regex, $value, $matches);
      }
      else {
        $found = preg_match("/{$regex}/us", $value, $matches);
      }
      if ($found) {
        $value = $matches[1];
      }
    }

    // Remove scraps of HTML entities from the end of a strings
    $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));
    if (!empty($alter['ellipsis'])) {

      // @todo: What about changing this to a real ellipsis?
      $value .= t('...');
    }
  }
  if (!empty($alter['html'])) {
    $value = _filter_htmlcorrector($value);
  }
  return $value;
}