Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PreprocessViewsView | |
0.00% |
0 / 18 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
n/a |
0 / 0 |
n/a |
0 / 0 |
|
0.00% |
0 / 1 |
2 | |||||
| preprocessViewsView | |
0.00% |
0 / 17 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 9 |
|
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\display_builder\DisplayBuildablePluginManager; |
| 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 | #[Autowire('@plugin.manager.display_buildable')] |
| 25 | private DisplayBuildablePluginManager $displayBuildableManager, |
| 26 | ) {} |
| 27 | |
| 28 | /** |
| 29 | * Implements hook_preprocess_HOOK() for 'views_view'. |
| 30 | * |
| 31 | * @param array $variables |
| 32 | * An associative array containing the variables to pass to the template. |
| 33 | */ |
| 34 | #[Hook('preprocess_views_view')] |
| 35 | public function preprocessViewsView(array &$variables): void { |
| 36 | $view = $variables['view']; |
| 37 | $extenders = $view->getDisplay()->getExtenders(); |
| 38 | |
| 39 | if (!isset($extenders['display_builder'])) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $extender = $extenders['display_builder']; |
| 44 | /** @var \Drupal\display_builder\DisplayBuildableInterface $buildable */ |
| 45 | $buildable = $this->displayBuildableManager->createInstance('view_display', ['extender' => $extender]); |
| 46 | $sources = $buildable->getSources(); |
| 47 | |
| 48 | // We fallback on normal View if Display Builder is empty or disabled! |
| 49 | if (empty($sources) || !$buildable->getProfile()) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // Inject the view in context to be available by our UI Patterns source |
| 54 | // plugins. |
| 55 | $contexts = $buildable->getAvailableContexts(); |
| 56 | // Needed by \Drupal\display_builder_views\Plugin\ViewsUiPatternsSourceBase. |
| 57 | // @todo pass all variables for each source, find a way to do it sooner than |
| 58 | // in this preprocess if possible. |
| 59 | // @todo Can we remove it once #3533043 and #3542796 are done? |
| 60 | $contexts['ui_patterns_views:variables'] = new Context(new ContextDefinition('any'), $variables); |
| 61 | // Needed by ui_patterns_views's ViewRowsSource. |
| 62 | $contexts['ui_patterns_views:rows'] = new Context(new ContextDefinition('any'), $variables['rows'] ?? []); |
| 63 | |
| 64 | $fake_build = []; |
| 65 | |
| 66 | foreach ($sources as $source) { |
| 67 | $fake_build = $this->componentElementBuilder->buildSource($fake_build, 'content', [], $source, $contexts); |
| 68 | } |
| 69 | |
| 70 | // Init the variable to render in views-view.html.twig. |
| 71 | // @see \Drupal\display_builder_views\Plugin\views\display_extender\DisplayExtender::preExecute() |
| 72 | $variables['content'] = $fake_build['#slots']['content'] ?? []; |
| 73 | $variables['content']['#cache'] = $fake_build['#cache'] ?? []; |
| 74 | } |
| 75 | |
| 76 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 35 | public function preprocessViewsView(array &$variables): void { |
| 36 | $view = $variables['view']; |
| 37 | $extenders = $view->getDisplay()->getExtenders(); |
| 38 | |
| 39 | if (!isset($extenders['display_builder'])) { |
| 40 | return; |
| 43 | $extender = $extenders['display_builder']; |
| 44 | /** @var \Drupal\display_builder\DisplayBuildableInterface $buildable */ |
| 45 | $buildable = $this->displayBuildableManager->createInstance('view_display', ['extender' => $extender]); |
| 46 | $sources = $buildable->getSources(); |
| 47 | |
| 48 | // We fallback on normal View if Display Builder is empty or disabled! |
| 49 | if (empty($sources) || !$buildable->getProfile()) { |
| 49 | if (empty($sources) || !$buildable->getProfile()) { |
| 49 | if (empty($sources) || !$buildable->getProfile()) { |
| 50 | return; |
| 55 | $contexts = $buildable->getAvailableContexts(); |
| 56 | // Needed by \Drupal\display_builder_views\Plugin\ViewsUiPatternsSourceBase. |
| 57 | // @todo pass all variables for each source, find a way to do it sooner than |
| 58 | // in this preprocess if possible. |
| 59 | // @todo Can we remove it once #3533043 and #3542796 are done? |
| 60 | $contexts['ui_patterns_views:variables'] = new Context(new ContextDefinition('any'), $variables); |
| 61 | // Needed by ui_patterns_views's ViewRowsSource. |
| 62 | $contexts['ui_patterns_views:rows'] = new Context(new ContextDefinition('any'), $variables['rows'] ?? []); |
| 63 | |
| 64 | $fake_build = []; |
| 65 | |
| 66 | foreach ($sources as $source) { |
| 66 | foreach ($sources as $source) { |
| 66 | foreach ($sources as $source) { |
| 67 | $fake_build = $this->componentElementBuilder->buildSource($fake_build, 'content', [], $source, $contexts); |
| 66 | foreach ($sources as $source) { |
| 67 | $fake_build = $this->componentElementBuilder->buildSource($fake_build, 'content', [], $source, $contexts); |
| 68 | } |
| 69 | |
| 70 | // Init the variable to render in views-view.html.twig. |
| 71 | // @see \Drupal\display_builder_views\Plugin\views\display_extender\DisplayExtender::preExecute() |
| 72 | $variables['content'] = $fake_build['#slots']['content'] ?? []; |
| 73 | $variables['content']['#cache'] = $fake_build['#cache'] ?? []; |
| 74 | } |