Skip to content

Commit 19dd007

Browse files
authored
Merge pull request #2 from Sylius/fix-province-on-checkout
Add and document ProvinceController polyfill
2 parents e6c914b + a09c184 commit 19dd007

File tree

5 files changed

+152
-23
lines changed

5 files changed

+152
-23
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ fos_rest:
6060
- { path: '^/', stop: true }
6161
```
6262

63-
### 3. Configure Order Controllers
63+
### 3. Configure Controllers
6464

65-
Configure the Sylius order controllers to use the legacy bridge functionality:
65+
Configure the Sylius controllers to use the legacy bridge functionality:
6666

67-
**Option A: If you have NOT overridden Order/OrderItem controllers in your project**
67+
**Option A: If you have NOT overridden the following controllers in your project**
6868

6969
Add the following to your `config/packages/_sylius.yaml`:
7070

@@ -77,9 +77,15 @@ sylius_order:
7777
order_item:
7878
classes:
7979
controller: Sylius\LegacyBridgePlugin\Controller\OrderItemController
80+
81+
sylius_addressing:
82+
resources:
83+
province:
84+
classes:
85+
controller: Sylius\LegacyBridgePlugin\Controller\ProvinceController
8086
```
8187

82-
**Option B: If you HAVE already overridden Order/OrderItem controllers in your project**
88+
**Option B: If you HAVE already any of these controllers in your project**
8389

8490
Add the appropriate traits to your existing controllers:
8591

@@ -112,6 +118,21 @@ class OrderItemController extends ResourceController
112118
}
113119
```
114120

121+
```php
122+
// src/Controller/ProvinceController.php
123+
namespace App\Controller;
124+
125+
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
126+
use Sylius\LegacyBridgePlugin\Controller\Trait\ProvinceTrait;
127+
128+
class ProvinceController extends ResourceController
129+
{
130+
use ProvinceTrait; // Adds: choiceOrTextFieldFormAction() and helper methods
131+
132+
// ... your existing custom methods
133+
}
134+
```
135+
115136
### 4. Update UI Configuration
116137

117138
Replace `sylius_ui` configuration with `sylius_legacy_bridge` in your `config/packages/sylius_ui.yaml` (or wherever your UI events are configured):

config/routes/shop/ajax.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ sylius_shop_ajax_cart:
99
resource: "@SyliusLegacyBridgePlugin/config/routes/shop/ajax/cart.yml"
1010
prefix: /cart
1111

12-
#sylius_shop_ajax_render_province_form:
13-
# path: /render-province-form
14-
# methods: [GET]
15-
# defaults:
16-
# _controller: sylius.controller.province::choiceOrTextFieldFormAction
17-
# _sylius:
18-
# template: "@SyliusShop/Common/Form/_province.html.twig"
12+
sylius_shop_ajax_render_province_form:
13+
path: /render-province-form
14+
methods: [GET]
15+
defaults:
16+
_controller: sylius.controller.province::choiceOrTextFieldFormAction
17+
_sylius:
18+
template: "@SyliusShop/Common/Form/_province.html.twig"

config/routes/shop/ajax/user.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# This file is part of the Sylius package.
22
# (c) Sylius Sp. z o.o.
33

4-
#sylius_shop_ajax_user_check_action:
5-
# path: /check
6-
# methods: [GET]
7-
# defaults:
8-
# _controller: sylius.controller.shop_user::showAction
9-
# _format: json
10-
# _sylius:
11-
# repository:
12-
# method: findOneByEmail
13-
# arguments:
14-
# email: $email
15-
# serialization_groups: [Secured]
4+
sylius_shop_ajax_user_check_action:
5+
path: /check
6+
methods: [GET]
7+
defaults:
8+
_controller: sylius.controller.shop_user::showAction
9+
_format: json
10+
_sylius:
11+
repository:
12+
method: findOneByEmail
13+
arguments:
14+
email: $email
15+
serialization_groups: [Secured]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\LegacyBridgePlugin\Controller;
15+
16+
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17+
use Sylius\LegacyBridgePlugin\Controller\Trait\ProvinceTrait;
18+
19+
class ProvinceController extends ResourceController
20+
{
21+
use ProvinceTrait;
22+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\LegacyBridgePlugin\Controller\Trait;
6+
7+
use Sylius\Bundle\AddressingBundle\Form\Type\ProvinceCodeChoiceType;
8+
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
9+
use Sylius\Component\Addressing\Model\CountryInterface;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormInterface;
12+
use Symfony\Component\HttpFoundation\JsonResponse;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17+
18+
/** @mixin ResourceController */
19+
trait ProvinceTrait
20+
{
21+
/**
22+
* @throws AccessDeniedException
23+
* @throws NotFoundHttpException
24+
*/
25+
public function choiceOrTextFieldFormAction(Request $request): Response
26+
{
27+
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
28+
29+
if (!$configuration->isHtmlRequest() || null === $countryCode = $request->query->get('countryCode')) {
30+
throw new AccessDeniedException();
31+
}
32+
33+
/** @var CountryInterface|null $country */
34+
$country = $this->get('sylius.repository.country')->findOneBy(['code' => $countryCode]);
35+
if ($country === null) {
36+
throw new NotFoundHttpException('Requested country does not exist.');
37+
}
38+
39+
if (!$country->hasProvinces()) {
40+
$form = $this->createProvinceTextForm();
41+
42+
$content = $this->renderView(
43+
$configuration->getTemplate('_provinceText.html'),
44+
[
45+
'metadata' => $this->metadata,
46+
'form' => $form->createView(),
47+
],
48+
);
49+
50+
return new JsonResponse([
51+
'content' => $content,
52+
]);
53+
}
54+
55+
$form = $this->createProvinceChoiceForm($country);
56+
57+
$content = $this->renderView(
58+
$configuration->getTemplate('_provinceChoice.html'),
59+
[
60+
'metadata' => $this->metadata,
61+
'form' => $form->createView(),
62+
],
63+
);
64+
65+
return new JsonResponse([
66+
'content' => $content,
67+
]);
68+
}
69+
70+
protected function createProvinceChoiceForm(CountryInterface $country): FormInterface
71+
{
72+
return $this->get('form.factory')->createNamed('sylius_address_province', ProvinceCodeChoiceType::class, null, [
73+
'country' => $country,
74+
'label' => 'sylius.form.address.province',
75+
'placeholder' => 'sylius.form.province.select',
76+
]);
77+
}
78+
79+
protected function createProvinceTextForm(): FormInterface
80+
{
81+
return $this->get('form.factory')->createNamed('sylius_address_province', TextType::class, null, [
82+
'required' => false,
83+
'label' => 'sylius.form.address.province',
84+
]);
85+
}
86+
}

0 commit comments

Comments
 (0)