Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
40 / 40 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Form | |
100.00% |
40 / 40 |
|
100.00% |
5 / 5 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 | |||
| validate | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| submit | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\visitors\Plugin\VisitorsEvent; |
| 4 | |
| 5 | use Drupal\Core\Form\FormStateInterface; |
| 6 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 7 | use Drupal\Core\Routing\RouteMatchInterface; |
| 8 | use Drupal\visitors\Attribute\VisitorsEvent; |
| 9 | use Drupal\visitors\Plugin\VisitorsEventBase; |
| 10 | use Drupal\visitors\VisitorsEventPluginInterface; |
| 11 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 12 | |
| 13 | /** |
| 14 | * Plugin implementation for form event. |
| 15 | */ |
| 16 | #[VisitorsEvent( |
| 17 | id: "form", |
| 18 | label: "Form", |
| 19 | weight: 10, |
| 20 | )] |
| 21 | class Form extends VisitorsEventBase implements VisitorsEventPluginInterface, ContainerFactoryPluginInterface { |
| 22 | |
| 23 | /** |
| 24 | * Constructs a new Form object. |
| 25 | * |
| 26 | * @param array $configuration |
| 27 | * A configuration array containing information about the plugin instance. |
| 28 | * @param string $plugin_id |
| 29 | * The plugin_id for the plugin instance. |
| 30 | * @param mixed $plugin_definition |
| 31 | * The plugin implementation definition. |
| 32 | * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch |
| 33 | * The route match service. |
| 34 | */ |
| 35 | public function __construct( |
| 36 | array $configuration, |
| 37 | $plugin_id, |
| 38 | $plugin_definition, |
| 39 | protected readonly RouteMatchInterface $routeMatch, |
| 40 | ) { |
| 41 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 49 | return new static( |
| 50 | $configuration, |
| 51 | $plugin_id, |
| 52 | $plugin_definition, |
| 53 | $container->get('current_route_match'), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * {@inheritdoc} |
| 59 | */ |
| 60 | public function process(array $context): ?array { |
| 61 | |
| 62 | $forms = &drupal_static('visitors_form_alter') ?? []; |
| 63 | |
| 64 | if ($forms && count($forms) == 1) { |
| 65 | |
| 66 | $form_id = array_keys($forms)[0]; |
| 67 | $base_form_id = array_values($forms)[0]; |
| 68 | $return['event'] = 'view'; |
| 69 | $return['variables']['plugin_var_3'] = $base_form_id ?? $form_id; |
| 70 | $return['variables']['plugin_var_4'] = $form_id; |
| 71 | |
| 72 | $form_validate = &drupal_static('visitors_form_validate') ?? []; |
| 73 | if ($form_validate && count($form_validate) == 1) { |
| 74 | $return['event'] = 'validate'; |
| 75 | } |
| 76 | |
| 77 | $form_submit = &drupal_static('visitors_form_submit') ?? []; |
| 78 | if ($form_submit && count($form_submit) == 1) { |
| 79 | $return['event'] = 'submit'; |
| 80 | } |
| 81 | |
| 82 | return $return; |
| 83 | } |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * A validation callback for forms. |
| 90 | * |
| 91 | * @param array $form |
| 92 | * The form array. |
| 93 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 94 | * The form state. |
| 95 | */ |
| 96 | public static function validate(array &$form, FormStateInterface $form_state) { |
| 97 | $static = &drupal_static('visitors_form_validate'); |
| 98 | |
| 99 | $form_object = $form_state->getFormObject(); |
| 100 | if (!$form_object) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | $form_id = $form_object->getFormId(); |
| 105 | $base_form_id = $form_id; |
| 106 | if (method_exists($form_object, 'getBaseFormId')) { |
| 107 | $base_form_id = $form_object->getBaseFormId(); |
| 108 | } |
| 109 | |
| 110 | $static[$form_id] = $base_form_id; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * A submit callback for forms. |
| 115 | * |
| 116 | * @param array $form |
| 117 | * The form array. |
| 118 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 119 | * The form state. |
| 120 | */ |
| 121 | public static function submit(array &$form, FormStateInterface $form_state) { |
| 122 | $static = &drupal_static('visitors_form_submit'); |
| 123 | |
| 124 | $form_object = $form_state->getFormObject(); |
| 125 | if (!$form_object) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | $form_id = $form_object->getFormId(); |
| 130 | $base_form_id = $form_id; |
| 131 | if (method_exists($form_object, 'getBaseFormId')) { |
| 132 | $base_form_id = $form_object->getBaseFormId(); |
| 133 | } |
| 134 | |
| 135 | $static[$form_id] = $base_form_id; |
| 136 | } |
| 137 | |
| 138 | } |