Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
21.15% |
11 / 52 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
ConfigFormBuilder | |
21.15% |
11 / 52 |
|
33.33% |
1 / 3 |
82.58 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
build | |
6.98% |
3 / 43 |
|
0.00% |
0 / 1 |
59.52 | |||
getAllowedProfiles | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder; |
6 | |
7 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
8 | use Drupal\Core\Extension\ModuleHandlerInterface; |
9 | use Drupal\Core\Session\AccountProxyInterface; |
10 | use Drupal\Core\StringTranslation\StringTranslationTrait; |
11 | use Drupal\Core\Url; |
12 | |
13 | /** |
14 | * Config form builder. |
15 | */ |
16 | class ConfigFormBuilder implements ConfigFormBuilderInterface { |
17 | |
18 | use StringTranslationTrait; |
19 | |
20 | public function __construct( |
21 | protected EntityTypeManagerInterface $entityTypeManager, |
22 | protected AccountProxyInterface $currentUser, |
23 | protected readonly ModuleHandlerInterface $moduleHandler, |
24 | ) {} |
25 | |
26 | /** |
27 | * {@inheritdoc} |
28 | */ |
29 | public function build(DisplayBuildableInterface $entity, bool $mandatory = TRUE): array { |
30 | $options = $this->getAllowedProfiles(); |
31 | |
32 | if (empty($options)) { |
33 | return []; |
34 | } |
35 | |
36 | $form = []; |
37 | |
38 | $options = $mandatory ? $options : ['' => $this->t('- Disabled -')] + $options; |
39 | $form[ConfigFormBuilderInterface::PROFILE_PROPERTY] = [ |
40 | '#type' => 'select', |
41 | '#title' => $this->t('Profile'), |
42 | '#description' => $this->t('The profile defines the features available in the builder. It can be changed anytime.'), |
43 | '#options' => $options, |
44 | ]; |
45 | |
46 | // Add admin information to link the profiles. |
47 | if ($this->moduleHandler->moduleExists('display_builder_ui') && $this->currentUser->hasPermission('administer display builder profile')) { |
48 | $form[ConfigFormBuilderInterface::PROFILE_PROPERTY]['#description'] = [ |
49 | [ |
50 | '#markup' => $form[ConfigFormBuilderInterface::PROFILE_PROPERTY]['#description'] . '<br>', |
51 | ], |
52 | [ |
53 | '#type' => 'link', |
54 | '#title' => $this->t('Add and configure display builder profiles'), |
55 | '#url' => Url::fromRoute('entity.display_builder_profile.collection'), |
56 | '#suffix' => '.', |
57 | ], |
58 | ]; |
59 | } |
60 | |
61 | // Add the builder link to edit. |
62 | $instance_id = $entity->getInstanceId(); |
63 | |
64 | if ($instance_id && $entity->getProfile()) { |
65 | $form['link'] = [ |
66 | '#type' => 'html_tag', |
67 | '#tag' => 'p', |
68 | '#attributes' => [ |
69 | 'class' => ['form-item__description'], |
70 | ], |
71 | 'content' => [ |
72 | '#type' => 'link', |
73 | '#title' => $this->t('Build the display'), |
74 | '#url' => $entity->getBuilderUrl(), |
75 | '#attributes' => [ |
76 | 'class' => ['button', 'button--small'], |
77 | ], |
78 | ], |
79 | ]; |
80 | } |
81 | |
82 | if ($entity->getProfile()?->id()) { |
83 | $form[ConfigFormBuilderInterface::PROFILE_PROPERTY]['#default_value'] = (string) $entity->getProfile()->id(); |
84 | } |
85 | |
86 | return $form; |
87 | } |
88 | |
89 | /** |
90 | * {@inheritdoc} |
91 | */ |
92 | public function getAllowedProfiles(): array { |
93 | $options = []; |
94 | $storage = $this->entityTypeManager->getStorage('display_builder_profile'); |
95 | $entity_ids = $storage->getQuery()->accessCheck(TRUE)->sort('weight', 'ASC')->execute(); |
96 | /** @var \Drupal\display_builder\ProfileInterface[] $display_builders */ |
97 | $display_builders = $storage->loadMultiple($entity_ids); |
98 | |
99 | foreach ($display_builders as $entity_id => $entity) { |
100 | if ($this->currentUser->hasPermission($entity->getPermissionName())) { |
101 | $options[$entity_id] = $entity->label(); |
102 | } |
103 | } |
104 | |
105 | return $options; |
106 | } |
107 | |
108 | } |