From a0bf9683e366dfee0852731f9e5ec844e0ef9b0b Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 10 Jan 2024 12:56:26 +0000 Subject: [PATCH] Issue #2763431 by dimitriskr, smustgrave, quietone: Replace views_ui_truncate() with Unicode::truncate() --- .../src/Plugin/views/display/RestExport.php | 3 +- .../views/src/Plugin/views/display/Block.php | 5 +-- .../views/display/DisplayPluginBase.php | 5 +-- .../views/src/Plugin/views/display/Page.php | 3 +- .../Plugin/views/display/PathPluginBase.php | 3 +- .../src/Plugin/views/display/DisplayTest.php | 3 +- .../display_extender/DisplayExtenderTest.php | 3 +- core/modules/views_ui/src/ViewFormBase.php | 3 +- .../tests/src/Functional/DisplayTest.php | 3 +- .../tests/src/Functional/ViewEditTest.php | 3 +- .../tests/src/Functional/WizardTest.php | 3 +- .../src/Kernel/TruncateDeprecateTest.php | 32 +++++++++++++++++++ core/modules/views_ui/views_ui.module | 14 ++++---- 13 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 core/modules/views_ui/tests/src/Kernel/TruncateDeprecateTest.php diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php index b3733e4ec87..063f983e547 100644 --- a/core/modules/rest/src/Plugin/views/display/RestExport.php +++ b/core/modules/rest/src/Plugin/views/display/RestExport.php @@ -2,6 +2,7 @@ namespace Drupal\rest\Plugin\views\display; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\CacheableResponse; use Drupal\Core\Form\FormStateInterface; @@ -296,7 +297,7 @@ class RestExport extends PathPluginBase implements ResponseDisplayPluginInterfac $options['auth'] = [ 'category' => 'path', 'title' => $this->t('Authentication'), - 'value' => views_ui_truncate($auth, 24), + 'value' => Unicode::truncate($auth, 24, FALSE, TRUE), ]; // Remove css/exposed form settings, as they are not used for the data diff --git a/core/modules/views/src/Plugin/views/display/Block.php b/core/modules/views/src/Plugin/views/display/Block.php index 00bdf1a10b8..423f2a944c2 100644 --- a/core/modules/views/src/Plugin/views/display/Block.php +++ b/core/modules/views/src/Plugin/views/display/Block.php @@ -2,6 +2,7 @@ namespace Drupal\views\Plugin\views\display; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Url; use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; use Drupal\Core\Block\BlockManagerInterface; @@ -162,12 +163,12 @@ class Block extends DisplayPluginBase { $options['block_description'] = [ 'category' => 'block', 'title' => $this->t('Block name'), - 'value' => views_ui_truncate($block_description, 24), + 'value' => Unicode::truncate($block_description, 24, FALSE, TRUE), ]; $options['block_category'] = [ 'category' => 'block', 'title' => $this->t('Block category'), - 'value' => views_ui_truncate($block_category, 24), + 'value' => Unicode::truncate($block_category, 24, FALSE, TRUE), ]; $filtered_allow = array_filter($this->getOption('allow')); diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index ef24bb6eee6..81c37a2c37d 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -5,6 +5,7 @@ namespace Drupal\views\Plugin\views\display; use Drupal\Component\Plugin\DependentPluginInterface; use Drupal\Component\Utility\Html; use Drupal\Component\Render\FormattableMarkup; +use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; @@ -1119,7 +1120,7 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayPluginInte ]; } - $display_comment = views_ui_truncate($this->getOption('display_comment'), 80); + $display_comment = Unicode::truncate($this->getOption('display_comment'), 80, TRUE, TRUE); $options['display_comment'] = [ 'category' => 'other', 'title' => $this->t('Administrative comment'), @@ -1135,7 +1136,7 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayPluginInte $options['title'] = [ 'category' => 'title', 'title' => $this->t('Title'), - 'value' => views_ui_truncate($title, 32), + 'value' => Unicode::truncate($title, 32, FALSE, TRUE), 'desc' => $this->t('Change the title that this display will use.'), ]; diff --git a/core/modules/views/src/Plugin/views/display/Page.php b/core/modules/views/src/Plugin/views/display/Page.php index 19f129148db..bf0286ffa6c 100644 --- a/core/modules/views/src/Plugin/views/display/Page.php +++ b/core/modules/views/src/Plugin/views/display/Page.php @@ -2,6 +2,7 @@ namespace Drupal\views\Plugin\views\display; +use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Xss; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; @@ -239,7 +240,7 @@ class Page extends PathPluginBase { $options['menu'] = [ 'category' => 'page', 'title' => $this->t('Menu'), - 'value' => views_ui_truncate($menu_str, 24), + 'value' => Unicode::truncate($menu_str, 24, FALSE, TRUE), ]; // This adds a 'Settings' link to the style_options setting if the style diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php index 438c2095412..984d740e57b 100644 --- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php @@ -2,6 +2,7 @@ namespace Drupal\views\Plugin\views\display; +use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; @@ -426,7 +427,7 @@ abstract class PathPluginBase extends DisplayPluginBase implements DisplayRouter $options['path'] = [ 'category' => 'page', 'title' => $this->t('Path'), - 'value' => views_ui_truncate($path, 24), + 'value' => Unicode::truncate($path, 24, FALSE, TRUE), ]; } diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php index 0ff2ba81f1a..3c4b41d5cd1 100644 --- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php +++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display/DisplayTest.php @@ -2,6 +2,7 @@ namespace Drupal\views_test_data\Plugin\views\display; +use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Xss; use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -63,7 +64,7 @@ class DisplayTest extends DisplayPluginBase { $options['test_option'] = [ 'category' => 'display_test', 'title' => $this->t('Test option'), - 'value' => views_ui_truncate($test_option, 24), + 'value' => Unicode::truncate($test_option, 24, FALSE, TRUE), ]; } diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display_extender/DisplayExtenderTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display_extender/DisplayExtenderTest.php index 25594c3584a..ff08fa4e933 100644 --- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display_extender/DisplayExtenderTest.php +++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/display_extender/DisplayExtenderTest.php @@ -2,6 +2,7 @@ namespace Drupal\views_test_data\Plugin\views\display_extender; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\display_extender\DisplayExtenderPluginBase; @@ -50,7 +51,7 @@ class DisplayExtenderTest extends DisplayExtenderPluginBase { $options['test_extender_test_option'] = [ 'category' => 'display_extender_test', 'title' => $this->t('Test option'), - 'value' => views_ui_truncate($this->options['test_extender_test_option'], 24), + 'value' => Unicode::truncate($this->options['test_extender_test_option'], 24, FALSE, TRUE), ]; } diff --git a/core/modules/views_ui/src/ViewFormBase.php b/core/modules/views_ui/src/ViewFormBase.php index c04628bfdab..67a71bdd552 100644 --- a/core/modules/views_ui/src/ViewFormBase.php +++ b/core/modules/views_ui/src/ViewFormBase.php @@ -2,6 +2,7 @@ namespace Drupal\views_ui; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -160,7 +161,7 @@ abstract class ViewFormBase extends EntityForm { public function getDisplayLabel(ViewUI $view, $display_id, $check_changed = TRUE) { $display = $view->get('display'); $title = $display_id == 'default' ? $this->t('Default') : $display[$display_id]['display_title']; - $title = views_ui_truncate($title, 25); + $title = Unicode::truncate($title, 25, FALSE, TRUE); if ($check_changed && !empty($view->changed_display[$display_id])) { $changed = '*'; diff --git a/core/modules/views_ui/tests/src/Functional/DisplayTest.php b/core/modules/views_ui/tests/src/Functional/DisplayTest.php index ae46f6abf2c..8d10c8c8f87 100644 --- a/core/modules/views_ui/tests/src/Functional/DisplayTest.php +++ b/core/modules/views_ui/tests/src/Functional/DisplayTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\views_ui\Functional; +use Drupal\Component\Utility\Unicode; use Drupal\views\Entity\View; use Drupal\views\Views; @@ -227,7 +228,7 @@ class DisplayTest extends UITestBase { $view->save(); $this->drupalGet("admin/structure/views/view/{$view->id()}"); - $escaped = views_ui_truncate($input, 25); + $escaped = Unicode::truncate($input, 25, FALSE, TRUE); $this->assertSession()->assertEscaped($escaped); $this->assertSession()->responseNotContains($xss_markup); diff --git a/core/modules/views_ui/tests/src/Functional/ViewEditTest.php b/core/modules/views_ui/tests/src/Functional/ViewEditTest.php index 722a2a332c6..6d957f6f14e 100644 --- a/core/modules/views_ui/tests/src/Functional/ViewEditTest.php +++ b/core/modules/views_ui/tests/src/Functional/ViewEditTest.php @@ -50,12 +50,11 @@ class ViewEditTest extends UITestBase { $this->drupalGet('admin/structure/views/view/test_view'); // Add a new attachment display. $this->submitForm([], 'Add Attachment'); - // Test that a long administrative comment is truncated. $edit = ['display_comment' => 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen']; $this->drupalGet('admin/structure/views/nojs/display/test_view/attachment_1/display_comment'); $this->submitForm($edit, 'Apply'); - $this->assertSession()->pageTextContains('one two three four five six seven eight nine ten eleven twelve thirteen fourteen...'); + $this->assertSession()->pageTextContains('one two three four five six seven eight nine ten eleven twelve thirteen…'); // Change the machine name for the display from page_1 to test_1. $edit = ['display_id' => 'test_1']; diff --git a/core/modules/views_ui/tests/src/Functional/WizardTest.php b/core/modules/views_ui/tests/src/Functional/WizardTest.php index 81718fd21c4..95378286c47 100644 --- a/core/modules/views_ui/tests/src/Functional/WizardTest.php +++ b/core/modules/views_ui/tests/src/Functional/WizardTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\views_ui\Functional; +use Drupal\Component\Utility\Unicode; use Drupal\Tests\views\Functional\Wizard\WizardTestBase; /** @@ -65,7 +66,7 @@ class WizardTest extends WizardTestBase { $this->submitForm($view, 'Save and edit'); $this->assertSession()->addressEquals('admin/structure/views/view/' . $view['id']); // Assert that the page title is correctly truncated. - $this->assertSession()->pageTextContains(views_ui_truncate($view['page[title]'], 32)); + $this->assertSession()->pageTextContains(Unicode::truncate($view['page[title]'], 32, FALSE, TRUE)); } } diff --git a/core/modules/views_ui/tests/src/Kernel/TruncateDeprecateTest.php b/core/modules/views_ui/tests/src/Kernel/TruncateDeprecateTest.php new file mode 100644 index 00000000000..754445ad5b9 --- /dev/null +++ b/core/modules/views_ui/tests/src/Kernel/TruncateDeprecateTest.php @@ -0,0 +1,32 @@ +expectDeprecation('views_ui_truncate() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \Drupal\Component\Utility\Unicode::truncate(). See https://www.drupal.org/node/3408283'); + } + +} diff --git a/core/modules/views_ui/views_ui.module b/core/modules/views_ui/views_ui.module index 33dbb6b3c65..0598b2d142e 100644 --- a/core/modules/views_ui/views_ui.module +++ b/core/modules/views_ui/views_ui.module @@ -5,6 +5,7 @@ * Provide structure for the administrative interface to Views. */ +use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Xss; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; @@ -339,12 +340,13 @@ function views_ui_views_analyze(ViewExecutable $view) { * Truncate strings to a set length and provide a '...' if they truncated. * * This is often used in the UI to ensure long strings fit. + * + * @deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. + * Use \Drupal\Component\Utility\Unicode::truncate(). + * + * @see https://www.drupal.org/node/3408283 */ function views_ui_truncate($string, $length) { - if (mb_strlen($string) > $length) { - $string = mb_substr($string, 0, $length); - $string .= '...'; - } - - return $string; + @trigger_error('views_ui_truncate() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \Drupal\Component\Utility\Unicode::truncate(). See https://www.drupal.org/node/3408283', E_USER_DEPRECATED); + return Unicode::truncate($string, $length, FALSE, TRUE); }