|
| 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 | + |
| 13 | + |
| 14 | + |
| 15 | +For no link, there nothing to do : |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +For an external link, only input target url : |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +For an internal link, first choose the internal target page among all available pages : |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +Then input parameters if there are some : |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +When admin form is loaded the link form is collpased but a button allows to open and update it. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 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`. |
0 commit comments