Skip to content

Commit

Permalink
Use translation domain of admin as default for form_help
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Apr 16, 2021
1 parent 56aeb58 commit f41c2b1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
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;
}
}

0 comments on commit f41c2b1

Please sign in to comment.