|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (C) 2025 SYSTOPIA GmbH |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +declare(strict_types=1); |
| 21 | + |
| 22 | +namespace Drupal\json_forms\Form\Control\Callbacks; |
| 23 | + |
| 24 | +use Drupal\Core\Form\FormStateInterface; |
| 25 | + |
| 26 | +final class TriggeringElementCallback { |
| 27 | + |
| 28 | + /** |
| 29 | + * @param array<int|string, mixed> $element |
| 30 | + * |
| 31 | + * @return array<int|string, mixed> |
| 32 | + */ |
| 33 | + public static function onAfterBuild(array $element, FormStateInterface $formState): array { |
| 34 | + // For disabled elements the triggering element is not set. (Required if |
| 35 | + // disabled element is used for the initial calculation call.) |
| 36 | + if (NULL === $formState->getTriggeringElement() && self::elementTriggeredScriptedSubmission($element, $formState)) { |
| 37 | + $formState->setTriggeringElement($element); |
| 38 | + } |
| 39 | + |
| 40 | + return $element; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Based on |
| 45 | + * \Drupal\Core\Form\FormBuilder::elementTriggeredScriptedSubmission(). |
| 46 | + * |
| 47 | + * Detects if an element triggered the form submission via Ajax. |
| 48 | + * |
| 49 | + * This detects button or non-button controls that trigger a form submission |
| 50 | + * via Ajax or some other scriptable environment. These environments can set |
| 51 | + * the special input key '_triggering_element_name' to identify the triggering |
| 52 | + * element. If the name alone doesn't identify the element uniquely, the input |
| 53 | + * key '_triggering_element_value' may also be set to require a match on |
| 54 | + * element value. An example where this is needed is if there are several |
| 55 | + * // buttons all named 'op', and only differing in their value. |
| 56 | + * |
| 57 | + * @param array<int|string, mixed> $element |
| 58 | + */ |
| 59 | + private static function elementTriggeredScriptedSubmission(array $element, FormStateInterface $formState): bool { |
| 60 | + $input = $formState->getUserInput(); |
| 61 | + if ($element['#name'] === ($input['_triggering_element_name'] ?? NULL)) { |
| 62 | + // @phpstan-ignore equal.notAllowed |
| 63 | + if (!isset($input['_triggering_element_value']) || $input['_triggering_element_value'] == $element['#value']) { |
| 64 | + return TRUE; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return FALSE; |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments