Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 89 |
|
0.00% |
0 / 32 |
|
0.00% |
0 / 26 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| PageLayoutForm | |
0.00% |
0 / 89 |
|
0.00% |
0 / 32 |
|
0.00% |
0 / 26 |
|
0.00% |
0 / 8 |
506 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
n/a |
0 / 0 |
n/a |
0 / 0 |
|
0.00% |
0 / 1 |
2 | |||||
| form | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| save | |
0.00% |
0 / 10 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| submitForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| actionsElement | |
0.00% |
0 / 5 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| buildConditionsForm | |
0.00% |
0 / 27 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
72 | |||
| alterConditionsForm | |
0.00% |
0 / 12 |
|
0.00% |
0 / 5 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| submitConditions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 3 |
|
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 | * {@inheritdoc} |
| 97 | */ |
| 98 | protected function actionsElement(array $form, FormStateInterface $form_state): array { |
| 99 | $form = parent::actionsElement($form, $form_state); |
| 100 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $page_layout */ |
| 101 | $page_layout = $this->entity; |
| 102 | |
| 103 | if ($page_layout->isNew() && !$this->configFormBuilder->isAllowed($page_layout)) { |
| 104 | $form['submit']['#disabled'] = TRUE; |
| 105 | } |
| 106 | |
| 107 | return $form; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Helper function for building the conditions UI form. |
| 112 | * |
| 113 | * @param array $form |
| 114 | * An associative array containing the structure of the form. |
| 115 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 116 | * The current state of the form. |
| 117 | * |
| 118 | * @return array |
| 119 | * The form array with the conditions UI added in. |
| 120 | */ |
| 121 | private function buildConditionsForm(array $form, FormStateInterface $form_state) { |
| 122 | $form['visibility_tabs'] = [ |
| 123 | '#type' => 'vertical_tabs', |
| 124 | '#title' => $this->t('Conditions'), |
| 125 | '#parents' => ['visibility_tabs'], |
| 126 | ]; |
| 127 | |
| 128 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
| 129 | $entity = $this->entity; |
| 130 | // @todo \PluginNotFoundException: |
| 131 | $conditions = $entity->getConditions()->getConfiguration(); |
| 132 | $definitions = $this->conditionManager->getDefinitions(); |
| 133 | |
| 134 | foreach ($definitions as $condition_id => $definition) { |
| 135 | // Don't display the current theme condition. |
| 136 | if ($condition_id === 'current_theme') { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // Don't display the language condition until we have multiple languages. |
| 141 | if ($condition_id === 'language' && !$this->languageManager->isMultilingual()) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (\str_starts_with($condition_id, 'entity_bundle:')) { |
| 146 | $entity_type_id = \str_replace('entity_bundle:', '', $condition_id); |
| 147 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 148 | $url = $entity_type->getLinkTemplate('canonical'); |
| 149 | |
| 150 | if (!$url || \str_starts_with((string) $url, '/admin')) { |
| 151 | continue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** @var \Drupal\Core\Condition\ConditionInterface $condition */ |
| 156 | $condition = $this->conditionManager->createInstance($condition_id, $conditions[$condition_id] ?? []); |
| 157 | $form_state->set(['conditions', $condition_id], $condition); |
| 158 | $condition_form = $condition->buildConfigurationForm([], $form_state); |
| 159 | $condition_form['#type'] = 'details'; |
| 160 | $condition_form['#title'] = $definition['label']; |
| 161 | $condition_form['#group'] = 'visibility_tabs'; |
| 162 | $form[$condition_id] = $condition_form; |
| 163 | } |
| 164 | |
| 165 | return $this->alterConditionsForm($form); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Alter conditions form. |
| 170 | * |
| 171 | * @param array $form |
| 172 | * An associative array containing the structure of the form. |
| 173 | */ |
| 174 | private function alterConditionsForm(array $form): array { |
| 175 | if (isset($form['user_role'])) { |
| 176 | $form['user_role']['#title'] = $this->t('User roles'); |
| 177 | } |
| 178 | |
| 179 | if (isset($form['request_path'])) { |
| 180 | $form['request_path']['#title'] = $this->t('Pages'); |
| 181 | $form['request_path']['negate']['#type'] = 'radios'; |
| 182 | $form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value']; |
| 183 | $form['request_path']['negate']['#title_display'] = 'invisible'; |
| 184 | $form['request_path']['negate']['#options'] = [ |
| 185 | $this->t('Activate on the listed pages'), |
| 186 | $this->t('Skip for the listed pages'), |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | return $form; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Helper function to independently submit the conditions UI. |
| 195 | * |
| 196 | * @param array $form |
| 197 | * A nested array form elements comprising the form. |
| 198 | * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 199 | * The current state of the form. |
| 200 | */ |
| 201 | private function submitConditions(array $form, FormStateInterface $form_state): void { |
| 202 | foreach (\array_keys($form_state->getValue('conditions')) as $condition_id) { |
| 203 | // Allow the condition to submit the form. |
| 204 | $condition = $form_state->get(['conditions', $condition_id]); |
| 205 | $condition->submitConfigurationForm($form['conditions'][$condition_id], SubformState::createForSubform($form['conditions'][$condition_id], $form, $form_state)); |
| 206 | |
| 207 | $condition_configuration = $condition->getConfiguration(); |
| 208 | // Update the visibility conditions on the block. |
| 209 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
| 210 | $entity = $this->entity; |
| 211 | $entity->getConditions()->addInstanceId((string) $condition_id, $condition_configuration); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 98 | protected function actionsElement(array $form, FormStateInterface $form_state): array { |
| 99 | $form = parent::actionsElement($form, $form_state); |
| 100 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $page_layout */ |
| 101 | $page_layout = $this->entity; |
| 102 | |
| 103 | if ($page_layout->isNew() && !$this->configFormBuilder->isAllowed($page_layout)) { |
| 103 | if ($page_layout->isNew() && !$this->configFormBuilder->isAllowed($page_layout)) { |
| 104 | $form['submit']['#disabled'] = TRUE; |
| 105 | } |
| 106 | |
| 107 | return $form; |
| 107 | return $form; |
| 174 | private function alterConditionsForm(array $form): array { |
| 175 | if (isset($form['user_role'])) { |
| 176 | $form['user_role']['#title'] = $this->t('User roles'); |
| 177 | } |
| 178 | |
| 179 | if (isset($form['request_path'])) { |
| 179 | if (isset($form['request_path'])) { |
| 180 | $form['request_path']['#title'] = $this->t('Pages'); |
| 181 | $form['request_path']['negate']['#type'] = 'radios'; |
| 182 | $form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value']; |
| 183 | $form['request_path']['negate']['#title_display'] = 'invisible'; |
| 184 | $form['request_path']['negate']['#options'] = [ |
| 185 | $this->t('Activate on the listed pages'), |
| 186 | $this->t('Skip for the listed pages'), |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | return $form; |
| 190 | return $form; |
| 121 | private function buildConditionsForm(array $form, FormStateInterface $form_state) { |
| 122 | $form['visibility_tabs'] = [ |
| 123 | '#type' => 'vertical_tabs', |
| 124 | '#title' => $this->t('Conditions'), |
| 125 | '#parents' => ['visibility_tabs'], |
| 126 | ]; |
| 127 | |
| 128 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
| 129 | $entity = $this->entity; |
| 130 | // @todo \PluginNotFoundException: |
| 131 | $conditions = $entity->getConditions()->getConfiguration(); |
| 132 | $definitions = $this->conditionManager->getDefinitions(); |
| 133 | |
| 134 | foreach ($definitions as $condition_id => $definition) { |
| 134 | foreach ($definitions as $condition_id => $definition) { |
| 134 | foreach ($definitions as $condition_id => $definition) { |
| 135 | // Don't display the current theme condition. |
| 136 | if ($condition_id === 'current_theme') { |
| 137 | continue; |
| 141 | if ($condition_id === 'language' && !$this->languageManager->isMultilingual()) { |
| 141 | if ($condition_id === 'language' && !$this->languageManager->isMultilingual()) { |
| 142 | continue; |
| 145 | if (\str_starts_with($condition_id, 'entity_bundle:')) { |
| 146 | $entity_type_id = \str_replace('entity_bundle:', '', $condition_id); |
| 147 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 148 | $url = $entity_type->getLinkTemplate('canonical'); |
| 149 | |
| 150 | if (!$url || \str_starts_with((string) $url, '/admin')) { |
| 150 | if (!$url || \str_starts_with((string) $url, '/admin')) { |
| 151 | continue; |
| 134 | foreach ($definitions as $condition_id => $definition) { |
| 135 | // Don't display the current theme condition. |
| 136 | if ($condition_id === 'current_theme') { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // Don't display the language condition until we have multiple languages. |
| 141 | if ($condition_id === 'language' && !$this->languageManager->isMultilingual()) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (\str_starts_with($condition_id, 'entity_bundle:')) { |
| 146 | $entity_type_id = \str_replace('entity_bundle:', '', $condition_id); |
| 147 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 148 | $url = $entity_type->getLinkTemplate('canonical'); |
| 149 | |
| 150 | if (!$url || \str_starts_with((string) $url, '/admin')) { |
| 151 | continue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** @var \Drupal\Core\Condition\ConditionInterface $condition */ |
| 156 | $condition = $this->conditionManager->createInstance($condition_id, $conditions[$condition_id] ?? []); |
| 134 | foreach ($definitions as $condition_id => $definition) { |
| 135 | // Don't display the current theme condition. |
| 136 | if ($condition_id === 'current_theme') { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // Don't display the language condition until we have multiple languages. |
| 141 | if ($condition_id === 'language' && !$this->languageManager->isMultilingual()) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (\str_starts_with($condition_id, 'entity_bundle:')) { |
| 146 | $entity_type_id = \str_replace('entity_bundle:', '', $condition_id); |
| 147 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 148 | $url = $entity_type->getLinkTemplate('canonical'); |
| 149 | |
| 150 | if (!$url || \str_starts_with((string) $url, '/admin')) { |
| 151 | continue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** @var \Drupal\Core\Condition\ConditionInterface $condition */ |
| 156 | $condition = $this->conditionManager->createInstance($condition_id, $conditions[$condition_id] ?? []); |
| 157 | $form_state->set(['conditions', $condition_id], $condition); |
| 158 | $condition_form = $condition->buildConfigurationForm([], $form_state); |
| 159 | $condition_form['#type'] = 'details'; |
| 160 | $condition_form['#title'] = $definition['label']; |
| 161 | $condition_form['#group'] = 'visibility_tabs'; |
| 162 | $form[$condition_id] = $condition_form; |
| 163 | } |
| 164 | |
| 165 | return $this->alterConditionsForm($form); |
| 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; |
| 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), |
| 78 | SAVED_NEW => $this->t('Created new page layout %label.', $message_args), |
| 79 | default => $this->t('Updated 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; |
| 201 | private function submitConditions(array $form, FormStateInterface $form_state): void { |
| 202 | foreach (\array_keys($form_state->getValue('conditions')) as $condition_id) { |
| 202 | foreach (\array_keys($form_state->getValue('conditions')) as $condition_id) { |
| 202 | foreach (\array_keys($form_state->getValue('conditions')) as $condition_id) { |
| 203 | // Allow the condition to submit the form. |
| 204 | $condition = $form_state->get(['conditions', $condition_id]); |
| 202 | foreach (\array_keys($form_state->getValue('conditions')) as $condition_id) { |
| 203 | // Allow the condition to submit the form. |
| 204 | $condition = $form_state->get(['conditions', $condition_id]); |
| 205 | $condition->submitConfigurationForm($form['conditions'][$condition_id], SubformState::createForSubform($form['conditions'][$condition_id], $form, $form_state)); |
| 206 | |
| 207 | $condition_configuration = $condition->getConfiguration(); |
| 208 | // Update the visibility conditions on the block. |
| 209 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $entity */ |
| 210 | $entity = $this->entity; |
| 211 | $entity->getConditions()->addInstanceId((string) $condition_id, $condition_configuration); |
| 212 | } |
| 213 | } |
| 90 | public function submitForm(array &$form, FormStateInterface $form_state): void { |
| 91 | parent::submitForm($form, $form_state); |
| 92 | $this->submitConditions($form, $form_state); |
| 93 | } |