Skip to content

Commit

Permalink
Replace truncate filter with u.truncate (#6132)
Browse files Browse the repository at this point in the history
* Replace truncate filter with u.truncate

* Deprecate UnicodeString in favor of the Symfony one

* Deprecate sonata_truncate filter
  • Loading branch information
franmomu authored Jun 14, 2020
1 parent 2fafa32 commit bde0095
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 33 deletions.
8 changes: 8 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
UPGRADE 3.x
===========

## Deprecated `sonata_truncate` Twig filter

This filter has been deprecated in favor of the [`u` filter](https://twig.symfony.com/doc/2.x/filters/u.html):

## Deprecated `SonataAdminBundle\Twig\Extension\UnicodeString`

Use `Symfony\Component\String\UnicodeString` instead.

UPGRADE FROM 3.67 to 3.68
=========================

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
"symfony/security-bundle": "^4.3",
"symfony/security-core": "^4.3",
"symfony/security-csrf": "^4.3",
"symfony/string": "^5.1",
"symfony/translation": "^4.3",
"symfony/twig-bridge": "^4.3",
"symfony/twig-bundle": "^4.3",
"symfony/validator": "^4.3",
"twig/extensions": "^1.5",
"twig/extra-bundle": "^3.0",
"twig/string-extra": "^3.0",
"twig/twig": "^2.12.1"
Expand Down
8 changes: 4 additions & 4 deletions docs/cookbook/recipe_customizing_a_mosaic_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ It is possible to configure the default view by creating a dedicated template.
please use the following configuration:

.. code-block:: yaml
# config/packages/sonata_admin.yaml
sonata_admin:
Expand Down Expand Up @@ -89,11 +89,11 @@ The ``list_outer_rows_mosaic.html.twig`` is the name of one mosaic's tile. You s
{% block sonata_mosaic_description %}
{% if admin.hasAccess('edit', object) and admin.hasRoute('edit') %}
<a href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
<a href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|u.truncate(40) }}</a>
{% elseif admin.hasAccess('show', object) and admin.hasRoute('show') %}
<a href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
<a href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|u.truncate(40) }}</a>
{% else %}
{{ meta.title|truncate(40) }}
{{ meta.title|u.truncate(40) }}
{% endif %}
{% endblock %}
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/views/CRUD/base_edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ file that was distributed with this source code.
{% block title %}
{# NEXT_MAJOR: remove default filter #}
{% if objectId|default(admin.id(object)) is not null %}
{{ 'title_edit'|trans({'%name%': admin.toString(object)|truncate(15) }, 'SonataAdminBundle') }}
{{ 'title_edit'|trans({'%name%': admin.toString(object)|u.truncate(15) }, 'SonataAdminBundle') }}
{% else %}
{{ 'title_create'|trans({}, 'SonataAdminBundle')|truncate(15) }}
{{ 'title_create'|trans({}, 'SonataAdminBundle')|u.truncate(15) }}
{% endif %}
{% endblock %}

{% block navbar_title %}
{# NEXT_MAJOR: remove default filter #}
{% if objectId|default(admin.id(object)) is not null %}
{{ 'title_edit'|trans({'%name%': admin.toString(object)|truncate(100) }, 'SonataAdminBundle') }}
{{ 'title_edit'|trans({'%name%': admin.toString(object)|u.truncate(100) }, 'SonataAdminBundle') }}
{% else %}
{{ 'title_create'|trans({}, 'SonataAdminBundle')|truncate(100) }}
{{ 'title_create'|trans({}, 'SonataAdminBundle')|u.truncate(100) }}
{% endif %}
{% endblock %}

Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ file that was distributed with this source code.
{%- endblock -%}

{% block title %}
{{ admin.isChild and admin.parent.subject ? 'title_edit'|trans({'%name%': admin.parent.toString(admin.parent.subject)|truncate(15) }, 'SonataAdminBundle') : '' }}
{{ admin.isChild and admin.parent.subject ? 'title_edit'|trans({'%name%': admin.parent.toString(admin.parent.subject)|u.truncate(15) }, 'SonataAdminBundle') : '' }}
{% endblock %}

{% block navbar_title %}
{{ admin.isChild and admin.parent.subject ? 'title_edit'|trans({'%name%': admin.parent.toString(admin.parent.subject)|truncate(100) }, 'SonataAdminBundle') : '' }}
{{ admin.isChild and admin.parent.subject ? 'title_edit'|trans({'%name%': admin.parent.toString(admin.parent.subject)|u.truncate(100) }, 'SonataAdminBundle') : '' }}
{% endblock %}

{% block list_table %}
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/CRUD/base_show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ file that was distributed with this source code.
{% extends base_template %}

{% block title %}
{{ 'title_show'|trans({'%name%': admin.toString(object)|truncate(15) }, 'SonataAdminBundle') }}
{{ 'title_show'|trans({'%name%': admin.toString(object)|u.truncate(15) }, 'SonataAdminBundle') }}
{% endblock %}

{% block navbar_title %}
{{ 'title_show'|trans({'%name%': admin.toString(object)|truncate(100) }, 'SonataAdminBundle') }}
{{ 'title_show'|trans({'%name%': admin.toString(object)|u.truncate(100) }, 'SonataAdminBundle') }}
{% endblock %}

{%- block actions -%}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/list_html.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% deprecated 'The "truncate.separator" option is deprecated since sonata-project/admin-bundle 3.65, to be removed in 4.0. Use "truncate.ellipsis" instead.' %}
{% endif %}
{% set ellipsis = truncate.ellipsis is defined ? truncate.ellipsis : (truncate.separator is defined ? truncate.separator : '...') %}
{{ value|striptags|sonata_truncate(length, cut == false, ellipsis)|raw }}
{{ value|striptags|u.truncate(length, ellipsis, cut)|raw }}
{%- else -%}
{%- if field_description.options.strip is defined -%}
{% set value = value|striptags %}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/list_outer_rows_mosaic.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This template can be customized to match your needs. You should only extends the
{% endif %}

{% block sonata_mosaic_description %}
{{ meta.title|truncate(40) }}
{{ meta.title|u.truncate(40) }}
{% endblock %}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/show_html.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% deprecated 'The "truncate.separator" option is deprecated since sonata-project/admin-bundle 3.65, to be removed in 4.0. Use "truncate.ellipsis" instead.' %}
{% endif %}
{% set ellipsis = truncate.ellipsis is defined ? truncate.ellipsis : (truncate.separator is defined ? truncate.separator : '...') %}
{{ value|striptags|sonata_truncate(length, cut == false, ellipsis)|raw }}
{{ value|striptags|u.truncate(length, ellipsis, cut)|raw }}
{%- else -%}
{%- if field_description.options.strip is defined -%}
{% set value = value|striptags %}
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/views/standard_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ file that was distributed with this source code.
{% if menu.extra('safe_label', true) %}
{{- label|raw -}}
{% else %}
{{- label|truncate(100) -}}
{{- label|u.truncate(100) -}}
{% endif %}
</a>
{% else %}
<span>{{ label|truncate(100) }}</span>
<span>{{ label|u.truncate(100) }}</span>
{% endif %}
</li>
{% else %}
<li class="active"><span>{{ label|truncate(100) }}</span></li>
<li class="active"><span>{{ label|u.truncate(100) }}</span></li>
{% endif %}
{% endfor %}
{% endif %}
Expand Down
33 changes: 24 additions & 9 deletions src/Twig/Extension/StringExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

