Spaces:
No application file
No application file
File size: 8,529 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
<?php
namespace Mautic\FormBundle\Event;
use Mautic\CoreBundle\Event\ComponentValidationTrait;
use Mautic\CoreBundle\Exception\BadConfigurationException;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\Translation\TranslatorInterface;
class FormBuilderEvent extends Event
{
use ComponentValidationTrait;
private array $actions = [];
private array $fields = [];
private array $validators = [];
public function __construct(
private TranslatorInterface $translator
) {
}
/**
* Adds a submit action to the list of available actions.
*
* @param string $key a unique identifier; it is recommended that it be namespaced i.e. lead.action
* @param array $action can contain the following keys:
* $action = [
* 'group' => (required) Label of the group to add this action to
* 'label' => (required) what to display in the list
* 'eventName' => (required) Event dispatched to execute action; it will receive a SubmissionEvent object
* 'formType' => (required) name of the form type SERVICE for the action
* 'allowCampaignForm' => (optional) true to allow this action for campaign forms; defaults to false
* 'description' => (optional) short description of event
* 'template' => (optional) template to use for the action's HTML in the form builder; eg AcmeMyBundle:FormAction:theaction.html.twig
* 'formTypeOptions' => (optional) array of options to pass to formType
* 'formTheme' => (optional theme for custom form views
* ]
*
* @throws BadConfigurationException
*/
public function addSubmitAction(string $key, array $action): void
{
if (array_key_exists($key, $this->actions)) {
throw new \InvalidArgumentException("The key, '$key' is already used by another action. Please use a different key.");
}
// check for required keys and that given functions are callable
$this->verifyComponent(
['group', 'label', 'formType', 'eventName'],
$action
);
$action['label'] = $this->translator->trans($action['label']);
$action['description'] ??= '';
$this->actions[$key] = $action;
}
/**
* Get submit actions.
*
* @return array
*/
public function getSubmitActions()
{
uasort(
$this->actions,
fn ($a, $b): int => strnatcasecmp(
$a['label'],
$b['label']
)
);
return $this->actions;
}
/**
* Get submit actions by groups.
*/
public function getSubmitActionGroups(): array
{
$actions = $this->getSubmitActions();
$groups = [];
foreach ($actions as $key => $action) {
$groups[$action['group']][$key] = $action;
}
return $groups;
}
/**
* Adds a form field to the list of available fields in the form builder.
*
* @param string $key unique identifier; it is recommended that it be namespaced i.e. leadbundle.myfield
* @param array $field can contain the following key/values
* $field = [
* 'label' => (required) what to display in the list
* 'formType' => (required) name of the form type SERVICE for the field's property column
* 'template' => (required) template to use for the field's HTML eg AcmeMyBundle:FormField:thefield.html.twig
* 'formTypeOptions' => (optional) array of options to pass to formType
* 'formTheme' => (optional) theme for custom form view
* 'valueFilter' => (optional) the filter to use to clean the input as supported by InputHelper or a callback;
* should accept arguments FormField $field and $filteredValue
* 'builderOptions' => (optional) array of options
* [
* 'addHelpMessage' => (bool) show help message inputs
* 'addShowLabel' => (bool) show label input
* 'addDefaultValue' => (bool) show default value input
* 'addLabelAttributes' => (bool) show label attribute input
* 'addInputAttributes' => (bool) show input attribute input
* 'addIsRequired' => (bool) show is required toggle
* ]
* ]
*
* @throws \InvalidArgumentException
* @throws BadConfigurationException
*/
public function addFormField($key, array $field): void
{
if (array_key_exists($key, $this->fields)) {
throw new \InvalidArgumentException("The key, '$key' is already used by another field. Please use a different key.");
}
$callbacks = [];
// Only validate valueFilter if it's not a InputHelper method
if (isset($field['valueFilter'])
&& (!is_string($field['valueFilter'])
|| !is_callable(
[\Mautic\CoreBundle\Helper\InputHelper::class, $field['valueFilter']]
))
) {
$callbacks = ['valueFilter'];
}
$this->verifyComponent(['label', 'formType', 'template'], $field, $callbacks);
$this->fields[$key] = $field;
}
/**
* Get form fields.
*
* @return mixed
*/
public function getFormFields()
{
return $this->fields;
}
/**
* Add a field validator.
*
* $validator = [
* 'eventName' => (required) Event name to dispatch to validate the form; it will recieve a ValidationEvent object
* 'fieldType' => (optional) Optional filter to validate only a specific type of field; otherwise every field
* will be sent through the validation event
* ]
*/
public function addValidator($key, array $validator): void
{
if (array_key_exists($key, $this->fields)) {
throw new \InvalidArgumentException("The key, '$key' is already used by another validator. Please use a different key.");
}
// check for required keys and that given functions are callable
$this->verifyComponent(['eventName'], $validator);
$this->validators[$key] = $validator;
}
/**
* @param FormInterface<object> $form
*/
public function addValidatorsToBuilder(FormInterface $form): void
{
if (!empty($this->validators)) {
$validationData = $form->getData()['validation'] ?? [];
foreach ($this->validators as $validator) {
if (isset($validator['formType']) && isset($validator['fieldType']) && $validator['fieldType'] == $form->getData()['type']) {
$form->add(
'validation',
$validator['formType'],
[
'label' => false,
'data' => $validationData,
]
);
}
}
}
}
/**
* Returns validators organized by ['form' => [], 'fieldType' => [], ...
*/
public function getValidators(): array
{
// Organize by field
$fieldValidators = [
'form' => [],
];
foreach ($this->validators as $validator) {
if (isset($validator['fieldType'])) {
if (!isset($fieldValidators[$validator['fieldType']])) {
$fieldValidators[$validator['fieldType']] = [];
}
$fieldValidators[$validator['fieldType']] = $validator['eventName'];
} else {
$fieldValidators['form'] = $validator['eventName'];
}
}
return $fieldValidators;
}
}
|