Skip to content

Commit

Permalink
Fix Field label configuration
Browse files Browse the repository at this point in the history
The 'field' option wasn't defined so it was never possible to even use
a label for localized input (in StringQuery).

Secondly the labels can now be translated using a TranslatableInterface
value (for the Symfony bundle).
  • Loading branch information
sstok committed Sep 17, 2024
1 parent d2e9170 commit 9e17420
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 13 deletions.
21 changes: 21 additions & 0 deletions docs/integration/symfony_bundle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ Search Query
That's it. You can now process search requests! See the reference section
below to learn more about application wide cache configuring.

Translating labels (StringQuery only)
-------------------------------------

The :class:`Rollerworks\\Component\\Search\\Input\\StringQueryInput`
allows to use localized names by setting a :class:`Symfony\\Contracts\\Translation\\TranslatableInterface`
as the fields label.

.. code-block:: php
:linenos:
use Symfony\Component\Translation\TranslatableMessage;
$userFieldSet = $searchFactory->createFieldSetBuilder()
->add('id', IntegerType::class, ['label' => new TranslatableMessage('label.id', [], 'search')]
->add('username', TextType::class, ['label' => new TranslatableMessage('label.username', [], 'search'))
->getFieldSet('users')
;
Now in the StringQuery can use a localized label like 'gebruikersnaam' (in Dutch)
instead of username. This option works the same for order fields.

Registering types and type extensions
-------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion lib/Core/Exporter/StringQueryExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class StringQueryExporter extends StringExporter
*/
public function __construct(?callable $labelResolver = null)
{
$this->labelResolver = $labelResolver ?? static fn (FieldConfig $field) => $field->getOption('label', $field->getName());
$this->labelResolver = $labelResolver ?? static fn (FieldConfig $field) => $field->getOption('label') ?? $field->getName();
}

protected function resolveLabels(FieldSet $fieldSet): array
Expand Down
2 changes: 2 additions & 0 deletions lib/Core/Extension/Core/Type/SearchFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'label' => null,
'translation_domain' => 'messages',
'invalid_message' => 'This value is not valid.',
'invalid_message_parameters' => [],
Expand All @@ -56,6 +57,7 @@ public function configureOptions(OptionsResolver $resolver): void
]
);

$resolver->setAllowedTypes('label', ['null', 'string']);
$resolver->setAllowedTypes('invalid_message', ['string']);
$resolver->setAllowedTypes('invalid_message_parameters', ['array']);
$resolver->setAllowedTypes(StringQueryInput::FIELD_LEXER_OPTION_NAME, ['null', \Closure::class]);
Expand Down
2 changes: 2 additions & 0 deletions lib/Core/Field/OrderFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function configureOptions(OptionsResolver $resolver): void
'view_label' => ['ASC' => 'asc', 'DESC' => 'desc'],
'type' => null,
'type_options' => [],
'label' => null,
]);

$resolver->setAllowedValues('case', [
Expand All @@ -48,6 +49,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('default', ['null', 'string']);
$resolver->setAllowedTypes('type', ['string', 'null']);
$resolver->setAllowedTypes('type_options', ['array']);
$resolver->setAllowedTypes('label', ['null', 'string']);

// Ensure view-labels are part of the alias list.
$resolver->addNormalizer('alias', static function (Options $options, array $value): mixed {
Expand Down
2 changes: 1 addition & 1 deletion lib/Core/Input/StringQueryInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class StringQueryInput extends StringInput
public function __construct(?Validator $validator = null, ?callable $labelResolver = null)
{
parent::__construct($validator);
$this->labelResolver = $labelResolver ?? static fn (FieldConfig $field) => $field->getOption('label', $field->getName());
$this->labelResolver = $labelResolver ?? static fn (FieldConfig $field) => $field->getOption('label') ?? $field->getName();
}

protected function initForProcess(ProcessorConfig $config): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@
<argument id="translator" type="service" />
</service>

<service id="Rollerworks\Bundle\SearchBundle\Type\TranslatableFieldTypeExtension">
<tag name="rollerworks_search.type_extension" extended-type="Rollerworks\Component\Search\Extension\Core\Type\SearchFieldType" />
</service>

<service id="Rollerworks\Bundle\SearchBundle\Type\TranslatableOrderFieldTypeExtension">
<tag name="rollerworks_search.type_extension" extended-type="Rollerworks\Component\Search\Field\OrderFieldType" />
</service>

</services>
</container>
22 changes: 11 additions & 11 deletions lib/Symfony/SearchBundle/TranslatorBasedAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
namespace Rollerworks\Bundle\SearchBundle;

use Rollerworks\Component\Search\Field\FieldConfig;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class TranslatorBasedAliasResolver
{
/**
* @var TranslatorInterface
*/
private $translator;
private TranslatorInterface $translator;

public function __construct(TranslatorInterface $translator)
{
Expand All @@ -30,14 +28,16 @@ public function __construct(TranslatorInterface $translator)

public function __invoke(FieldConfig $field)
{
if (null !== $label = $field->getOption('label')) {
return $label;
$label = $field->getOption('label');

if ($label === null) {
return $field->getName();
}

if ($label instanceof TranslatableInterface) {
return $label->trans($this->translator);
}

return $this->translator->trans(
$field->getOption('label_template', $field->getName()),
$field->getOption('label_parameters', []),
$field->getOption('label_domain', 'search')
);
return $label;
}
}
32 changes: 32 additions & 0 deletions lib/Symfony/SearchBundle/Type/TranslatableFieldTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Bundle\SearchBundle\Type;

use Rollerworks\Component\Search\Extension\Core\Type\SearchFieldType;
use Rollerworks\Component\Search\Field\AbstractFieldTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatableInterface;

final class TranslatableFieldTypeExtension extends AbstractFieldTypeExtension
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->addAllowedTypes('label', TranslatableInterface::class);
}

public function getExtendedType(): string
{
return SearchFieldType::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Bundle\SearchBundle\Type;

use Rollerworks\Component\Search\Field\AbstractFieldTypeExtension;
use Rollerworks\Component\Search\Field\OrderFieldType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatableInterface;

final class TranslatableOrderFieldTypeExtension extends AbstractFieldTypeExtension
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->addAllowedTypes('label', TranslatableInterface::class);
}

public function getExtendedType(): string
{
return OrderFieldType::class;
}
}

0 comments on commit 9e17420

Please sign in to comment.