Skip to content

Commit 0ffd2cd

Browse files
author
Maxime
authored
Merge pull request #10 from delyriand/feature/use-new-email-manager-service
Use correct email manager serverivce for Sylius > 1.13, and add messages for constraints and prefill name
2 parents f1d41dd + 4da6ce2 commit 0ffd2cd

File tree

8 files changed

+62
-18
lines changed

8 files changed

+62
-18
lines changed

.github/workflows/recipe.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
php: ['8.1', '8.2', '8.3']
18-
sylius: ["~1.12.0", "~1.13.0", "1.14.0"]
18+
sylius: ["~1.13.0", "1.14.0"]
1919

2020
steps:
2121
- name: Setup PHP

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ This plugin allows you to customize the contact page on the front-end of your Sy
1616

1717
| Sylius Version | PHP Version |
1818
|----------------|-----------------|
19-
| 1.12 | 8.1 - 8.2 - 8.3 |
2019
| 1.13 | 8.1 - 8.2 - 8.3 |
2120
| 1.14 | 8.1 - 8.2 - 8.3 |
2221

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "MIT",
77
"require": {
88
"php": "^8.1",
9-
"sylius/sylius": ">=1.12 <2.0",
9+
"sylius/sylius": ">=1.13 <2.0",
1010
"monsieurbiz/sylius-settings-plugin": "^1.2.0",
1111
"monsieurbiz/sylius-rich-editor-plugin": "^2.8.0"
1212
},

src/EmailManager/DecorateContactEmailManager.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use MonsieurBiz\SyliusContactRequestPlugin\Factory\ContactRequestFactoryInterface;
1818
use MonsieurBiz\SyliusSettingsPlugin\Provider\SettingsProviderInterface;
19-
use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
19+
use Sylius\Bundle\CoreBundle\Mailer\ContactEmailManagerInterface;
20+
use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface as OldContactEmailManagerInterface;
2021
use Sylius\Component\Core\Model\ChannelInterface;
2122

