Skip to content

Commit dbf54f8

Browse files
author
Sagrario Meneses
committed
[ALICE-1062] Added help message.
1 parent 65710e2 commit dbf54f8

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

DependencyInjection/CompilerPass.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function process(ContainerBuilder $container)
1616
$this->loadFormlyFields($container);
1717
$this->loadDataProviders($container);
1818
$this->loadSubscribers($container);
19+
$this->loadHelpMessageProviders($container);
1920
}
2021

2122
/**
@@ -66,4 +67,19 @@ public function loadSubscribers(ContainerBuilder $container)
6667
}
6768
}
6869
}
70+
71+
/**
72+
* @param ContainerBuilder $container
73+
*/
74+
public function loadHelpMessageProviders(ContainerBuilder $container)
75+
{
76+
$containerDefinition = $container->getDefinition('dynamic_form.factory');
77+
$taggedServices = $container->findTaggedServiceIds('dynamic_form.help_message_provider');
78+
79+
foreach ($taggedServices as $id => $tags) {
80+
foreach ($tags as $attributes) {
81+
$containerDefinition->addMethodCall('addHelpMessageProvider', [$attributes['alias'], new Reference($id)]);
82+
}
83+
}
84+
}
6985
}

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function getConfigTreeBuilder()
4747

4848
->scalarNode('data_provider')
4949
->end()
50+
51+
->scalarNode('help_message_provider')
52+
->end()
5053
->end()
5154
->end()
5255
->end()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Linio\DynamicFormBundle\Form\Extension;
4+
5+
use Symfony\Component\Form\FormInterface;
6+
use Symfony\Component\Form\FormView;
7+
use Symfony\Component\Form\AbstractTypeExtension;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
use Symfony\Component\Form\Extension\Core\Type\FormType;
10+
11+
class HelpMessageTypeExtension extends AbstractTypeExtension
12+
{
13+
/**
14+
* @return string
15+
*/
16+
public function getExtendedType()
17+
{
18+
return FormType::class;
19+
}
20+
21+
/**
22+
* @param OptionsResolver $resolver
23+
*/
24+
public function configureOptions(OptionsResolver $resolver)
25+
{
26+
$resolver->setDefined(['help']);
27+
}
28+
29+
/**
30+
* @param FormView $view
31+
* @param FormInterface $form
32+
* @param array $options
33+
*/
34+
public function buildView(FormView $view, FormInterface $form, array $options)
35+
{
36+
$view->vars['help'] = isset($options['help']) ? $options['help'] : '';
37+
}
38+
}

Form/FormFactory.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Linio\DynamicFormBundle\Form;
44

55
use Linio\DynamicFormBundle\DataProvider;
6+
use Linio\DynamicFormBundle\HelpMessageProvider;
67
use Linio\DynamicFormBundle\Exception\NonExistentFormException;
78
use Linio\DynamicFormBundle\Exception\NotExistentDataProviderException;
89
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -32,6 +33,11 @@ class FormFactory
3233
*/
3334
protected $eventSubscribers = [];
3435

36+
/**
37+
* @var HelpMessageProvider[]
38+
*/
39+
protected $helpMessageProviders = [];
40+
3541
/**
3642
* @param SymfonyFormFactory $formFactory
3743
*/
@@ -57,6 +63,15 @@ public function addDataProvider($alias, DataProvider $dataProvider)
5763
$this->dataProviders[$alias] = $dataProvider;
5864
}
5965

66+
/**
67+
* @param string $alias
68+
* @param HelpMessageProvider $helpMessageProvider
69+
*/
70+
public function addHelpMessageProvider($alias, HelpMessageProvider $helpMessageProvider)
71+
{
72+
$this->helpMessageProviders[$alias] = $helpMessageProvider;
73+
}
74+
6075
/**
6176
* @param string $formName
6277
* @param EventSubscriberInterface $eventSubscriber
@@ -120,6 +135,10 @@ public function createBuilder($key, $data = [], $options = [], $name = null)
120135
$fieldOptions['choices'] = $this->loadDataProvider($fieldConfiguration['data_provider'])->getData();
121136
}
122137

138+
if (isset($fieldConfiguration['help_message_provider'])) {
139+
$fieldOptions['help'] = $this->loadHelpMessageProvider($fieldConfiguration['help_message_provider'])->getHelpMessage($fieldOptions['help']);
140+
}
141+
123142
if (isset($fieldConfiguration['validation'])) {
124143
$constraints = [];
125144

@@ -167,6 +186,22 @@ public function loadDataProvider($alias)
167186
return $this->dataProviders[$alias];
168187
}
169188

189+
/**
190+
* @param string $alias
191+
*
192+
* @return HelpMessageProvider
193+
*
194+
* @throws NotExistentDataProviderException
195+
*/
196+
public function loadHelpMessageProvider($alias)
197+
{
198+
if (!isset($this->helpMessageProviders[$alias])) {
199+
throw new NotExistentDataProviderException();
200+
}
201+
202+
return $this->helpMessageProviders[$alias];
203+
}
204+
170205
/**
171206
* @param string $name
172207
*

HelpMessageProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Linio\DynamicFormBundle;
4+
5+
interface HelpMessageProvider
6+
{
7+
/**
8+
* @param string $key
9+
*
10+
* @return string
11+
*/
12+
public function getHelpMessage($key);
13+
}

Resources/config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@ services:
104104
lazy: true
105105
tags:
106106
- {name: formly_field, alias: url}
107+
108+
form.type_extension.help_message:
109+
class: Linio\DynamicFormBundle\Form\Extension\HelpMessageTypeExtension
110+
tags:
111+
- {name: form.type_extension, alias: Symfony\Component\Form\Extension\Core\Type\FormType}

0 commit comments

Comments
 (0)