Skip to content

Commit 82033d2

Browse files
committed
add InternalExternalLink form type
1 parent 13cba06 commit 82033d2

29 files changed

+1422
-8
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"php": "^7.1",
1414
"sonata-project/admin-bundle": "^3.1",
1515
"sonata-project/doctrine-orm-admin-bundle": "^3.0",
16+
"sonata-project/page-bundle": "^3.8",
1617
"symfony/framework-bundle": "^3.0 || ^4.0"
1718
},
1819
"require-dev": {

docs/00-docs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
## Immutable tabs form type
44

55
- [Immutable Tabs Form Type](./10-immutable-tabs-type.md)
6+
7+
- [Internal External Form Type](./20-internal-external-form-type.md)

docs/10-immutable-tabs-type.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ To get tabs into a block of page or admin form.
1212

1313
It extends the `Sonata\CoreBundle\Form\Type\ImmutableArrayType`
1414

15-
And use a specific widget defined in `src/AdminBundle/Ressources/Views/form_admin_fields.html.twig` so you must add it to your admin sonata config
15+
And uses a specific widget defined in `src/Ressources/views/Form/form_admin_fields.html.twig` so you must add it to your admin sonata config
1616

17-
```
17+
```yaml
1818
sonata_admin:
1919
admin_services:
20-
canalplus.awaken.admin.item:
20+
your.admin.entity:
2121
templates:
22-
form: ['CanalPlusAwakenAdminBundle:Form:form_admin_fields.html.twig']
22+
form: ['SonataHelperBundle:Form:form_admin_fields.html.twig']
2323
```
2424
25-
Don't forget to add the css `src/AdminBundle/Ressources/public/css/immutableTabsType.css` to have cute tabs
25+
Don't forget to add the css `src/Ressources/public/css/immutableTabsType.css` to have cute tabs
2626

2727
An array of tabs must be set
2828
key : key of tab that will be used to record value in database
@@ -32,7 +32,7 @@ This array must be send in the options array with the key 'tabs'
3232

3333
### Example of page block with an immutableTabsType
3434

35-
```
35+
```php
3636
/**
3737
* Class TitleBlockService.
3838
*/
@@ -98,7 +98,7 @@ data are a json with the key of the immutableTabsType then the key of tabs as su
9898

9999
### Exemple of data
100100

101-
````
101+
````json
102102
{
103103
"settings": {
104104
"fr": {
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Internal External Form Type
2+
3+
## For what
4+
5+
Form type to input an link in an admin form
6+
7+
You can choice a type of link :
8+
- no link
9+
- link to an external link
10+
- link to an internal page of your admin. they can be filtered by current site if needed.
11+
12+
![internal-external-link-choices.png](img/internal-external-link-choices.png)
13+
14+
15+
For no link, there nothing to do :
16+
17+
![internal-external-link-none.png](img/internal-external-link-none.png)
18+
19+
20+
For an external link, only input target url :
21+
22+
![internal-external-link-external.png](img/internal-external-link-external.png)
23+
24+
25+
For an internal link, first choose the internal target page among all available pages :
26+
27+
![internal-external-link-internal-choice](img/internal-external-link-internal-choice.png)
28+
29+
Then input parameters if there are some :
30+
31+
![internal-external-link-internal-params](img/internal-external-link-internal-params.png)
32+
33+
When admin form is loaded the link form is collpased but a button allows to open and update it.
34+
35+
![internal-external-link-external-closed](img/internal-external-link-external-closed.png)
36+
37+
![internal-external-link-internal-closed](img/internal-external-link-internal-closed.png)
38+
39+
40+
## How it works
41+
42+
InternalExternalLink form type groups many fields to record each type of values.
43+
Javascript is used to display form and manage interaction between each field.
44+
45+
A specific Twig block is used to format all fields and add classes that will be used as selector by javascript :
46+
block `sonata_type_internal_external_link_type_widget` of `src/Ressources/views/Form/form_admin_fields.html.twig`
47+
48+
49+
## Installation
50+
51+
You must include css `src/Ressources/public/css/linkWidget.css` to your project
52+
53+
You must call the `src/Ressources/public/js/backoffice.js` to init InternalExternalLink form type if present in current admin
54+
55+
## How to implement an InternalExternalLink form type to your admin
56+
57+
It uses a specific widget defined in `src/Ressources/views/Form/form_admin_fields.html.twig` so you must add it to your admin sonata config
58+
59+
```yaml
60+
sonata_admin:
61+
admin_services:
62+
your.admin.entity:
63+
templates:
64+
form: ['SonataHelperBundle:Form:form_admin_fields.html.twig']
65+
```
66+
67+
You must inject to your admin the service `sonata.page.manager.page` with method `setPageManager`. It will be passed to InternalExternalLink form types to get internal pages.
68+
69+
You can add each field of InternalExternalLink form type to your entity to save them in separated properties but it is more easy to encapsulate an
70+
InternalExternalLink form type in an ImmutableArrayType form type so all fields will be save in one property of your entity with a json format.
71+
72+
So you must add property and getter/setter to your entity.
73+
74+
```php
75+
class YourEntity
76+
{
77+
/**
78+
* @var string
79+
*/
80+
protected $backLink;
81+
82+
/**
83+
* @return array
84+
*/
85+
public function getLink()
86+
{
87+
return json_decode($this->link, true);
88+
}
89+
90+
/**
91+
* @param array $link This should be an array containing the following keys:
92+
* - linkType
93+
* - link
94+
* - page
95+
* - params
96+
*
97+
* @return $this
98+
*/
99+
public function setLink(array $link)
100+
{
101+
$this->link = json_encode($link);
102+
103+
return $this;
104+
}
105+
106+
...
107+
}
108+
```
109+
110+
Then you can use an InternalExternalLink form type to your admin form by example :
111+
112+
```php
113+
use Sonata\SonataHelpersBundle\Form\DataTransformer\PageDataTransformer;
114+
use Sonata\SonataHelpersBundle\Manager\PageManagerInterface;
115+
116+
class YourAdmin extends AbstractAdmin
117+
{
118+
/**
119+
* @var PageManagerInterface
120+
*/
121+
private $pageManager;
122+
123+
/**
124+
* @param PageManagerInterface $pageManager
125+
*/
126+
public function setPageManager(PageManagerInterface $pageManager)
127+
{
128+
$this->pageManager = $pageManager;
129+
}
130+
131+
...
132+
133+
/**
134+
* {@inheritdoc}
135+
*/
136+
protected function configureFormFields(FormMapper $formMapper)
137+
{
138+
$site = $this->getSubject()->getSite(); // To filter page by the site of the current object
139+
140+
$formMapper
141+
->with('Lien de retour', ['class' => 'col-md-6'])
142+
->add('backLabel', TextType::class, [
143+
'label' => 'Libellé du lien',
144+
'required' => true,
145+
])
146+
->add('Link', ImmutableArrayType::class, [
147+
'keys' => [
148+
['link', InternalExternalLinkType::class, ['site' => $site, 'label' => false, 'required' => false]],
149+
],
150+
'label' => false,
151+
])
152+
->end()
153+
}
154+
155+
...
156+
}
157+
```
158+
159+
160+
## Validator
161+
162+
You can check InternalExternalLink form type data with validator `src/Validator/Constraints/InternalExternalLinkValidator.php`.
141 KB
Loading
126 KB
Loading
137 KB
Loading
195 KB
Loading
123 KB
Loading
163 KB
Loading

0 commit comments

Comments
 (0)