Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use translation domain of admin as default for form_help #7021

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Form/Extension/Field/Type/FormTypeFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['sonata_admin_enabled'] = true;
$view->vars['sonata_admin'] = $sonataAdmin;
$view->vars['sonata_admin_code'] = $sonataAdmin['admin']->getCode();
$view->vars['sonata_admin_translation_domain'] = $sonataAdmin['admin']->getTranslationDomain();

$attr = $view->vars['attr'];

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ file that was distributed with this source code.
{% endblock %}

{% block form_help -%}
{% if sonata_admin_translation_domain is defined and translation_domain is null %}
{% set translation_domain = sonata_admin_translation_domain %}
{% endif %}
{% if help is not empty %}
{# NEXT_MAJOR: Remove class sonata-ba-field-widget-help #}
{% set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-block sonata-ba-field-widget-help sonata-ba-field-help')}) %}
Expand Down
60 changes: 46 additions & 14 deletions tests/Form/AdminLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Form;

use PHPUnit\Framework\MockObject\Stub;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand Down Expand Up @@ -81,20 +82,7 @@ public function testLabelWithCustomTranslationDomain(): void

public function testLabelWithAdminTranslationDomain(): void
{
$fieldDescription = $this->createStub(FieldDescriptionInterface::class);

$admin = $this->createStub(AdminInterface::class);
$admin
->method('getCode')
->willReturn('sonata_code');

$admin
->method('getTranslationDomain')
->willReturn('sonata_translation_domain');

$fieldDescription
->method('getAdmin')
->willReturn($admin);
$fieldDescription = $this->createFieldDescriptionWithTranslationDomain('sonata_translation_domain');

$form = $this->factory->createNamed('name', TextType::class, null, [
'sonata_field_description' => $fieldDescription,
Expand Down Expand Up @@ -129,6 +117,27 @@ public function testHelp(): void
$this->assertMatchesXpath($html, $expression);
}

public function testHelpWithAdminTranslationDomain(): void
{
$fieldDescription = $this->createFieldDescriptionWithTranslationDomain('sonata_translation_domain');

$form = $this->factory->createNamed('name', TextType::class, null, [
'help' => 'Help text test!',
'sonata_field_description' => $fieldDescription,
]);
$view = $form->createView();
$html = $this->renderHelp($view);

$expression = <<<'EOD'
/p
[@id="name_help"]
[@class="help-block sonata-ba-field-widget-help sonata-ba-field-help help-text"]
[.="[trans domain=sonata_translation_domain]Help text test![/trans]"]
EOD;

$this->assertMatchesXpath($html, $expression);
}

public function testRowSetId(): void
{
$form = $this->factory->createNamed('name', TextType::class);
Expand Down Expand Up @@ -202,4 +211,27 @@ public function testRowAttr(): void
'//div[@class="foo form-group"][@data-value="bar"][@id="sonata-ba-field-container-name"]'
);
}

/**
* @return Stub&FieldDescriptionInterface
*/
private function createFieldDescriptionWithTranslationDomain(string $translationDomain): Stub
{
$fieldDescription = $this->createStub(FieldDescriptionInterface::class);

$admin = $this->createStub(AdminInterface::class);
$admin
->method('getCode')
->willReturn('sonata_code');

$admin
->method('getTranslationDomain')
->willReturn($translationDomain);

$fieldDescription
->method('getAdmin')
->willReturn($admin);

return $fieldDescription;
}
}