Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.10% covered (success)
93.10%
27 / 29
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Profile
93.10% covered (success)
93.10%
27 / 29
77.78% covered (warning)
77.78%
7 / 9
17.09
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
1
 getIslandConfigurations
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%
1 / 1
2
 getEnabledIslands
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 getPermissionName
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
0.00% covered (danger)
0.00%
0 / 1
2.03
 toUrl
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
 getLibrary
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDebugModeActivated
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\ProfileInterface;
15use Drupal\display_builder\ProfileViewBuilder;
16use Drupal\display_builder_ui\ProfileListBuilder;
17use Drupal\user\Entity\Role;
18use Drupal\user\RoleInterface;
19
20/**
21 * Defines the display builder entity type.
22 */
23#[ConfigEntityType(
24  id: 'display_builder_profile',
25  label: new TranslatableMarkup('Display builder profile'),
26  label_collection: new TranslatableMarkup('Display builder profiles'),
27  label_singular: new TranslatableMarkup('display builder profile'),
28  label_plural: new TranslatableMarkup('display builders profiles'),
29  entity_keys: [
30    'id' => 'id',
31    'label' => 'label',
32    'description' => 'description',
33    'weight' => 'weight',
34  ],
35  handlers: [
36    'route_provider' => [
37      'html' => 'Drupal\display_builder\Routing\ProfileRouteProvider',
38    ],
39    'view_builder' => ProfileViewBuilder::class,
40    'list_builder' => ProfileListBuilder::class,
41    'form' => [
42      'add' => ProfileForm::class,
43      'edit' => ProfileForm::class,
44      'delete' => EntityDeleteForm::class,
45      'edit-plugin' => ProfileIslandPluginForm::class,
46    ],
47  ],
48  links: [
49    'add-form' => '/admin/structure/display-builder/add',
50    'edit-form' => '/admin/structure/display-builder/{display_builder_profile}',
51    'edit-plugin-form' => '/admin/structure/display-builder/{display_builder_profile}/edit/{island_id}',
52    'delete-form' => '/admin/structure/display-builder/{display_builder_profile}/delete',
53    'collection' => '/admin/structure/display-builder',
54  ],
55  admin_permission: 'administer display builder profile',
56  constraints: [
57    'ImmutableProperties' => [
58      'id',
59    ],
60  ],
61  // Example: display_builder.profile.default.yml.
62  config_prefix: 'profile',
63  config_export: [
64    'id',
65    'label',
66    'library',
67    'description',
68    'islands',
69    'debug',
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 library mode.
92   */
93  protected string $library = 'cdn';
94
95  /**
96   * The display builder debug mode.
97   */
98  protected bool $debug = FALSE;
99
100  /**
101   * The islands configuration for storage.
102   */
103  protected ?array $islands = [];
104
105  /**
106   * Weight of this page layout when negotiating the page variant.
107   *
108   * The first/lowest that is accessible according to conditions is loaded.
109   *
110   * @var int
111   */
112  protected $weight = 0;
113
114  /**
115   * {@inheritdoc}
116   */
117  public function getIslandConfiguration(string $island_id): array {
118    return $this->islands[$island_id] ?? [];
119  }
120
121  /**
122   * {@inheritdoc}
123   */
124  public function getIslandConfigurations(): array {
125    return $this->islands ?? [];
126  }
127
128  /**
129   * {@inheritdoc}
130   */
131  public function setIslandConfiguration(string $island_id, array $configuration = []): void {
132    // When $configuration is updated from ProfileIslandPluginForm,
133    // 'weight', 'enable' and 'region' properties are missing but they must not
134    // be reset.
135    $configuration['weight'] = $configuration['weight'] ?? $this->islands[$island_id]['weight'] ?? 0;
136    $configuration['enable'] = $configuration['enable'] ?? $this->islands[$island_id]['enable'] ?? FALSE;
137
138    // Only View islands have regions.
139    if (isset($this->islands[$island_id]['region'])) {
140      $configuration['region'] = $configuration['region'] ?? $this->islands[$island_id]['region'];
141    }
142
143    $this->islands[$island_id] = $configuration;
144  }
145
146  /**
147   * {@inheritdoc}
148   */
149  public function getEnabledIslands(): array {
150    $island_enabled = [];
151
152    foreach ($this->islands as $island_id => $configuration) {
153      if (isset($configuration['enable']) && (bool) $configuration['enable']) {
154        $island_enabled[$island_id] = $configuration['weight'] ?? 0;
155      }
156    }
157
158    return $island_enabled;
159  }
160
161  /**
162   * {@inheritdoc}
163   */
164  public function getPermissionName(): string {
165    return 'use display builder ' . $this->id();
166  }
167
168  /**
169   * {@inheritdoc}
170   */
171  public function getRoles(): array {
172    // Do not list any roles if the permission does not exist.
173    $permission = $this->getPermissionName();
174
175    if (empty($permission)) {
176      return [];
177    }
178
179    $roles = \array_filter(Role::loadMultiple(), static fn (RoleInterface $role) => $role->hasPermission($permission));
180
181    return \array_map(static fn (RoleInterface $role) => $role->label(), $roles);
182  }
183
184  /**
185   * {@inheritdoc}
186   */
187  public function toUrl($rel = NULL, array $options = []): Url {
188    if ($rel === 'edit-plugin-form' && $this->id() && isset($options['island_id'])) {
189      $island_id = $options['island_id'];
190      unset($options['island_id']);
191
192      return Url::fromRoute(
193        'entity.display_builder_profile.edit_plugin_form',
194        ['display_builder_profile' => $this->id(), 'island_id' => $island_id],
195        $options
196      );
197    }
198
199    return parent::toUrl($rel, $options);
200  }
201
202  /**
203   * {@inheritdoc}
204   */
205  public function getLibrary(): string {
206    return $this->library;
207  }
208
209  /**
210   * {@inheritdoc}
211   */
212  public function isDebugModeActivated(): bool {
213    return $this->debug;
214  }
215
216}