Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageLayoutListBuilder
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 8
342
0.00% covered (danger)
0.00%
0 / 1
 getFormId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 load
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 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 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getDefaultOperations
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 countActivePageLayouts
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 getConditionsSummary
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_page_layout;
6
7use Drupal\Core\Config\Entity\DraggableListBuilder;
8use Drupal\Core\Entity\EntityInterface;
9use Drupal\Core\StringTranslation\TranslatableMarkup;
10use Drupal\Core\Url;
11
12/**
13 * Provides a listing of page layouts.
14 */
15final class PageLayoutListBuilder extends DraggableListBuilder {
16
17  /**
18   * {@inheritdoc}
19   */
20  public function getFormId(): string {
21    return 'display_builder_page_layout';
22  }
23
24  /**
25   * {@inheritdoc}
26   */
27  public function render(): array {
28    $build = parent::render();
29    $build['notice'] = [
30      '#type' => 'html_tag',
31      '#tag' => 'p',
32      '#value' => $this->t('The first enabled, non-empty, applicable to a page according to its conditions, page layout will be loaded.'),
33      '#weight' => -100,
34    ];
35
36    return $build;
37  }
38
39  /**
40   * {@inheritdoc}
41   */
42  public function load(): array {
43    $entities = parent::load();
44
45    if ($this->countActivePageLayouts($entities) === 0) {
46      $params = [
47        '@url' => Url::fromRoute('block.admin_display')->toString(),
48      ];
49      $message = $this->t('Without any active Page Layout, pages are managed by <a href="@url">Block Layout</a>.', $params);
50      $this->messenger()->addStatus($message);
51    }
52
53    return $entities;
54  }
55
56  /**
57   * {@inheritdoc}
58   */
59  public function buildHeader(): array {
60    $header = [];
61    $header['label'] = $this->t('Page layout');
62    $header['profile_id'] = $this->t('Profile');
63    $header['conditions'] = $this->t('Conditions');
64    $header['status'] = $this->t('Status');
65
66    return $header + parent::buildHeader();
67  }
68
69  /**
70   * {@inheritdoc}
71   */
72  public function buildRow(EntityInterface $entity): array {
73    $row = [];
74    /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */
75    $row['label'] = $entity->label();
76    $row['profile_id']['#plain_text'] = $entity->getProfile()?->label() ?? '?';
77    $row['conditions'] = [
78      '#theme' => 'item_list',
79      '#list_type' => 'ul',
80      '#items' => $this->getConditionsSummary($entity),
81    ];
82    $status = $entity->status() ? $this->t('Enabled') : '❌ ' . $this->t('Disabled');
83    $status = $entity->status() && empty($entity->getSources()) ? '❌ ' . $this->t('Empty') : $status;
84    $row['status']['#plain_text'] = $status;
85
86    $row = $row + parent::buildRow($entity);
87    $row['#attributes']['data-id'] = $entity->id();
88
89    return $row;
90  }
91
92  /**
93   * {@inheritdoc}
94   */
95  public function getDefaultOperations(EntityInterface $entity): array {
96    /** @var \Drupal\display_builder_page_layout\PageLayoutInterface @page_layout */
97    $page_layout = $entity;
98    $operations = parent::getDefaultOperations($entity);
99    $operations[] = [
100      'title' => new TranslatableMarkup('Build display'),
101      'weight' => '-100',
102      'url' => $page_layout->getBuilderUrl(),
103    ];
104
105    return $operations;
106  }
107
108  /**
109   * Count enabled and non empty Page Layout entities.
110   *
111   * @param array<int,mixed> $entities
112   *   The entities to process.
113   *
114   * @return int
115   *   The number of enabled and non empty Page Layout entities.
116   */
117  private function countActivePageLayouts(array $entities): int {
118    $count = 0;
119
120    foreach ($entities as $entity) {
121      if ($entity->status === TRUE && !empty($entity->getSources())) {
122        ++$count;
123      }
124    }
125
126    return $count;
127  }
128
129  /**
130   * Get summary of configured conditions, one item per plugin.
131   *
132   * @param PageLayoutInterface $entity
133   *   The page layout entity.
134   *
135   * @return array
136   *   The summary of configured conditions.
137   */
138  private function getConditionsSummary(PageLayoutInterface $entity): array {
139    $summary = [];
140    $conditions = $entity->getConditions();
141
142    foreach ($conditions->getIterator() as $condition) {
143      if ($condition->getPluginId() === 'request_path') {
144        $pages = \array_map('trim', \explode("\n", $condition->getConfiguration()['pages']));
145        $pages = \implode(', ', $pages);
146
147        if (!$condition->isNegated()) {
148          $summary[] = $this->t('On the following pages: @pages', ['@pages' => $pages]);
149        }
150        else {
151          $summary[] = $this->t('Not on the following pages: @pages', ['@pages' => $pages]);
152        }
153
154        continue;
155      }
156      $summary[] = $condition->summary();
157    }
158
159    return $summary;
160  }
161
162}