Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Hook; | |
| 6 | ||
| 7 | use Drupal\Core\Form\FormStateInterface; | |
| 8 | use Drupal\Core\Hook\Attribute\Hook; | |
| 9 | use Drupal\visitors\Plugin\VisitorsEvent\Form; | |
| 10 | use Drupal\visitors\VisitorsVisibilityInterface; | |
| 11 | ||
| 12 | /** | |
| 13 | * Implements hook_form_alter(). | |
| 14 | */ | |
| 15 | class FormHooks { | |
| 16 | ||
| 17 | /** | |
| 18 | * Constructs a FormHooks object. | |
| 19 | * | |
| 20 | * @param \Drupal\visitors\VisitorsVisibilityInterface $visibility | |
| 21 | * The visitors visibility service. | |
| 22 | */ | |
| 23 | public function __construct( | |
| 24 | protected VisitorsVisibilityInterface $visibility, | |
| 25 | 4 | ) {} |
| 26 | ||
| 27 | /** | |
| 28 | * Implements hook_form_alter(). | |
| 29 | */ | |
| 30 | #[Hook('form_alter')] | |
| 31 | public function formAlter(&$form, FormStateInterface $form_state, $form_id) { | |
| 32 | // Skipped pages must not record a form view or submit event. | |
| 33 | 4 | if (!$this->visibility->isVisible()) { |
| 34 | 1 | return; |
| 35 | } | |
| 36 | ||
| 37 | 4 | $static =& drupal_static('visitors_form_alter'); |
| 38 | 4 | $form_object = $form_state->getFormObject(); |
| 39 | 4 | if (method_exists($form_object, 'getBaseFormId')) { |
| 40 | 1 | $base_form_id = $form_object->getBaseFormId(); |
| 41 | } | |
| 42 | else { | |
| 43 | 4 | $base_form_id = $form_id; |
| 44 | } | |
| 45 | 4 | $static[$form_id] = $base_form_id; |
| 46 | 4 | $form['#submit'][] = [ |
| 47 | 4 | Form::class, |
| 48 | 4 | 'submit', |
| 49 | 4 | ]; |
| 50 | 4 | $form['#validate'][] = [ |
| 51 | 4 | Form::class, |
| 52 | 4 | 'validate', |
| 53 | 4 | ]; |
| 54 | } | |
| 55 | ||
| 56 | } |