Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EntityViewController
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 title
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBuilder
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getEntityViewDisplay
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_entity_view\Controller;
6
7use Drupal\Core\Entity\Display\EntityDisplayInterface;
8use Drupal\Core\Routing\RouteMatchInterface;
9use Drupal\Core\StringTranslation\TranslatableMarkup;
10use Drupal\display_builder\Controller\IntegrationControllerBase;
11use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13/**
14 * Defines a controller to get access the Display Builder admin UI.
15 *
16 * @internal
17 *   Controller classes are internal.
18 */
19final class EntityViewController extends IntegrationControllerBase {
20
21  /**
22   * Provides a generic title callback for a display used in entities.
23   *
24   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
25   *   The route match object.
26   *
27   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
28   *   The title for the display page.
29   */
30  public function title(RouteMatchInterface $route_match): TranslatableMarkup {
31    $param = [
32      '@bundle' => \ucfirst($route_match->getParameter('bundle')),
33      '@view_mode_name' => $route_match->getParameter('view_mode_name'),
34    ];
35
36    return $this->t('Display builder for @bundle, @view_mode_name', $param);
37  }
38
39  /**
40   * Renders the Layout UI.
41   *
42   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
43   *   The route match object.
44   *
45   * @return array
46   *   A render array.
47   */
48  public function getBuilder(RouteMatchInterface $route_match): array {
49    $entity_type_id = $route_match->getParameter('entity_type_id');
50    $bundle = $route_match->getParameter('bundle');
51    $view_mode = $route_match->getParameter('view_mode_name');
52
53    $entity_display = $this->getEntityViewDisplay($entity_type_id, $bundle, $view_mode);
54
55    if (!$entity_display) {
56      // No entity view display.
57      throw new NotFoundHttpException();
58    }
59
60    /** @var \Drupal\display_builder\DisplayBuildableInterface $buildable */
61    $buildable = $this->displayBuildableManager->createInstance('entity_view', ['entity' => $entity_display]);
62
63    return $this->renderBuilder($buildable);
64  }
65
66  /**
67   * Get entity view display entity.
68   *
69   * @param string $entity_type_id
70   *   Entity type ID.
71   * @param string $bundle
72   *   Fieldable entity's bundle.
73   * @param string $view_mode
74   *   View mode of the display.
75   *
76   * @return \Drupal\Core\Entity\Display\EntityDisplayInterface|null
77   *   The corresponding entity view display.
78   */
79  protected function getEntityViewDisplay(string $entity_type_id, string $bundle, string $view_mode): ?EntityDisplayInterface {
80    $display_id = \sprintf('%s.%s.%s', $entity_type_id, $bundle, $view_mode);
81    /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface|null $display */
82    $display = $this->entityTypeManager()->getStorage('entity_view_display')
83      ->load($display_id);
84
85    return $display;
86  }
87
88}