2223
final class DecorateContactEmailManager implements ContactEmailManagerInterface
2324
{
2425
public function __construct(
25-
private ContactEmailManagerInterface $decoratedContactEmailManager,
26+
private OldContactEmailManagerInterface|ContactEmailManagerInterface $decoratedContactEmailManager,
2627
private ContactRequestFactoryInterface $contactRequestFactory,
2728
private EntityManagerInterface $contactRequestManager,
2829
private SettingsProviderInterface $settingProvider,
2930
) {
31+
if ($this->decoratedContactEmailManager instanceof OldContactEmailManagerInterface) {
32+
trigger_deprecation(
33+
'sylius/shop-bundle',
34+
'1.13',
35+
'The "%s" interface is deprecated, use "%s" instead.',
36+
OldContactEmailManagerInterface::class,
37+
ContactEmailManagerInterface::class,
38+
);
39+
}
3040
}
3141

3242
public function sendContactRequest(array $data, array $recipients, ChannelInterface $channel = null, string $localeCode = null): void

src/Form/Extension/ContactTypeExtension.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515

1616
use MonsieurBiz\SyliusSettingsPlugin\Provider\SettingsProviderInterface;
1717
use Sylius\Bundle\CoreBundle\Form\Type\ContactType;
18+
use Sylius\Component\Customer\Context\CustomerContextInterface;
1819
use Symfony\Component\Form\AbstractTypeExtension;
1920
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
2021
use Symfony\Component\Form\Extension\Core\Type\TelType;
2122
use Symfony\Component\Form\Extension\Core\Type\TextType;
2223
use Symfony\Component\Form\FormBuilderInterface;
24+
use Symfony\Component\Form\FormEvent;
25+
use Symfony\Component\Form\FormEvents;
2326
use Symfony\Component\Validator\Constraints as Assert;
2427

2528
final class ContactTypeExtension extends AbstractTypeExtension
2629
{
2730
public function __construct(
2831
private SettingsProviderInterface $settingProvider,
32+
private CustomerContextInterface $customerContext,
2933
) {
3034
}
3135

@@ -36,15 +40,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3640
{
3741
parent::buildForm($builder, $options);
3842

39-
$defaultConstraints = [
40-
new Assert\Length(['max' => 255]),
41-
];
42-
43-
$requiredConstraints = [
44-
new Assert\NotBlank(),
45-
new Assert\Length(['max' => 255]),
46-
];
47-
4843
$isNameRequired = $this->isFieldRequired('field_name_required', 'field_name_displayed');
4944
$isCompanyRequired = $this->isFieldRequired('field_company_required', 'field_company_displayed');
5045
$isPhoneNumberRequired = $this->isFieldRequired('field_phone_number_required', 'field_phone_number_displayed');
@@ -53,17 +48,18 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5348
->add('name', TextType::class, [
5449
'label' => 'monsieurbiz.contact_request.form.name',
5550
'required' => $isNameRequired,
56-
'constraints' => $isNameRequired ? $requiredConstraints : $defaultConstraints,
51+
'constraints' => $this->getConstraints($isNameRequired, 'monsieurbiz.contact_request.name.not_blank'),
5752
])
5853
->add('company', TextType::class, [
5954
'label' => 'monsieurbiz.contact_request.form.company',
6055
'required' => $isCompanyRequired,
61-
'constraints' => $isCompanyRequired ? $requiredConstraints : $defaultConstraints,
56+
'constraints' => $this->getConstraints($isCompanyRequired, 'monsieurbiz.contact_request.company.not_blank'),
6257
])
6358
->add('phoneNumber', TelType::class, [
6459
'label' => 'monsieurbiz.contact_request.form.phone_number',
60+
'invalid_message' => 'monsieurbiz.contact_request.phone_number.invalid',
6561
'required' => $isPhoneNumberRequired,
66-
'constraints' => $isPhoneNumberRequired ? $requiredConstraints : $defaultConstraints,
62+
'constraints' => $this->getConstraints($isPhoneNumberRequired, 'monsieurbiz.contact_request.phone_number.not_blank'),
6763
])
6864
;
6965

@@ -82,6 +78,19 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
8278
],
8379
]);
8480
}
81+
82+
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
83+
$name = $this->customerContext->getCustomer()?->getFullName();
84+
if (null === $name) {
85+
return;
86+
}
87+
88+
/** @var array $data */
89+
$data = $event->getData();
90+
$data['name'] = $name;
91+
92+
$event->setData($data);
93+
});
8594
}
8695

8796
public static function getExtendedTypes(): iterable
@@ -96,4 +105,16 @@ private function isFieldRequired(string $path, string $pathDisplayed): bool
96105
return $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $path)
97106
&& $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $pathDisplayed);
98107
}
108+
109+
private function getConstraints(bool $isRequired, ?string $blankMessage): array
110+
{
111+
return $isRequired ? [
112+
new Assert\NotBlank([
113+
'message' => $blankMessage,
114+
]),
115+
new Assert\Length(['max' => 255]),
116+
] : [
117+
new Assert\Length(['max' => 255]),
118+
];
119+
}
99120
}

src/Resources/config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010

1111
# Save contact request entity
1212
MonsieurBiz\SyliusContactRequestPlugin\EmailManager\DecorateContactEmailManager:
13-
decorates: 'sylius.email_manager.contact'
13+
decorates: 'sylius.mailer.contact_email_manager.shop'
1414
arguments: [ '@.inner' ]
1515

1616
# Add admin menu
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
monsieurbiz:
22
contact_request:
33
confirmation_error: 'You need to approve this field'
4+
name:
5+
not_blank: Please enter your name
6+
company:
7+
not_blank: Please enter your company name
8+
phone_number:
9+
invalid: Please enter a valid phone number
10+
not_blank: Please enter your phone number
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
monsieurbiz:
22
contact_request:
33
confirmation_error: 'Vous devez approuver ce champ'
4+
name:
5+
not_blank: Veuillez entrer votre nom
6+
company:
7+
not_blank: Veuillez entrer le nom de votre société
8+
phone_number:
9+
invalid: Veuillez entrer un numéro de téléphone valide
10+
not_blank: Veuillez entrer votre numéro de téléphone

0 commit comments

Comments
 (0)