Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
ProfileIslandPluginForm | |
0.00% |
0 / 54 |
|
0.00% |
0 / 10 |
306 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
editFormTitle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getEntityFromRouteMatch | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
validateForm | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
form | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
save | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
actions | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
copyFormValuesToEntity | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getIslandPlugin | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder\Form; |
6 | |
7 | use Drupal\Core\Entity\EntityForm; |
8 | use Drupal\Core\Entity\EntityInterface; |
9 | use Drupal\Core\Form\FormStateInterface; |
10 | use Drupal\Core\Form\SubformState; |
11 | use Drupal\Core\Plugin\PluginFormInterface; |
12 | use Drupal\Core\Routing\RouteMatchInterface; |
13 | use Drupal\display_builder\Entity\Profile; |
14 | use Drupal\display_builder\IslandInterface; |
15 | use Drupal\display_builder\IslandPluginManagerInterface; |
16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
17 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
18 | |
19 | /** |
20 | * Display builder plugin form. |
21 | */ |
22 | final class ProfileIslandPluginForm extends EntityForm { |
23 | |
24 | /** |
25 | * The route parameter for the island plugin. |
26 | */ |
27 | protected string $islandId; |
28 | |
29 | /** |
30 | * The island plugin. |
31 | */ |
32 | private ?IslandInterface $island = NULL; |
33 | |
34 | public function __construct( |
35 | protected IslandPluginManagerInterface $islandPluginManager, |
36 | ) {} |
37 | |
38 | /** |
39 | * Returns the title of the edit plugin form. |
40 | * |
41 | * @param string $island_id |
42 | * The island ID. |
43 | * |
44 | * @return string |
45 | * The title of the edit plugin form. |
46 | */ |
47 | public static function editFormTitle(string $island_id): string { |
48 | /** @var \Drupal\display_builder\IslandInterface $island */ |
49 | $island = \Drupal::service('plugin.manager.db_island')->createInstance($island_id, []); |
50 | |
51 | return $island->label(); |
52 | } |
53 | |
54 | /** |
55 | * {@inheritdoc} |
56 | */ |
57 | public static function create(ContainerInterface $container): ProfileIslandPluginForm { |
58 | return new self( |
59 | $container->get('plugin.manager.db_island') |
60 | ); |
61 | } |
62 | |
63 | /** |
64 | * {@inheritdoc} |
65 | */ |
66 | public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id): EntityInterface { |
67 | $this->islandId = $route_match->getParameter('island_id'); |
68 | |
69 | return parent::getEntityFromRouteMatch($route_match, $entity_type_id); |
70 | } |
71 | |
72 | /** |
73 | * {@inheritdoc} |
74 | */ |
75 | public function validateForm(array &$form, FormStateInterface $form_state): void { |
76 | parent::validateForm($form, $form_state); |
77 | $island = $this->getIslandPlugin(); |
78 | |
79 | if ($island instanceof PluginFormInterface) { |
80 | $subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state); |
81 | $island->validateConfigurationForm($form['configuration'], $subform_state); |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * {@inheritdoc} |
87 | */ |
88 | public function form(array $form, FormStateInterface $form_state): array { |
89 | $form = parent::form($form, $form_state); |
90 | $island = $this->getIslandPlugin(); |
91 | |
92 | if (!($island instanceof PluginFormInterface)) { |
93 | throw new AccessDeniedHttpException('The island plugin does not support configuration.'); |
94 | } |
95 | $form['configuration'] = [ |
96 | '#type' => 'container', |
97 | '#tree' => TRUE, |
98 | ]; |
99 | $subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state); |
100 | $form['configuration'] = $island->buildConfigurationForm($form['configuration'], $subform_state); |
101 | |
102 | return $form; |
103 | } |
104 | |
105 | /** |
106 | * {@inheritdoc} |
107 | */ |
108 | public function save(array $form, FormStateInterface $form_state): int { |
109 | $result = parent::save($form, $form_state); |
110 | |
111 | // Clear the plugin cache so changes are applied on front theme builder. |
112 | /** @var \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $pluginCacheClearer */ |
113 | $pluginCacheClearer = \Drupal::service('plugin.cache_clearer'); // phpcs:ignore |
114 | $pluginCacheClearer->clearCachedDefinitions(); |
115 | |
116 | $message_args = ['%label' => $this->entity->label()]; |
117 | $this->messenger()->addStatus( |
118 | match ($result) { |
119 | SAVED_NEW => $this->t('Created new display builder config %label.', $message_args), |
120 | SAVED_UPDATED => $this->t('Updated display builder config %label.', $message_args), |
121 | default => '', |
122 | } |
123 | ); |
124 | |
125 | return $result; |
126 | } |
127 | |
128 | /** |
129 | * {@inheritdoc} |
130 | */ |
131 | protected function actions(array $form, FormStateInterface $form_state): array { |
132 | $actions = parent::actions($form, $form_state); |
133 | unset($actions['delete']); |
134 | |
135 | return $actions; |
136 | } |
137 | |
138 | /** |
139 | * {@inheritdoc} |
140 | */ |
141 | protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state): void { |
142 | \assert($entity instanceof Profile); |
143 | parent::copyFormValuesToEntity($entity, $form, $form_state); |
144 | $island = $this->getIslandPlugin(); |
145 | |
146 | if ($island instanceof PluginFormInterface) { |
147 | $subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state); |
148 | $island->submitConfigurationForm($form['configuration'], $subform_state); |
149 | $entity->setIslandConfiguration($this->islandId, $island->getConfiguration()); |
150 | } |
151 | } |
152 | |
153 | /** |
154 | * Retrieves the configured island plugin. |
155 | */ |
156 | private function getIslandPlugin(): IslandInterface { |
157 | if ($this->island !== NULL) { |
158 | return $this->island; |
159 | } |
160 | /** @var \Drupal\display_builder\Entity\Profile $entity */ |
161 | $entity = $this->entity; |
162 | $island_configuration = $entity->getIslandConfiguration($this->islandId); |
163 | $island = $this->islandPluginManager->createInstance($this->islandId, $island_configuration); |
164 | \assert($island instanceof IslandInterface); |
165 | $this->island = $island; |
166 | |
167 | return $this->island; |
168 | } |
169 | |
170 | } |