Skip to content

Commit

Permalink
Add Language::locale in the form as a preview only, update CQRS query…
Browse files Browse the repository at this point in the history
… result and improve behat tests
  • Loading branch information
jolelievre committed Dec 18, 2024
1 parent 0c823d8 commit e4c007c
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 291 deletions.
9 changes: 3 additions & 6 deletions src/Adapter/Language/CommandHandler/EditLanguageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,12 @@ private function uploadFlagImageIfChanged(Language $language, EditLanguageComman
*/
private function assertLanguageWithIsoCodeDoesNotExist(Language $language, EditLanguageCommand $command)
{
if (null !== $command->getIsoCode()) {
if (null === $command->getIsoCode()) {
return;
}

/* @phpstan-ignore-next-line */
if ($language->iso_code === $command->getIsoCode()->getValue() && Language::getIdByIso($command->getIsoCode()->getValue())
) {
/* @phpstan-ignore-next-line */
throw new LanguageConstraintException(sprintf('Language with ISO code "%s" already exists', $command->getIsoCode()->getValue()), LanguageConstraintException::INVALID_ISO_CODE);
if ($language->iso_code !== $command->getIsoCode()->getValue() && Language::getIdByIso($command->getIsoCode()->getValue())) {
throw new LanguageConstraintException(sprintf('Language with ISO code "%s" already exists', $command->getIsoCode()->getValue()), LanguageConstraintException::DUPLICATE_ISO_CODE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
use PrestaShop\PrestaShop\Core\Domain\Language\Query\GetLanguageForEditing;
use PrestaShop\PrestaShop\Core\Domain\Language\QueryHandler\GetLanguageForEditingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Language\QueryResult\EditableLanguage;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\IsoCode;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\LanguageId;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\TagIETF;

/**
* Gets language for editing
Expand All @@ -52,10 +50,11 @@ public function handle(GetLanguageForEditing $query)
$language = $this->getLegacyLanguageObject($query->getLanguageId());

return new EditableLanguage(
$query->getLanguageId(),
$query->getLanguageId()->getValue(),
$language->name,
new IsoCode($language->iso_code),
new TagIETF($language->language_code),
$language->iso_code,
$language->language_code,
$language->locale,
$language->date_format_lite,
$language->date_format_full,
(bool) $language->is_rtl,
Expand Down
138 changes: 24 additions & 114 deletions src/Core/Domain/Language/QueryResult/EditableLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,161 +26,71 @@

namespace PrestaShop\PrestaShop\Core\Domain\Language\QueryResult;

use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\IsoCode;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\LanguageId;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\TagIETF;

/**
* Transfers editable language's data
*/
class EditableLanguage
{
/**
* @var LanguageId
*/
private $languageId;

/**
* @var string
*/
private $name;

/**
* @var IsoCode
*/
private $isoCode;

/**
* @var TagIETF
*/
private $tagIETF;

/**
* @var string
*/
private $shortDateFormat;

/**
* @var string
*/
private $fullDateFormat;

/**
* @var bool
*/
private $isRtl;

/**
* @var bool
*/
private $isActive;

/**
* @var array
*/
private $shopAssociation;

/**
* @param LanguageId $languageId
* @param string $name
* @param IsoCode $isoCode
* @param TagIETF $tagIETF
* @param string $shortDateFormat
* @param string $fullDateFormat
* @param bool $isRtl
* @param bool $isActive
* @param array $shopAssociation
*/
public function __construct(
LanguageId $languageId,
$name,
IsoCode $isoCode,
TagIETF $tagIETF,
$shortDateFormat,
$fullDateFormat,
$isRtl,
$isActive,
array $shopAssociation
private readonly int $languageId,
private readonly string $name,
private readonly string $isoCode,
private readonly string $tagIETF,
private readonly string $locale,
private readonly string $shortDateFormat,
private readonly string $fullDateFormat,
private readonly bool $isRtl,
private readonly bool $isActive,
private readonly array $shopAssociation,
) {
$this->languageId = $languageId;
$this->name = $name;
$this->isoCode = $isoCode;
$this->tagIETF = $tagIETF;
$this->shortDateFormat = $shortDateFormat;
$this->fullDateFormat = $fullDateFormat;
$this->isRtl = $isRtl;
$this->isActive = $isActive;
$this->shopAssociation = $shopAssociation;
}

/**
* @return LanguageId
*/
public function getLanguageId()
public function getLanguageId(): int
{
return $this->languageId;
}

/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* @return IsoCode
*/
public function getIsoCode()
public function getIsoCode(): string
{
return $this->isoCode;
}

/**
* @return TagIETF
*/
public function getTagIETF()
public function getTagIETF(): string
{
return $this->tagIETF;
}

/**
* @return string
*/
public function getShortDateFormat()
public function getLocale(): string
{
return $this->locale;
}

public function getShortDateFormat(): string
{
return $this->shortDateFormat;
}

/**
* @return string
*/
public function getFullDateFormat()
public function getFullDateFormat(): string
{
return $this->fullDateFormat;
}

/**
* @return bool
*/
public function isRtl()
public function isRtl(): bool
{
return $this->isRtl;
}

/**
* @return bool
*/
public function isActive()
public function isActive(): bool
{
return $this->isActive;
}

/**
* @return array
*/
public function getShopAssociation()
public function getShopAssociation(): array
{
return $this->shopAssociation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,26 @@
*/
final class LanguageFormDataProvider implements FormDataProviderInterface
{
/**
* @var CommandBusInterface
*/
private $bus;

/**
* @var bool
*/
private $isMultistoreFeatureActive;

/**
* @var int[]
*/
private $defaultShopAssociation;

/**
* @param CommandBusInterface $bus
* @param bool $isMultistoreFeatureActive
* @param int[] $defaultShopAssociation
*/
public function __construct(
CommandBusInterface $bus,
$isMultistoreFeatureActive,
array $defaultShopAssociation
private readonly CommandBusInterface $bus,
private readonly bool $isMultistoreFeatureActive,
private readonly array $defaultShopAssociation
) {
$this->bus = $bus;
$this->isMultistoreFeatureActive = $isMultistoreFeatureActive;
$this->defaultShopAssociation = $defaultShopAssociation;
}

/**
* {@inheritdoc}
*/
public function getData($languageId)
public function getData($id)
{
/** @var EditableLanguage $editableLanguage */
$editableLanguage = $this->bus->handle(new GetLanguageForEditing($languageId));
$editableLanguage = $this->bus->handle(new GetLanguageForEditing($id));

$data = [
'name' => $editableLanguage->getName(),
'iso_code' => $editableLanguage->getIsoCode()->getValue(),
'tag_ietf' => $editableLanguage->getTagIETF()->getValue(),
'iso_code' => $editableLanguage->getIsoCode(),
'tag_ietf' => $editableLanguage->getTagIETF(),
'locale' => $editableLanguage->getLocale(),
'short_date_format' => $editableLanguage->getShortDateFormat(),
'full_date_format' => $editableLanguage->getFullDateFormat(),
'is_rtl' => $editableLanguage->isRtl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected function getFilters()
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->translator->trans('Search ISO code', [], 'Admin.Internaltional.Help'),
'placeholder' => $this->translator->trans('Search ISO code', [], 'Admin.International.Help'),
],
])
->setAssociatedColumn('iso_code')
Expand All @@ -223,7 +223,7 @@ protected function getFilters()
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->translator->trans('Search code', [], 'Admin.Internaltional.Help'),
'placeholder' => $this->translator->trans('Search code', [], 'Admin.International.Help'),
],
])
->setAssociatedColumn('language_code')
Expand All @@ -233,7 +233,7 @@ protected function getFilters()
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->translator->trans('Search locale', [], 'Admin.Internaltional.Help'),
'placeholder' => $this->translator->trans('Search locale', [], 'Admin.International.Help'),
],
])
->setAssociatedColumn('locale')
Expand All @@ -243,7 +243,7 @@ protected function getFilters()
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->translator->trans('Search date format', [], 'Admin.Internaltional.Help'),
'placeholder' => $this->translator->trans('Search date format', [], 'Admin.International.Help'),
],
])
->setAssociatedColumn('date_format_lite')
Expand All @@ -253,7 +253,7 @@ protected function getFilters()
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->translator->trans('Search date format', [], 'Admin.Internaltional.Help'),
'placeholder' => $this->translator->trans('Search date format', [], 'Admin.International.Help'),
],
])
->setAssociatedColumn('date_format_full')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,14 @@ public function editAction(
}
}

