Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PreprocessViewsView | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
preprocessViewsView | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder_views\Hook; |
6 | |
7 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
8 | use Drupal\Core\Hook\Attribute\Hook; |
9 | use Drupal\Core\Plugin\Context\Context; |
10 | use Drupal\Core\Plugin\Context\ContextDefinition; |
11 | use Drupal\Core\Plugin\Context\EntityContext; |
12 | use Drupal\ui_patterns\Element\ComponentElementBuilder; |
13 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
14 | |
15 | /** |
16 | * Hook implementations for the display_builder_views module. |
17 | */ |
18 | class PreprocessViewsView { |
19 | |
20 | public function __construct( |
21 | protected EntityTypeManagerInterface $entityTypeManager, |
22 | #[Autowire('@ui_patterns.component_element_builder')] |
23 | protected ComponentElementBuilder $componentElementBuilder, |
24 | ) {} |
25 | |
26 | /** |
27 | * Implements hook_preprocess_HOOK() for 'views_view'. |
28 | * |
29 | * @param array $variables |
30 | * An associative array containing the variables to pass to the template. |
31 | */ |
32 | #[Hook('preprocess_views_view')] |
33 | public function preprocessViewsView(array &$variables): void { |
34 | $view = $variables['view']; |
35 | $extenders = $view->getDisplay()->getExtenders(); |
36 | |
37 | if (!isset($extenders['display_builder'])) { |
38 | return; |
39 | } |
40 | |
41 | $extender = $extenders['display_builder']; |
42 | $sources = $extender->getSources(); |
43 | |
44 | // We fallback on normal View if Display Builder is empty or disabled! |
45 | if (empty($sources) || !$extender->getProfile()) { |
46 | return; |
47 | } |
48 | |
49 | // Inject the view in context to be available by our UI Patterns source |
50 | // plugins. |
51 | $contexts = []; |
52 | $view_entity = $this->entityTypeManager->getStorage('view')->load($view->id()); |
53 | $contexts['ui_patterns_views:view_entity'] = EntityContext::fromEntity($view_entity); |
54 | $contexts['ui_patterns_views:rows'] = new Context(new ContextDefinition('any'), $variables['rows'] ?? []); |
55 | // @todo pass all variables for each source, find a way to do it sooner than |
56 | // in this preprocess if possible. |
57 | $contexts['ui_patterns_views:variables'] = new Context(new ContextDefinition('any'), $variables); |
58 | |
59 | $fake_build = []; |
60 | |
61 | foreach ($sources as $source) { |
62 | $fake_build = $this->componentElementBuilder->buildSource($fake_build, 'content', [], $source, $contexts); |
63 | } |
64 | |
65 | // Init the variable to render in views-view.html.twig. |
66 | // @see \Drupal\display_builder_views\Plugin\views\display_extender\DisplayExtender::preExecute() |
67 | $variables['content'] = $fake_build['#slots']['content'] ?? []; |
68 | $variables['content']['#cache'] = $fake_build['#cache'] ?? []; |
69 | } |
70 | |
71 | } |