Skip to content

Commit

Permalink
Add support for form-extensions 2.0 (#8064)
Browse files Browse the repository at this point in the history
* Add support for form-extensions 2.0

* Actualizar test.yaml
  • Loading branch information
jordisala1991 authored May 13, 2023
1 parent 001daf2 commit fc1ce38
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- php-version: '8.2'
dependencies: highest
allowed-to-fail: false
variant: sonata-project/form-extensions:"2.x-dev as 1.999"
variant: sonata-project/form-extensions:"2.0.0-alpha-1"
- php-version: '8.2'
dependencies: highest
allowed-to-fail: false
Expand Down
1 change: 1 addition & 0 deletions assets/scss/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

html {
font-size: initial !important;
min-height: 100%;
position: relative;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"sonata-project/block-bundle": "^4.11",
"sonata-project/doctrine-extensions": "^1.8 || ^2.0",
"sonata-project/exporter": "^2.14 || ^3.1.1",
"sonata-project/form-extensions": "^1.15",
"sonata-project/form-extensions": "^1.15 || ^2.0",
"sonata-project/twig-extensions": "^1.4.1 || ^2.0",
"symfony/asset": "^5.4 || ^6.2",
"symfony/config": "^5.4 || ^6.2",
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@
->tag('twig.runtime')
->args([
service('request_stack'),
// TODO: Remove this argument when dropping support for `sonata-project/form-extensions` 1.x.
service('sonata.form.twig.canonicalize_runtime')->nullOnInvalid(),
])

// NEXT_MAJOR: Remove the `args()` call.
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/app.css

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Resources/views/standard_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ file that was distributed with this source code.
{% endfor %}
{% endblock %}

{# TODO: Drop locale for moment calls when dropping support for `sonata-project/form-extensions` 1.x #}
{# localize moment #}
{% set localeForMoment = sonata_form_canonicalize_locale_for_moment() %}
{% set localeForMoment = canonicalize_locale_for_moment() %}
{% if localeForMoment %}
<script src="{{ asset('bundles/sonataform/moment-locale/' ~ localeForMoment ~ '.js') }}"></script>
{% endif %}
Expand Down
35 changes: 7 additions & 28 deletions src/Twig/CanonicalizeRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,36 @@

namespace Sonata\AdminBundle\Twig;

use Sonata\Form\Twig\CanonicalizeRuntime as SonataFormCanonicalizeRuntime;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Extension\RuntimeExtensionInterface;

final class CanonicalizeRuntime implements RuntimeExtensionInterface
{
// @todo: there are more locales which are not supported by "Moment.js" NPM library and they need to be translated/normalized/canonicalized here
private const MOMENT_UNSUPPORTED_LOCALES = [
'de' => ['de', 'de-at'],
'es' => ['es', 'es-do'],
'nl' => ['nl', 'nl-be'],
'fr' => ['fr', 'fr-ca', 'fr-ch'],
];

/**
* TODO: Remove second argument when dropping support for `sonata-project/form-extensions` 1.x.
*
* @internal This class should only be used through Twig
*/
public function __construct(
private RequestStack $requestStack
private RequestStack $requestStack,
private ?SonataFormCanonicalizeRuntime $canonicalizeRuntime
) {
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle version 4.12 use `sonata_form_canonicalize_locale_for_moment` twig function.
*
* Returns a canonicalized locale for "Moment.js" NPM library,
* or `null` if the locale's language is "en", which doesn't require localization.
*/
public function getCanonicalizedLocaleForMoment(): ?string
{
@trigger_error(
'The `canonicalize_locale_for_moment` twig function is deprecated since sonata-project/admin-bundle 4.12 and will be removed in 5.0.'
.' use `sonata_form_canonicalize_locale_for_moment` instead.',
\E_USER_DEPRECATED
);

$locale = $this->getLocale();

// "en" language doesn't require localization.
if (('en' === $lang = substr($locale, 0, 2)) && !\in_array($locale, ['en-au', 'en-ca', 'en-gb', 'en-ie', 'en-nz'], true)) {
if (null === $this->canonicalizeRuntime) {
return null;
}

foreach (self::MOMENT_UNSUPPORTED_LOCALES as $language => $locales) {
if ($language === $lang && !\in_array($locale, $locales, true)) {
$locale = $language;
}
}

return $locale;
return $this->canonicalizeRuntime->getCanonicalizedLocaleForMoment();
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/Twig/CanonicalizeRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Twig\CanonicalizeRuntime;
use Sonata\Form\Twig\CanonicalizeRuntime as SonataFormCanonicalizeRuntime;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

Expand All @@ -29,7 +30,10 @@ protected function setUp(): void
$this->request = new Request();
$requestStack = new RequestStack();
$requestStack->push($this->request);
$this->canonicalizeRuntime = new CanonicalizeRuntime($requestStack);
$this->canonicalizeRuntime = new CanonicalizeRuntime(
$requestStack,
class_exists(SonataFormCanonicalizeRuntime::class) ? new SonataFormCanonicalizeRuntime($requestStack) : null,
);
}

/**
Expand All @@ -42,6 +46,9 @@ protected function setUp(): void
public function testCanonicalizedLocaleForMoment(?string $expected, string $original): void
{
$this->changeLocale($original);

$expected = class_exists(SonataFormCanonicalizeRuntime::class) ? $expected : null;

static::assertSame($expected, $this->canonicalizeRuntime->getCanonicalizedLocaleForMoment());
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Twig/Extension/CanonicalizeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Twig\CanonicalizeRuntime;
use Sonata\AdminBundle\Twig\Extension\CanonicalizeExtension;
use Sonata\Form\Twig\CanonicalizeRuntime as SonataFormCanonicalizeRuntime;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

Expand All @@ -35,7 +36,12 @@ protected function setUp(): void
$this->request = new Request();
$requestStack = new RequestStack();
$requestStack->push($this->request);
$this->twigExtension = new CanonicalizeExtension(new CanonicalizeRuntime($requestStack));
$this->twigExtension = new CanonicalizeExtension(
new CanonicalizeRuntime(
$requestStack,
class_exists(SonataFormCanonicalizeRuntime::class) ? new SonataFormCanonicalizeRuntime($requestStack) : null,
)
);
}

/**
Expand All @@ -44,6 +50,9 @@ protected function setUp(): void
public function testCanonicalizedLocaleForMoment(?string $expected, string $original): void
{
$this->changeLocale($original);

$expected = class_exists(SonataFormCanonicalizeRuntime::class) ? $expected : null;

static::assertSame($expected, $this->twigExtension->getCanonicalizedLocaleForMoment());
}

Expand Down

0 comments on commit fc1ce38

Please sign in to comment.