Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
HtmxTrait | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
20 | |
0.00% |
0 / 1 |
addOutOfBand | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
makeOutOfBand | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
addTarget | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
setTrigger | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder; |
6 | |
7 | use Drupal\Core\Url; |
8 | |
9 | /** |
10 | * Trait for handling HTMX attributes and rendering. |
11 | */ |
12 | trait HtmxTrait { |
13 | |
14 | /** |
15 | * Wrap a renderable array for out-of-band swapping. |
16 | * |
17 | * @param array $renderable |
18 | * The renderable array to be prepared. |
19 | * @param string $target_selector |
20 | * The selector for the target element. |
21 | * @param string $swap |
22 | * The swap method to use. |
23 | * |
24 | * @return array |
25 | * The wrapped renderable array. |
26 | */ |
27 | protected function addOutOfBand(array $renderable, string $target_selector, string $swap): array { |
28 | return [ |
29 | '#type' => 'html_tag', |
30 | '#tag' => 'div', |
31 | '#attributes' => [ |
32 | 'hx-swap-oob' => $swap . ':' . $target_selector, |
33 | ], |
34 | 'content' => $renderable, |
35 | ]; |
36 | } |
37 | |
38 | /** |
39 | * Alter a renderable array for out-of-band swapping. |
40 | * |
41 | * @param array $renderable |
42 | * The renderable array to be prepared. |
43 | * @param string $target_selector |
44 | * The selector for the target element. |
45 | * @param string $swap |
46 | * The swap method to use. |
47 | * |
48 | * @return array |
49 | * The altered renderable array. |
50 | */ |
51 | protected function makeOutOfBand(array $renderable, string $target_selector, string $swap): array { |
52 | $renderable['#attributes']['hx-swap-oob'] = $swap . ':' . $target_selector; |
53 | |
54 | return $renderable; |
55 | } |
56 | |
57 | /** |
58 | * Adds a target and swap method to an array of attributes. |
59 | * |
60 | * @param array $attr |
61 | * The array of attributes to modify. |
62 | * @param string $target |
63 | * (Optional) The target selector. Defaults to 'this'. |
64 | * |
65 | * @return array |
66 | * The modified array of attributes. |
67 | */ |
68 | protected function addTarget(array $attr, string $target = 'this'): array { |
69 | $attr['hx-target'] = $target; |
70 | $attr['hx-swap'] = 'outerHTML'; |
71 | |
72 | return $attr; |
73 | } |
74 | |
75 | /** |
76 | * Sets the trigger and method for an HTMX request. |
77 | * |
78 | * @param string $trigger |
79 | * The trigger event for the request. |
80 | * @param string $method |
81 | * The HTTP method to use. |
82 | * @param \Drupal\Core\Url $url |
83 | * The URL for the request. |
84 | * |
85 | * @return array |
86 | * An array of attributes for the HTMX request. |
87 | */ |
88 | protected function setTrigger(string $trigger, string $method, Url $url): array { |
89 | return [ |
90 | 'hx-' . $method => $url->toString(), |
91 | 'hx-trigger' => $trigger, |
92 | // Most HTMX responses will use OOB swaps so let's de activate swapping by |
93 | // default. |
94 | 'hx-swap' => 'none', |
95 | ]; |
96 | } |
97 | |
98 | } |