Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
75.00% |
15 / 20 |
|
50.00% |
5 / 10 |
|
37.50% |
3 / 8 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DisplayBuilderHtmx | |
75.00% |
15 / 20 |
|
50.00% |
5 / 10 |
|
37.50% |
3 / 8 |
|
66.67% |
2 / 3 |
28.78 | |
0.00% |
0 / 1 |
| outOfBand | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeOutOfBand | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| request | |
54.55% |
6 / 11 |
|
37.50% |
3 / 8 |
|
16.67% |
1 / 6 |
|
0.00% |
0 / 1 |
35.36 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder; |
| 6 | |
| 7 | use Drupal\Core\Htmx\Htmx; |
| 8 | use Drupal\Core\Url; |
| 9 | |
| 10 | /** |
| 11 | * Display Builder specific HTMX helpers, built on top of core's Htmx class. |
| 12 | */ |
| 13 | final class DisplayBuilderHtmx { |
| 14 | |
| 15 | /** |
| 16 | * Wraps a renderable array for an HTMX out-of-band swap. |
| 17 | * |
| 18 | * @param array $renderable |
| 19 | * The renderable array to be prepared. |
| 20 | * @param string $target_selector |
| 21 | * The selector for the target element. |
| 22 | * @param string $swap |
| 23 | * The swap method to use. |
| 24 | * |
| 25 | * @return array |
| 26 | * The wrapped renderable array. |
| 27 | */ |
| 28 | public static function outOfBand(array $renderable, string $target_selector, string $swap): array { |
| 29 | $wrapper = [ |
| 30 | '#type' => 'html_tag', |
| 31 | '#tag' => 'div', |
| 32 | 'content' => $renderable, |
| 33 | ]; |
| 34 | |
| 35 | (new Htmx())->swapOob($swap . ':' . $target_selector)->applyTo($wrapper); |
| 36 | |
| 37 | return $wrapper; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Alters a renderable array in place for an HTMX out-of-band swap. |
| 42 | * |
| 43 | * @param array $renderable |
| 44 | * The renderable array to be prepared. |
| 45 | * @param string $target_selector |
| 46 | * The selector for the target element. |
| 47 | * @param string $swap |
| 48 | * The swap method to use. |
| 49 | * |
| 50 | * @return array |
| 51 | * The altered renderable array. |
| 52 | */ |
| 53 | public static function makeOutOfBand(array $renderable, string $target_selector, string $swap): array { |
| 54 | (new Htmx())->swapOob($swap . ':' . $target_selector)->applyTo($renderable); |
| 55 | |
| 56 | return $renderable; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Builds a pre-configured request for a trigger/method/URL combination. |
| 61 | * |
| 62 | * Most Display Builder HTMX responses use out-of-band swaps, so the |
| 63 | * primary swap is deactivated by default. Callers may chain further |
| 64 | * core Htmx methods (::vals(), ::on(), ::prompt(), ::include(), ...) |
| 65 | * before calling ::applyTo() on the target render array. |
| 66 | * |
| 67 | * @param string $method |
| 68 | * The HTTP method: get, post, put, patch or delete. |
| 69 | * @param \Drupal\Core\Url $url |
| 70 | * The URL for the HTMX request. |
| 71 | * @param string $trigger |
| 72 | * The HTMX trigger. |
| 73 | * |
| 74 | * @return \Drupal\Core\Htmx\Htmx |
| 75 | * A pre-configured Htmx instance, ready for further chaining. |
| 76 | */ |
| 77 | public static function request(string $method, Url $url, string $trigger): Htmx { |
| 78 | $htmx = new Htmx(); |
| 79 | match ($method) { |
| 80 | 'get' => $htmx->get($url), |
| 81 | 'post' => $htmx->post($url), |
| 82 | 'put' => $htmx->put($url), |
| 83 | 'patch' => $htmx->patch($url), |
| 84 | 'delete' => $htmx->delete($url), |
| 85 | default => throw new \InvalidArgumentException(\sprintf('Unsupported HTMX request method "%s".', $method)), |
| 86 | }; |
| 87 | $htmx->trigger($trigger)->swap('none'); |
| 88 | |
| 89 | return $htmx; |
| 90 | } |
| 91 | |
| 92 | } |