diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php index 5b684b33931f5c30838d70532da32f88561e84ff..46bcae1515a789d7fa9a19cfa5fe49e909e8e7f9 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php @@ -1306,7 +1306,7 @@ function render_trim_text($alter, $value) { // element type. $this->definition['element type'] = 'span'; } - return views_trim_text($alter, $value); + return static::trimText($alter, $value); } /** @@ -1634,6 +1634,53 @@ public function adminLabel($short = FALSE) { return $this->getField(parent::adminLabel($short)); } + /** + * Trims the field down to the specified length. + * + * @param 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. + * + * @param string $value + * The string which should be trimmed. + * + * @return string + * The trimmed string. + */ + 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; + } + } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php index 5b21a6736fe3932b003f88c1fb9af43df6094d11..450fee802daa80cd282326e1e9e0ffe8d2fa4671 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldUnitTest.php @@ -8,6 +8,7 @@ namespace Drupal\views\Tests\Handler; use Drupal\views\Tests\ViewUnitTestBase; +use Drupal\views\Plugin\views\field\FieldPluginBase; /** * Tests the generic field handler. @@ -533,4 +534,58 @@ public function testClickSortable() { $this->assertFalse($plugin->click_sortable(), 'FALSE as a views data value is correct.'); } + /** + * Tests the trimText method. + */ + public function testTrimText() { + // Test unicode, @see http://drupal.org/node/513396#comment-2839416 + $text = array( + 'Tuy nhiên, những hi vá»ng', + 'Giả sá» chúng tôi có 3 Apple', + 'siêu nhá» nà y là bá»™ xá» lý', + 'Di Ä‘á»™ng của nhà sản xuất Phần Lan', + 'khoảng cách từ đại là đến', + 'của hãng bao gồm ba dòng', + 'Ñд аÑд аÑд аÑ', + 'аÑд аÑд аÑд аÑ' + ); + // Just test maxlength without word boundry. + $alter = array( + 'max_length' => 10, + ); + $expect = array( + 'Tuy nhiên,', + 'Giả sá» chú', + 'siêu nhá» n', + 'Di Ä‘á»™ng củ', + 'khoảng các', + 'của hãng b', + 'Ñд аÑд аÑд', + 'аÑд аÑд аÑ', + ); + + foreach ($text as $key => $line) { + $result_text = FieldPluginBase::trimText($alter, $line); + $this->assertEqual($result_text, $expect[$key]); + } + + // Test also word_boundary + $alter['word_boundary'] = TRUE; + $expect = array( + 'Tuy nhiên', + 'Giả sá»', + 'siêu nhá»', + 'Di Ä‘á»™ng', + 'khoảng', + 'của hãng', + 'Ñд аÑд', + 'аÑд аÑд', + ); + + foreach ($text as $key => $line) { + $result_text = FieldPluginBase::trimText($alter, $line); + $this->assertEqual($result_text, $expect[$key]); + } + } + } diff --git a/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php index 85708c046fd1c684f4c8a2a7abf66adaee836393..dacdbabe28856bb185f85d5325fab664e1eaff07 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.php @@ -27,57 +27,6 @@ public static function getInfo() { ); } - public function test_views_trim_text() { - // Test unicode, @see http://drupal.org/node/513396#comment-2839416 - $text = array( - 'Tuy nhiên, những hi vá»ng', - 'Giả sá» chúng tôi có 3 Apple', - 'siêu nhá» nà y là bá»™ xá» lý', - 'Di Ä‘á»™ng của nhà sản xuất Phần Lan', - 'khoảng cách từ đại là đến', - 'của hãng bao gồm ba dòng', - 'Ñд аÑд аÑд аÑ', - 'аÑд аÑд аÑд аÑ' - ); - // Just test maxlength without word boundry. - $alter = array( - 'max_length' => 10, - ); - $expect = array( - 'Tuy nhiên,', - 'Giả sá» chú', - 'siêu nhá» n', - 'Di Ä‘á»™ng củ', - 'khoảng các', - 'của hãng b', - 'Ñд аÑд аÑд', - 'аÑд аÑд аÑ', - ); - - foreach ($text as $key => $line) { - $result_text = views_trim_text($alter, $line); - $this->assertEqual($result_text, $expect[$key]); - } - - // Test also word_boundary - $alter['word_boundary'] = TRUE; - $expect = array( - 'Tuy nhiên', - 'Giả sá»', - 'siêu nhá»', - 'Di Ä‘á»™ng', - 'khoảng', - 'của hãng', - 'Ñд аÑд', - 'аÑд аÑд', - ); - - foreach ($text as $key => $line) { - $result_text = views_trim_text($alter, $line); - $this->assertEqual($result_text, $expect[$key]); - } - } - /** * Tests the views_get_handler method. */ diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 1b8daa88c6b4381a3771622f4ddb513499366530..8dc5f5a8f79ef4339dabfc29e0c097b2badadf5c 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -1771,49 +1771,6 @@ function _field_view_formatter_options($field_type = NULL) { return $options; } -/** - * Trim the field down to the specified length. - * - * @param $alter - * - 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. - * - * @param $value - * The string which should be trimmed. - */ -function views_trim_text($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; -} - /** * Filter by no empty values, though allow to use "0". * @param $var