Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
26 / 28
90.48% covered (success)
90.48%
19 / 21
52.94% covered (warning)
52.94%
9 / 17
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Profile
92.86% covered (success)
92.86%
26 / 28
90.48% covered (success)
90.48%
19 / 21
52.94% covered (warning)
52.94%
9 / 17
75.00% covered (warning)
75.00%
6 / 8
42.68
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%
6 / 6
40.00% covered (danger)
40.00%
2 / 5
100.00% covered (success)
100.00%
1 / 1
7.46
 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
80.00% covered (warning)
80.00%
4 / 5
25.00% covered (danger)
25.00%
1 / 4
0.00% covered (danger)
0.00%
0 / 1
10.75
 isDebugModeActivated
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
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    'debug',
71    'weight',
72  ],
73)]
74final class Profile extends ConfigEntityBase implements ProfileInterface {
75
76  /**
77   * The display builder config ID.
78   */
79  protected string $id;
80
81  /**
82   * The display builder label.
83   */
84  protected string $label;
85
86  /**
87   * The display builder description.
88   */
89  protected string $description;
90
91  /**
92   * The display builder debug mode.
93   */
94  protected bool $debug = FALSE;
95
96  /**
97   * The islands configuration for storage.
98   */
99  protected ?array $islands = [];
100
101  /**
102   * Weight to order the entity in lists.
103   *
104   * @var int
105   */
106  protected $weight = 0;
107
108  /**
109   * {@inheritdoc}
110   */
111  public function getIslandConfiguration(string $island_id): array {
112    return $this->islands[$island_id] ?? [];
113  }
114
115  /**
116   * {@inheritdoc}
117   */
118  public function getIslandConfigurations(): array {
119    return $this->islands ?? [];
120  }
121
122  /**
123   * {@inheritdoc}
124   */
125  public function setIslandConfiguration(string $island_id, array $configuration = []): void {
126    // When $configuration is updated from ProfileIslandPluginForm,
127    // 'weight', 'status' and 'region' properties are missing but they must not
128    // be reset.
129    $configuration['weight'] = $configuration['weight'] ?? $this->islands[$island_id]['weight'] ?? 0;
130    $configuration['status'] = $configuration['status'] ?? $this->islands[$island_id]['status'] ?? FALSE;
131
132    // Only View islands have regions.
133    if (isset($this->islands[$island_id]['region'])) {
134      $configuration['region'] = $configuration['region'] ?? $this->islands[$island_id]['region'];
135    }
136
137    $this->islands[$island_id] = $configuration;
138  }
139
140  /**
141   * {@inheritdoc}
142   */
143  public function getEnabledIslands(): array {
144    $island_enabled = [];
145
146    foreach ($this->islands as $island_id => $configuration) {
147      if (isset($configuration['status']) && $configuration['status']) {
148        $island_enabled[$island_id] = $configuration['weight'] ?? 0;
149      }
150    }
151
152    return $island_enabled;
153  }
154
155  /**
156   * {@inheritdoc}
157   */
158  public function getPermissionName(): string {
159    return 'use display builder ' . $this->id();
160  }
161
162  /**
163   * {@inheritdoc}
164   */
165  public function getRoles(): array {
166    // Do not list any roles if the permission does not exist.
167    $permission = $this->getPermissionName();
168
169    if (empty($permission)) {
170      return [];
171    }
172
173    $roles = \array_filter(Role::loadMultiple(), static fn (RoleInterface $role) => $role->hasPermission($permission));
174
175    return \array_map(static fn (RoleInterface $role) => $role->label(), $roles);
176  }
177
178  /**
179   * {@inheritdoc}
180   */
181  public function toUrl($rel = NULL, array $options = []): Url {
182    if ($rel === 'edit-plugin-form' && $this->id() && isset($options['island_id'])) {
183      $island_id = $options['island_id'];
184      unset($options['island_id']);
185
186      return Url::fromRoute(
187        'entity.display_builder_profile.edit_plugin_form',
188        ['display_builder_profile' => $this->id(), 'island_id' => $island_id],
189        $options
190      );
191    }
192
193    return parent::toUrl($rel, $options);
194  }
195
196  /**
197   * {@inheritdoc}
198   */
199  public function isDebugModeActivated(): bool {
200    return $this->debug;
201  }
202
203}