/** @var EditableLanguage $editableLanguage */
$editableLanguage = $this->dispatchQuery(new GetLanguageForEditing((int) $languageId));

return $this->render('@PrestaShop/Admin/Improve/International/Language/edit.html.twig', [
'languageForm' => $languageForm->createView(),
'editableLanguage' => $editableLanguage,
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
'enableSidebar' => true,
'layoutTitle' => $this->trans(
'Editing language %name%',
[
'%name%' => $editableLanguage->getName(),
'%name%' => $languageForm->get('name')->getData(),
],
'Admin.Navigation.Menu'
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex;
use PrestaShopBundle\Form\Admin\Type\ShopChoiceTreeType;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TextPreviewType;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand Down Expand Up @@ -104,7 +105,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'maxLength' => 5,
],
'label' => $this->trans('Language code', 'Admin.International.Feature'),
'help' => $this->trans('IETF language tag (e.g. en-US, pt-BR).', 'Admin.International.Help'),
'help' => $this->trans('IETF language tag (e.g. en-US, pt-BR) in lower case.', 'Admin.International.Help'),
'constraints' => [
new NotBlank([
'message' => $this->trans('This field cannot be empty.', 'Admin.Notifications.Error'),
Expand All @@ -114,6 +115,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
]),
],
])
->add('locale', TextPreviewType::class, [
'label' => $this->trans('Locale', 'Admin.International.Feature'),
'help' => $this->trans('IETF language tag (e.g. en-US, pt-BR).', 'Admin.International.Help'),
])
->add('short_date_format', TextType::class, [
'label' => $this->trans('Date format', 'Admin.International.Feature'),
'help' => $this->trans('Short date format (e.g., Y-m-d).', 'Admin.International.Help'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,7 @@
</span>
{% endif %}
</span>
{{- block('form_help') -}}
{% endblock %}

{% block link_preview_widget %}
Expand Down
Loading

0 comments on commit e4c007c

Please sign in to comment.