Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 84 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
PageLayoutForm | |
0.00% |
0 / 84 |
|
0.00% |
0 / 7 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
form | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
2 | |||
save | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
submitForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
buildConditionsForm | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
72 | |||
alterConditionsForm | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
submitConditions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder_page_layout\Form; |
6 | |
7 | use Drupal\Core\Condition\ConditionManager; |
8 | use Drupal\Core\DependencyInjection\AutowireTrait; |
9 | use Drupal\Core\Entity\EntityForm; |
10 | use Drupal\Core\Form\FormStateInterface; |
11 | use Drupal\Core\Form\SubformState; |
12 | use Drupal\Core\Language\LanguageManagerInterface; |
13 | use Drupal\display_builder\ConfigFormBuilderInterface; |
14 | use Drupal\display_builder_page_layout\Entity\PageLayout; |
15 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
16 | |
17 | /** |
18 | * Page layout form. |
19 | */ |
20 | final class PageLayoutForm extends EntityForm { |
21 | |
22 | use AutowireTrait; |
23 | |
24 | public function __construct( |
25 | private readonly ConfigFormBuilderInterface $configFormBuilder, |
26 | #[Autowire(service: 'plugin.manager.condition')] |
27 | private readonly ConditionManager $conditionManager, |
28 | private readonly LanguageManagerInterface $languageManager, |
29 | ) {} |
30 | |
31 | /** |
32 | * {@inheritdoc} |
33 | */ |
34 | public function form(array $form, FormStateInterface $form_state): array { |
35 | $form = parent::form($form, $form_state); |
36 | // Because of $form['conditions']. |
37 | $form['#tree'] = TRUE; |
38 | |
39 | $form['label'] = [ |
40 | '#type' => 'textfield', |
41 | '#title' => $this->t('Label'), |
42 | '#maxlength' => 255, |
43 | '#default_value' => $this->entity->label(), |
44 | '#required' => TRUE, |
45 | ]; |
46 | |
47 | $form['id'] = [ |
48 | '#type' => 'machine_name', |
49 | '#default_value' => $this->entity->id(), |
50 | '#machine_name' => [ |
51 | 'exists' => [PageLayout::class, 'load'], |
52 | ], |
53 | '#disabled' => !$this->entity->isNew(), |
54 | ]; |
55 | |
56 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
57 | $entity = $this->entity; |
58 | $form = \array_merge($form, $this->configFormBuilder->build($entity)); |
59 | |
60 | $form['conditions'] = $this->buildConditionsForm([], $form_state); |
61 | $form['status'] = [ |
62 | '#type' => 'checkbox', |
63 | '#title' => $this->t('Enabled'), |
64 | '#default_value' => $entity->status(), |
65 | ]; |
66 | |
67 | return $form; |
68 | } |
69 | |
70 | /** |
71 | * {@inheritdoc} |
72 | */ |
73 | public function save(array $form, FormStateInterface $form_state): int { |
74 | $result = parent::save($form, $form_state); |
75 | $message_args = ['%label' => $this->entity->label()]; |
76 | $this->messenger()->addStatus( |
77 | match ($result) { |
78 | SAVED_NEW => $this->t('Created new page layout %label.', $message_args), |
79 | default => $this->t('Updated page layout %label.', $message_args), |
80 | } |
81 | ); |
82 | $form_state->setRedirectUrl($this->entity->toUrl('collection')); |
83 | |
84 | return $result; |
85 | } |
86 | |
87 | /** |
88 | * {@inheritdoc} |
89 | */ |
90 | public function submitForm(array &$form, FormStateInterface $form_state): void { |
91 | parent::submitForm($form, $form_state); |
92 | $this->submitConditions($form, $form_state); |
93 | } |
94 | |
95 | /** |
96 | * Helper function for building the conditions UI form. |
97 | * |
98 | * @param array $form |
99 | * An associative array containing the structure of the form. |
100 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
101 | * The current state of the form. |
102 | * |
103 | * @return array |
104 | * The form array with the conditions UI added in. |
105 | */ |
106 | private function buildConditionsForm(array $form, FormStateInterface $form_state) { |
107 | $form['visibility_tabs'] = [ |
108 | '#type' => 'vertical_tabs', |
109 | '#title' => $this->t('Conditions'), |
110 | '#parents' => ['visibility_tabs'], |
111 | ]; |
112 | |
113 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
114 | $entity = $this->entity; |
115 | // @todo \PluginNotFoundException: |
116 | $conditions = $entity->getConditions()->getConfiguration(); |
117 | $definitions = $this->conditionManager->getDefinitions(); |
118 | |
119 | foreach ($definitions as $condition_id => $definition) { |
120 | // Don't display the current theme condition. |
121 | if ($condition_id === 'current_theme') { |
122 | continue; |
123 | } |
124 | |
125 | // Don't display the language condition until we have multiple languages. |
126 | if ($condition_id === 'language' && !$this->languageManager->isMultilingual()) { |
127 | continue; |
128 | } |
129 | |
130 | if (\str_starts_with($condition_id, 'entity_bundle:')) { |
131 | $entity_type_id = \str_replace('entity_bundle:', '', $condition_id); |
132 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
133 | $url = $entity_type->getLinkTemplate('canonical'); |
134 | |
135 | if (!$url || \str_starts_with((string) $url, '/admin')) { |
136 | continue; |
137 | } |
138 | } |
139 | |
140 | /** @var \Drupal\Core\Condition\ConditionInterface $condition */ |
141 | $condition = $this->conditionManager->createInstance($condition_id, $conditions[$condition_id] ?? []); |
142 | $form_state->set(['conditions', $condition_id], $condition); |
143 | $condition_form = $condition->buildConfigurationForm([], $form_state); |
144 | $condition_form['#type'] = 'details'; |
145 | $condition_form['#title'] = $definition['label']; |
146 | $condition_form['#group'] = 'visibility_tabs'; |
147 | $form[$condition_id] = $condition_form; |
148 | } |
149 | |
150 | return $this->alterConditionsForm($form); |
151 | } |
152 | |
153 | /** |
154 | * Alter conditions form. |
155 | * |
156 | * @param array $form |
157 | * An associative array containing the structure of the form. |
158 | */ |
159 | private function alterConditionsForm(array $form): array { |
160 | if (isset($form['user_role'])) { |
161 | $form['user_role']['#title'] = $this->t('User roles'); |
162 | } |
163 | |
164 | if (isset($form['request_path'])) { |
165 | $form['request_path']['#title'] = $this->t('Pages'); |
166 | $form['request_path']['negate']['#type'] = 'radios'; |
167 | $form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value']; |
168 | $form['request_path']['negate']['#title_display'] = 'invisible'; |
169 | $form['request_path']['negate']['#options'] = [ |
170 | $this->t('Activate on the listed pages'), |
171 | $this->t('Skip for the listed pages'), |
172 | ]; |
173 | } |
174 | |
175 | return $form; |
176 | } |
177 | |
178 | /** |
179 | * Helper function to independently submit the conditions UI. |
180 | * |
181 | * @param array $form |
182 | * A nested array form elements comprising the form. |
183 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
184 | * The current state of the form. |
185 | */ |
186 | private function submitConditions(array $form, FormStateInterface $form_state): void { |
187 | foreach (\array_keys($form_state->getValue('conditions')) as $condition_id) { |
188 | // Allow the condition to submit the form. |
189 | $condition = $form_state->get(['conditions', $condition_id]); |
190 | $condition->submitConfigurationForm($form['conditions'][$condition_id], SubformState::createForSubform($form['conditions'][$condition_id], $form, $form_state)); |
191 | |
192 | $condition_configuration = $condition->getConfiguration(); |
193 | // Update the visibility conditions on the block. |
194 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
195 | $entity = $this->entity; |
196 | $entity->getConditions()->addInstanceId((string) $condition_id, $condition_configuration); |
197 | } |
198 | } |
199 | |
200 | } |