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