public static function FieldPluginBase::trimText in Drupal 9
Same name and namespace in other branches
- 8 core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::trimText()
Trims the field down to the specified length.
Parameters
array $alter: The alter array of options to use.
- max_length: Maximum length 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: Make 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()
- FieldKernelTest::testTrimText in core/
modules/ views/ tests/ src/ Kernel/ Handler/ FieldKernelTest.php - Tests the trimText method.
- FieldPluginBase::renderTrimText in core/
modules/ views/ src/ Plugin/ views/ field/ FieldPluginBase.php - Trims the field down to the specified length.
File
- core/
modules/ views/ src/ Plugin/ views/ field/ FieldPluginBase.php, line 1800
Class
- FieldPluginBase
- Base class for views fields.
Namespace
Drupal\views\Plugin\views\fieldCode
public static function trimText($alter, $value) {
if (mb_strlen($value) > $alter['max_length']) {
$value = mb_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'])) {
$value .= t('…');
}
}
if (!empty($alter['html'])) {
$value = Html::normalize($value);
}
return $value;
}