Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| WidgetLayoutService | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLayouts | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Service; |
| 6 | |
| 7 | use Drupal\Core\Cache\CacheBackendInterface; |
| 8 | use Drupal\Core\Extension\ModuleHandlerInterface; |
| 9 | |
| 10 | /** |
| 11 | * Cached widget layouts from hook_name_widget_layouts(). |
| 12 | * |
| 13 | * @internal |
| 14 | */ |
| 15 | class WidgetLayoutService implements WidgetLayoutInterface { |
| 16 | |
| 17 | /** |
| 18 | * Request-level cache; mirrors prior drupal_static() semantics. |
| 19 | * |
| 20 | * Unset and empty array are falsy so empty hook results re-resolve each call. |
| 21 | * |
| 22 | * @var array<string, mixed>|null |
| 23 | */ |
| 24 | private ?array $layouts = NULL; |
| 25 | |
| 26 | public function __construct( |
| 27 | private readonly CacheBackendInterface $cache, |
| 28 | private readonly ModuleHandlerInterface $moduleHandler, |
| 29 | ) {} |
| 30 | |
| 31 | /** |
| 32 | * Returns keyed widget layout definitions. |
| 33 | */ |
| 34 | public function getLayouts(): array { |
| 35 | if (!$this->layouts) { |
| 36 | $cid = 'name:widget_layouts'; |
| 37 | $cache = $this->cache->get($cid); |
| 38 | if ($cache) { |
| 39 | $this->layouts = is_array($cache->data) ? $cache->data : []; |
| 40 | } |
| 41 | if (!$cache) { |
| 42 | $layouts = $this->moduleHandler->invokeAll('name_widget_layouts'); |
| 43 | foreach ($layouts as &$layout) { |
| 44 | $layout += [ |
| 45 | 'library' => [], |
| 46 | 'wrapper_attributes' => [], |
| 47 | ]; |
| 48 | $layout['wrapper_attributes']['class'][] = 'name-widget-wrapper'; |
| 49 | } |
| 50 | unset($layout); |
| 51 | $this->cache->set($cid, $layouts); |
| 52 | $this->layouts = $layouts; |
| 53 | } |
| 54 | } |
| 55 | return $this->layouts ?? []; |
| 56 | } |
| 57 | |
| 58 | } |