Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
PatternPresetListBuilder
0.00% covered (danger)
0.00%
0 / 55
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 9
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
2
 createInstance
0.00% covered (danger)
0.00%
0 / 6
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
2
 getFormId
0.00% covered (danger)
0.00%
0 / 1
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
2
 buildHeader
0.00% covered (danger)
0.00%
0 / 7
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
2
 buildRow
0.00% covered (danger)
0.00%
0 / 12
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
20
 buildForm
0.00% covered (danger)
0.00%
0 / 5
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
6
 render
0.00% covered (danger)
0.00%
0 / 8
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
2
 prettyPrintContexts
0.00% covered (danger)
0.00%
0 / 10
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
20
 getThemeName
0.00% covered (danger)
0.00%
0 / 4
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_ui;
6
7use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
8use Drupal\Core\Config\Entity\DraggableListBuilder;
9use Drupal\Core\Entity\EntityInterface;
10use Drupal\Core\Entity\EntityTypeInterface;
11use Drupal\Core\Extension\ThemeExtensionList;
12use Drupal\Core\Form\FormStateInterface;
13use Drupal\Core\Render\Element;
14use Symfony\Component\DependencyInjection\ContainerInterface;
15
16/**
17 * Provides a listing of Pattern presets.
18 */
19final class PatternPresetListBuilder extends DraggableListBuilder {
20
21  /**
22   * The theme extension list.
23   */
24  protected ThemeExtensionList $themeExtensionList;
25
26  /**
27   * Constructs a new PatternPresetListBuilder object.
28   *
29   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
30   *   The entity type definition.
31   * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage
32   *   The entity storage class.
33   * @param \Drupal\Core\Extension\ThemeExtensionList $theme_extension_list
34   *   The theme extension list.
35   */
36  public function __construct(EntityTypeInterface $entity_type, ConfigEntityStorageInterface $storage, ThemeExtensionList $theme_extension_list) {
37    parent::__construct($entity_type, $storage);
38    $this->themeExtensionList = $theme_extension_list;
39  }
40
41  /**
42   * {@inheritdoc}
43   */
44  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
45    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
46    $storage = $container->get('entity_type.manager')->getStorage($entity_type->id());
47
48    return new self(
49      $entity_type,
50      $storage,
51      $container->get('extension.list.theme'),
52    );
53  }
54
55  /**
56   * {@inheritdoc}
57   */
58  public function getFormId(): string {
59    return 'pattern_preset_list_builder';
60  }
61
62  /**
63   * {@inheritdoc}
64   */
65  public function buildHeader(): array {
66    $header = [];
67    $header['label'] = $this->t('Label');
68    $header['group'] = $this->t('Group');
69    $header['description'] = $this->t('Description');
70    $header['contexts'] = $this->t('Contexts');
71    $header['theme'] = $this->t('Theme');
72
73    return $header + parent::buildHeader();
74  }
75
76  /**
77   * {@inheritdoc}
78   */
79  public function buildRow(EntityInterface $entity): array {
80    /** @var \Drupal\display_builder\PatternPresetInterface $entity */
81    $row = [];
82    $row['label'] = $entity->label();
83
84    $row['group']['data']['#plain_text'] = $entity->getGroup() ?? $this->t('Others');
85    $row['description']['data']['#plain_text'] = $entity->get('description') ?: $entity->getSummary();
86
87    if ($entity->getContexts()) {
88      $row['contexts']['data']['#plain_text'] = self::prettyPrintContexts($entity->getContexts());
89    }
90    else {
91      $row['contexts']['data']['#plain_text'] = '';
92    }
93    $dependencies = $entity->getDependencies();
94
95    if (empty($dependencies['theme'] ?? [])) {
96      $row['themes']['data']['#plain_text'] = $this->t('Any');
97    }
98    else {
99      $row['theme']['data']['#plain_text'] = \implode(', ', $this->getThemeName($dependencies['theme']));
100    }
101
102    return $row + parent::buildRow($entity);
103  }
104
105  /**
106   * {@inheritdoc}
107   */
108  public function buildForm(array $form, FormStateInterface $form_state) {
109    $form = parent::buildForm($form, $form_state);
110    $rows = Element::children($form['entities']);
111
112    if (\count($rows) < 2) {
113      unset($form['actions']['submit']);
114    }
115
116    return $form;
117  }
118
119  /**
120   * {@inheritdoc}
121   */
122  public function render(): array {
123    $build = parent::render();
124    $build['notice'] = [
125      '#markup' => $this->t('Pattern presets are reusable arrangements of components and blocks.'),
126      '#prefix' => '<div class="description">',
127      '#suffix' => '</div>',
128      '#weight' => -100,
129    ];
130
131    return $build;
132  }
133
134  /**
135   * Pretty print contexts.
136   *
137   * @param array<string, \Drupal\Core\Plugin\Context\ContextDefinitionInterface> $contexts
138   *   The contexts to pretty print.
139   *
140   * @return string
141   *   A nicely printed list of contexts.
142   */
143  private static function prettyPrintContexts(array $contexts): string {
144    $list = [];
145
146    foreach ($contexts as $context => $definition) {
147      if ($context === 'context_requirements') {
148        foreach ($definition->getConstraint('RequiredArrayValues') as $constraint) {
149          $constraint = \str_replace('views:', '', $constraint);
150          $list[] = \ucwords($constraint);
151        }
152
153        continue;
154      }
155      $context = \str_replace('ui_patterns_views:view_entity', 'View', $context);
156      $list[] = \ucwords($context);
157    }
158
159    return \implode(', ', $list);
160  }
161
162  /**
163   * Get preset's themes names.
164   *
165   * @param array $themes
166   *   The list of theme names.
167   *
168   * @return array
169   *   A list of themes extension names.
170   */
171  private function getThemeName(array $themes): array {
172    $names = [];
173
174    foreach ($themes as $theme) {
175      $names[] = $this->themeExtensionList->getName($theme);
176    }
177
178    return $names;
179  }
180
181}

Branches

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.