Coverage index

src/Plugin/VisitorsEvent/Form.php

Lines: 100.00%

LineHitsSource
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
  ) {
4112
    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) {
495
    return new static(
505
      $configuration,
515
      $plugin_id,
525
      $plugin_definition,
535
      $container->get('current_route_match'),
545
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function process(array $context): ?array {
61
624
    $forms = &drupal_static('visitors_form_alter') ?? [];
63
644
    if ($forms && count($forms) == 1) {
65
663
      $form_id = array_keys($forms)[0];
673
      $base_form_id = array_values($forms)[0];
683
      $return['event'] = 'view';
693
      $return['variables']['plugin_var_3'] = $base_form_id ?? $form_id;
703
      $return['variables']['plugin_var_4'] = $form_id;
71
723
      $form_validate = &drupal_static('visitors_form_validate') ?? [];
733
      if ($form_validate && count($form_validate) == 1) {
741
        $return['event'] = 'validate';
75
      }
76
773
      $form_submit = &drupal_static('visitors_form_submit') ?? [];
783
      if ($form_submit && count($form_submit) == 1) {
791
        $return['event'] = 'submit';
80
      }
81
823
      return $return;
83
    }
84
851
    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) {
974
    if (!\Drupal::service('visitors.visibility')->isVisible()) {
981
      return;
99
    }
100
1014
    $static = &drupal_static('visitors_form_validate');
102
1034
    $form_object = $form_state->getFormObject();
1044
    if (!$form_object) {
1051
      return;
106
    }
107
1084
    $form_id = $form_object->getFormId();
1094
    $base_form_id = $form_id;
1104
    if (method_exists($form_object, 'getBaseFormId')) {
1111
      $base_form_id = $form_object->getBaseFormId();
112
    }
113
1144
    $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) {
1264
    if (!\Drupal::service('visitors.visibility')->isVisible()) {
1271
      return;
128
    }
129
1304
    $static = &drupal_static('visitors_form_submit');
131
1324
    $form_object = $form_state->getFormObject();
1334
    if (!$form_object) {
1341
      return;
135
    }
136
1374
    $form_id = $form_object->getFormId();
1384
    $base_form_id = $form_id;
1394
    if (method_exists($form_object, 'getBaseFormId')) {
1401
      $base_form_id = $form_object->getBaseFormId();
141
    }
142
1434
    $static[$form_id] = $base_form_id;
144
  }
145
146
}