Issue #2763431 by dimitriskr, smustgrave, quietone: Replace views_ui_truncate() with Unicode::truncate()

merge-requests/6106/head
catch 2024-01-10 12:56:26 +00:00
parent 541baec3de
commit a0bf9683e3
13 changed files with 63 additions and 20 deletions

View File

@ -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

View File

@ -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'));

View File

@ -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.'),
];

View File

@ -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

View File

@ -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),
];
}

View File

@ -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),
];
}

View File

@ -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),
];
}

View File

@ -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 = '*';

View File

@ -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);

View File

@ -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'];

View File

@ -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));
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Drupal\Tests\views_ui\Kernel;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
/**
* Tests the deprecation of views_ui_truncate() function.
*
* @group views_ui
*/
class TruncateDeprecateTest extends ViewsKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['views', 'views_ui'];
/**
* Tests the deprecation of views_ui_truncate() replaced by Unicode::truncate.
*
* @group legacy
*/
public function testDeprecateViewsUiTruncate() {
$string = 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen';
$short_string = views_ui_truncate($string, 80);
$this->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');
}
}

View File

@ -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);
}