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    $row['group']['data']['#plain_text'] = $entity->getGroup();
84    $row['description']['data']['#plain_text'] = $entity->get('description') ?: $entity->getSummary();
85
86    if ($entity->getContexts()) {
87      $row['contexts']['data']['#plain_text'] = self::prettyPrintContexts($entity->getContexts());
88    }
89    else {
90      $row['contexts']['data']['#plain_text'] = '';
91    }
92    $dependencies = $entity->getDependencies();
93
94    if (empty($dependencies['theme'] ?? [])) {
95      $row['themes']['data']['#plain_text'] = $this->t('Any');
96    }
97    else {
98      $row['theme']['data']['#plain_text'] = \implode(', ', $this->getThemeName($dependencies['theme']));
99    }
100
101    return $row + parent::buildRow($entity);
102  }
103
104  /**
105   * {@inheritdoc}
106   */
107  public function buildForm(array $form, FormStateInterface $form_state) {
108    $form = parent::buildForm($form, $form_state);
109    $rows = Element::children($form['entities']);
110
111    if (\count($rows) < 2) {
112      unset($form['actions']['submit']);
113    }
114
115    return $form;
116  }
117
118  /**
119   * {@inheritdoc}
120   */
121  public function render(): array {
122    $build = parent::render();
123    $build['notice'] = [
124      '#markup' => $this->t('Pattern presets are reusable arrangements of components and blocks.'),
125      '#prefix' => '<div class="description">',
126      '#suffix' => '</div>',
127      '#weight' => -100,
128    ];
129
130    return $build;
131  }
132
133  /**
134   * Pretty print contexts.
135   *
136   * @param array<string, \Drupal\Core\Plugin\Context\ContextDefinitionInterface> $contexts
137   *   The contexts to pretty print.
138   *
139   * @return string
140   *   A nicely printed list of contexts.
141   */
142  private static function prettyPrintContexts(array $contexts): string {
143    $list = [];
144
145    foreach ($contexts as $context => $definition) {
146      if ($context === 'context_requirements') {
147        foreach ($definition->getConstraint('RequiredArrayValues') as $constraint) {
148          $constraint = \str_replace('views:', '', $constraint);
149          $list[] = \ucwords($constraint);
150        }
151
152        continue;
153      }
154      $context = \str_replace('ui_patterns_views:view_entity', 'View', $context);
155      $list[] = \ucwords($context);
156    }
157
158    return \implode(', ', $list);
159  }
160
161  /**
162   * Get preset's themes names.
163   *
164   * @param array $themes
165   *   The list of theme names.
166   *
167   * @return array
168   *   A list of themes extension names.
169   */
170  private function getThemeName(array $themes): array {
171    $names = [];
172
173    foreach ($themes as $theme) {
174      $names[] = $this->themeExtensionList->getName($theme);
175    }
176
177    return $names;
178  }
179
180}

Paths

Below are the source code lines that represent each code path as identified by Xdebug. Please note a path is not necessarily coterminous with a line, a line may contain multiple paths and therefore show up more than once. Please also be aware that some paths may include implicit rather than explicit branches, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.