Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
88.00% covered (warning)
88.00%
22 / 25
36.36% covered (danger)
36.36%
8 / 22
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Profile
89.29% covered (warning)
89.29%
25 / 28
88.00% covered (warning)
88.00%
22 / 25
36.36% covered (danger)
36.36%
8 / 22
62.50% covered (warning)
62.50%
5 / 8
81.97
0.00% covered (danger)
0.00%
0 / 1
 getIslandConfiguration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIslandConfigurations
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIslandConfiguration
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
 getEnabledIslands
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
8 / 8
33.33% covered (danger)
33.33%
2 / 6
100.00% covered (success)
100.00%
1 / 1
8.74
 getPermissionName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRoles
80.00% covered (warning)
80.00%
4 / 5
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 toUrl
88.89% covered (warning)
88.89%
8 / 9
85.71% covered (warning)
85.71%
6 / 7
12.50% covered (danger)
12.50%
1 / 8
0.00% covered (danger)
0.00%
0 / 1
14.72
 isDebugModeActivated
0.00% covered (danger)
0.00%
0 / 1
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;
6
7use Drupal\Core\Config\Entity\ConfigEntityBase;
8use Drupal\Core\Entity\Attribute\ConfigEntityType;
9use Drupal\Core\Entity\EntityDeleteForm;
10use Drupal\Core\StringTranslation\TranslatableMarkup;
11use Drupal\Core\Url;
12use Drupal\display_builder\Form\ProfileForm;
13use Drupal\display_builder\Form\ProfileIslandPluginForm;
14use Drupal\display_builder\ProfileAccessControlHandler;
15use Drupal\display_builder\ProfileInterface;
16use Drupal\display_builder\ProfileViewBuilder;
17use Drupal\display_builder_ui\ProfileListBuilder;
18use Drupal\user\Entity\Role;
19use Drupal\user\RoleInterface;
20
21/**
22 * Defines the display builder entity type.
23 */
24#[ConfigEntityType(
25  id: 'display_builder_profile',
26  label: new TranslatableMarkup('Display builder profile'),
27  label_collection: new TranslatableMarkup('Display builder profiles'),
28  label_singular: new TranslatableMarkup('display builder profile'),
29  label_plural: new TranslatableMarkup('display builders profiles'),
30  entity_keys: [
31    'id' => 'id',
32    'label' => 'label',
33    'description' => 'description',
34    'weight' => 'weight',
35  ],
36  handlers: [
37    'access' => ProfileAccessControlHandler::class,
38    'route_provider' => [
39      'html' => 'Drupal\display_builder\Routing\ProfileRouteProvider',
40    ],
41    'view_builder' => ProfileViewBuilder::class,
42    'list_builder' => ProfileListBuilder::class,
43    'form' => [
44      'add' => ProfileForm::class,
45      'edit' => ProfileForm::class,
46      'delete' => EntityDeleteForm::class,
47      'edit-plugin' => ProfileIslandPluginForm::class,
48    ],
49  ],
50  links: [
51    'add-form' => '/admin/structure/display-builder/add',
52    'edit-form' => '/admin/structure/display-builder/{display_builder_profile}',
53    'edit-plugin-form' => '/admin/structure/display-builder/{display_builder_profile}/edit/{island_id}',
54    'delete-form' => '/admin/structure/display-builder/{display_builder_profile}/delete',
55    'collection' => '/admin/structure/display-builder',
56  ],
57  admin_permission: 'administer display builder profile',
58  constraints: [
59    'ImmutableProperties' => [
60      'id',
61    ],
62  ],
63  // Example: display_builder.profile.default.yml.
64  config_prefix: 'profile',
65  config_export: [
66    'id',
67    'label',
68    'description',
69    'islands',
70    'weight',
71  ],
72)]
73final class Profile extends ConfigEntityBase implements ProfileInterface {
74
75  /**
76   * The display builder config ID.
77   */
78  protected string $id;
79
80  /**
81   * The display builder label.
82   */
83  protected string $label;
84
85  /**
86   * The display builder description.
87   */
88  protected string $description;
89
90  /**
91   * The display builder debug mode.
92   */
93  protected bool $debug = FALSE;
94
95  /**
96   * The islands configuration for storage.
97   */
98  protected ?array $islands = [];
99
100  /**
101   * Weight to order the entity in lists.
102   *
103   * @var int
104   */
105  protected $weight = 0;
106
107  /**
108   * {@inheritdoc}
109   */
110  public function getIslandConfiguration(string $island_id): array {
111    return $this->islands[$island_id] ?? [];
112  }
113
114  /**
115   * {@inheritdoc}
116   */
117  public function getIslandConfigurations(): array {
118    return $this->islands ?? [];
119  }
120
121  /**
122   * {@inheritdoc}
123   */
124  public function setIslandConfiguration(string $island_id, array $configuration = []): void {
125    // When $configuration is updated from ProfileIslandPluginForm,
126    // 'weight', 'status' and 'region' properties are missing but they must not
127    // be reset.
128    $configuration['weight'] ??= $this->islands[$island_id]['weight'] ?? 0;
129    $configuration['status'] ??= $this->islands[$island_id]['status'] ?? FALSE;
130
131    // Only View islands have regions.
132    if (isset($this->islands[$island_id]['region'])) {
133      $configuration['region'] ??= $this->islands[$island_id]['region'];
134    }
135
136    $this->islands[$island_id] = $configuration;
137  }
138
139  /**
140   * {@inheritdoc}
141   */
142  public function getEnabledIslands(): array {
143    $island_enabled = [];
144
145    foreach ($this->islands as $island_id => $configuration) {
146      if (isset($configuration['status']) && $configuration['status']) {
147        $island_enabled[$island_id] = $configuration['weight'] ?? 0;
148      }
149    }
150
151    return $island_enabled;
152  }
153
154  /**
155   * {@inheritdoc}
156   */
157  public function getPermissionName(): string {
158    return 'use display builder ' . $this->id();
159  }
160
161  /**
162   * {@inheritdoc}
163   */
164  public function getRoles(): array {
165    // Do not list any roles if the permission does not exist.
166    $permission = $this->getPermissionName();
167
168    if (empty($permission)) {
169      return [];
170    }
171
172    $roles = \array_filter(Role::loadMultiple(), static fn (RoleInterface $role) => $role->hasPermission($permission));
173
174    return \array_map(static fn (RoleInterface $role) => $role->label(), $roles);
175  }
176
177  /**
178   * {@inheritdoc}
179   */
180  public function toUrl($rel = NULL, array $options = []): Url {
181    if ($rel === 'edit-plugin-form' && $this->id() && isset($options['island_id'])) {
182      $island_id = $options['island_id'];
183      unset($options['island_id']);
184
185      return Url::fromRoute(
186        'entity.display_builder_profile.edit_plugin_form',
187        ['display_builder_profile' => $this->id(), 'island_id' => $island_id],
188        $options
189      );
190    }
191
192    return parent::toUrl($rel, $options);
193  }
194
195  /**
196   * {@inheritdoc}
197   */
198  public function isDebugModeActivated(): bool {
199    return $this->debug;
200  }
201
202}