namespace Sonata\AdminBundle\Twig\Extension;

use Symfony\Component\String\UnicodeString as DecoratedUnicodeString;
use Symfony\Component\String\UnicodeString as SymfonyUnicodeString;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Extensions\TextExtension;
use Twig\TwigFilter;

/**
* NEXT_MAJOR: Remove this class.
*
* Decorates `Twig\Extra\String\StringExtension` in order to provide the `$cut`
* argument for `Symfony\Component\String\UnicodeString::truncate()`.
* This class must be removed when the component ships this feature.
Expand Down Expand Up @@ -46,22 +49,34 @@ public function __construct(?TextExtension $legacyExtension = null)
*/
public function getFilters(): array
{
return [
new TwigFilter('sonata_truncate', [$this, 'deprecatedTruncate'], ['needs_environment' => true]),
];
}

/**
* @return SymfonyUnicodeString|string
*/
public function deprecatedTruncate(Environment $env, ?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...')
{
@trigger_error(
'The "sonata_truncate" twig filter is deprecated'
.' since sonata-project/admin-bundle 3.x and will be removed in 4.0. Use "u.truncate" instead.',
E_USER_DEPRECATED
);

if (null !== $this->legacyExtension) {
return [
new TwigFilter('sonata_truncate', 'twig_truncate_filter', ['needs_environment' => true]),
];
return twig_truncate_filter($env, $text, $length, $preserve, $ellipsis);
}

return [
new TwigFilter('sonata_truncate', [$this, 'legacyTruncteWithUnicodeString']),
];
return $this->legacyTruncteWithUnicodeString($text, $length, $preserve, $ellipsis);
}

/**
* NEXT_MAJOR: Fix the arguments in order to respect the signature at `UnicodeString::truncate()`.
*/
public function legacyTruncteWithUnicodeString(?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...'): DecoratedUnicodeString
public function legacyTruncteWithUnicodeString(?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...'): SymfonyUnicodeString
{
return (new UnicodeString($text ?? ''))->truncate($length, $ellipsis, $preserve);
return (new SymfonyUnicodeString($text ?? ''))->truncate($length, $ellipsis, $preserve);
}
}
3 changes: 3 additions & 0 deletions src/Twig/Extension/UnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*
* @see https://github.com/symfony/symfony/pull/35649
*
* NEXT_MAJOR: Remove this class
* @deprecated since sonata-project/admin-bundle 3.x, use Symfony\Component\String\UnicodeString instead.
*
* @throws ExceptionInterface
*
* @author Javier Spagnoletti <[email protected]>
Expand Down
8 changes: 3 additions & 5 deletions tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
use Sonata\AdminBundle\Tests\Fixtures\StubFilesystemLoader;
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
use Sonata\AdminBundle\Twig\Extension\StringExtension;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
Expand All @@ -42,7 +41,7 @@
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Extensions\TextExtension;
use Twig\Extra\String\StringExtension;

/**
* Test for SonataAdminExtension.
Expand Down Expand Up @@ -204,7 +203,6 @@ protected function setUp(): void
$requestContext = new RequestContext();
$urlGenerator = new UrlGenerator($routeCollection, $requestContext);
$this->environment->addExtension(new RoutingExtension($urlGenerator));
$this->environment->addExtension(new TextExtension());
$this->environment->addExtension(new StringExtension());

// initialize object
Expand Down Expand Up @@ -2203,7 +2201,7 @@ public function testDeprecatedTextExtension(string $expected, string $type, $val
]);
$environment->addExtension($this->twigExtension);
$environment->addExtension(new TranslationExtension($this->translator));
$environment->addExtension(new StringExtension(new TextExtension()));
$environment->addExtension(new StringExtension());

$this->admin
->method('getTemplate')
Expand Down Expand Up @@ -2250,7 +2248,7 @@ public function getDeprecatedTextExtensionItems(): iterable
];

yield 'custom_length' => [
'<th>Data</th> <td> Creating a Template for[...] </td>',
'<th>Data</th> <td> Creating a Template[...] </td>',
'html',
'<p><strong>Creating a Template for the Field</strong> and form</p>',
[
Expand Down

0 comments on commit bde0095

Please sign in to comment.