Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProfileListBuilder
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 7
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 createInstance
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getFormId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildHeader
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 buildRow
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 render
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 listViewPanels
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_ui;
6
7use Drupal\Core\Config\Entity\DraggableListBuilder;
8use Drupal\Core\Entity\EntityInterface;
9use Drupal\Core\Entity\EntityStorageInterface;
10use Drupal\Core\Entity\EntityTypeInterface;
11use Drupal\Core\StringTranslation\TranslatableMarkup;
12use Drupal\display_builder\IslandPluginManagerInterface;
13use Drupal\display_builder\ProfileInterface;
14use Symfony\Component\DependencyInjection\ContainerInterface;
15
16/**
17 * Provides a listing of display builders.
18 */
19final class ProfileListBuilder extends DraggableListBuilder {
20
21  /**
22   * Island plugin manager.
23   *
24   * @var \Drupal\display_builder\IslandPluginManagerInterface
25   */
26  protected $islandManager;
27
28  /**
29   * {@inheritdoc}
30   */
31  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, IslandPluginManagerInterface $island_manager) {
32    parent::__construct($entity_type, $storage);
33    $this->islandManager = $island_manager;
34  }
35
36  /**
37   * {@inheritdoc}
38   */
39  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): self {
40    return new self(
41      $entity_type,
42      $container->get('entity_type.manager')->getStorage($entity_type->id()),
43      $container->get('plugin.manager.db_island'),
44    );
45  }
46
47  /**
48   * {@inheritdoc}
49   */
50  public function getFormId(): string {
51    return 'display_builder_list_builder';
52  }
53
54  /**
55   * {@inheritdoc}
56   */
57  public function buildHeader(): array {
58    $header = [];
59    $header['label'] = $this->t('Label');
60    $header['description'] = $this->t('Description');
61    $header['roles'] = $this->t('Roles');
62    $header['status'] = $this->t('Status');
63
64    return $header + parent::buildHeader();
65  }
66
67  /**
68   * {@inheritdoc}
69   */
70  public function buildRow(EntityInterface $entity): array {
71    $row = [];
72    /** @var \Drupal\display_builder\ProfileInterface $entity */
73    $row['label'] = $entity->label();
74    // List enabled view panels instead of showing an empty description.
75    $description = $entity->get('description') ?? $this->listViewPanels($entity);
76    $row['description']['data']['#plain_text'] = $description;
77    $row['roles']['data'] = [
78      '#theme' => 'item_list',
79      '#items' => $entity->getRoles(),
80      '#empty' => $this->t('No roles may use this display builder'),
81      '#context' => ['list_style' => 'comma-list'],
82    ];
83    $row['status']['data']['#plain_text'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled');
84
85    return $row + parent::buildRow($entity);
86  }
87
88  /**
89   * {@inheritdoc}
90   */
91  public function render(): array {
92    $build = parent::render();
93    $build['notice'] = [
94      '#markup' => $this->t('A Display builder profile is a configuration of the builder itself.<br>Each Display builder profile can be used to build a display with specific settings and capabilities.'),
95      '#weight' => -100,
96    ];
97
98    return $build;
99  }
100
101  /**
102   * List enabled view panels as a description fallback.
103   *
104   * @param \Drupal\display_builder\ProfileInterface $entity
105   *   The entity.
106   *
107   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
108   *   The description listing the panels.
109   */
110  protected function listViewPanels(ProfileInterface $entity): TranslatableMarkup {
111    $view_panels = $this->islandManager->getIslandsByTypes()['view'];
112    $view_panels = \array_intersect_key($view_panels, $entity->getEnabledIslands());
113    $view_panels = \array_map(static fn ($island) => $island->label(), $view_panels);
114
115    return $this->t('With: @panels', ['@panels' => \implode(', ', $view_panels)]);
116  }
117
118}