Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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 | 12 | 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 | 5 | return new static( |
| 50 | 5 | $configuration, |
| 51 | 5 | $plugin_id, |
| 52 | 5 | $plugin_definition, |
| 53 | 5 | $container->get('current_route_match'), |
| 54 | 5 | ); |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * {@inheritdoc} | |
| 59 | */ | |
| 60 | public function process(array $context): ?array { | |
| 61 | ||
| 62 | 4 | $forms = &drupal_static('visitors_form_alter') ?? []; |
| 63 | ||
| 64 | 4 | if ($forms && count($forms) == 1) { |
| 65 | ||
| 66 | 3 | $form_id = array_keys($forms)[0]; |
| 67 | 3 | $base_form_id = array_values($forms)[0]; |
| 68 | 3 | $return['event'] = 'view'; |
| 69 | 3 | $return['variables']['plugin_var_3'] = $base_form_id ?? $form_id; |
| 70 | 3 | $return['variables']['plugin_var_4'] = $form_id; |
| 71 | ||
| 72 | 3 | $form_validate = &drupal_static('visitors_form_validate') ?? []; |
| 73 | 3 | if ($form_validate && count($form_validate) == 1) { |
| 74 | 1 | $return['event'] = 'validate'; |
| 75 | } | |
| 76 | ||
| 77 | 3 | $form_submit = &drupal_static('visitors_form_submit') ?? []; |
| 78 | 3 | if ($form_submit && count($form_submit) == 1) { |
| 79 | 1 | $return['event'] = 'submit'; |
| 80 | } | |
| 81 | ||
| 82 | 3 | return $return; |
| 83 | } | |
| 84 | ||
| 85 | 1 | 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 | 4 | if (!\Drupal::service('visitors.visibility')->isVisible()) { |
| 98 | 1 | return; |
| 99 | } | |
| 100 | ||
| 101 | 4 | $static = &drupal_static('visitors_form_validate'); |
| 102 | ||
| 103 | 4 | $form_object = $form_state->getFormObject(); |
| 104 | 4 | if (!$form_object) { |
| 105 | 1 | return; |
| 106 | } | |
| 107 | ||
| 108 | 4 | $form_id = $form_object->getFormId(); |
| 109 | 4 | $base_form_id = $form_id; |
| 110 | 4 | if (method_exists($form_object, 'getBaseFormId')) { |
| 111 | 1 | $base_form_id = $form_object->getBaseFormId(); |
| 112 | } | |
| 113 | ||
| 114 | 4 | $static[$form_id] = $base_form_id; |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * A submit callback for forms. | |
| 119 | * | |
| 120 | * @param array $form | |
| 121 | * The form array. | |
| 122 | * @param \Drupal\Core\Form\FormStateInterface $form_state | |
| 123 | * The form state. | |
| 124 | */ | |
| 125 | public static function submit(array &$form, FormStateInterface $form_state) { | |
| 126 | 4 | if (!\Drupal::service('visitors.visibility')->isVisible()) { |
| 127 | 1 | return; |
| 128 | } | |
| 129 | ||
| 130 | 4 | $static = &drupal_static('visitors_form_submit'); |
| 131 | ||
| 132 | 4 | $form_object = $form_state->getFormObject(); |
| 133 | 4 | if (!$form_object) { |
| 134 | 1 | return; |
| 135 | } | |
| 136 | ||
| 137 | 4 | $form_id = $form_object->getFormId(); |
| 138 | 4 | $base_form_id = $form_id; |
| 139 | 4 | if (method_exists($form_object, 'getBaseFormId')) { |
| 140 | 1 | $base_form_id = $form_object->getBaseFormId(); |
| 141 | } | |
| 142 | ||
| 143 | 4 | $static[$form_id] = $base_form_id; |
| 144 | } | |
| 145 | ||
| 146 | } |