Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 161
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
IslandConfigurationFormTrait
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 161
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 validateConfigurationForm
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
 submitConfigurationForm
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 160
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder\Island;
6
7use Drupal\Component\Plugin\ConfigurableInterface;
8use Drupal\Core\Form\FormStateInterface;
9
10/**
11 * Add methods to make plugin configuration forms.
12 */
13trait IslandConfigurationFormTrait {
14
15  /**
16   * Validate the Island configuration.
17   *
18   * @param array $form
19   *   The form.
20   * @param \Drupal\Core\Form\FormStateInterface $form_state
21   *   The form state.
22   */
23  public function validateConfigurationForm(array &$form, FormStateInterface $form_state): void {}
24
25  /**
26   * Submit the island configuration.
27   *
28   * @param array $form
29   *   The form.
30   * @param \Drupal\Core\Form\FormStateInterface $form_state
31   *   The form state.
32   */
33  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
34    $values = $form_state->getValues();
35    $flatValues = [];
36
37    // Define keys that represent structural wrappers without data
38    // You might need to add 'container' or others depending on your forms.
39    $wrappers = ['fieldset', 'details', 'container'];
40
41    foreach ($values as $key => $value) {
42      // Check if this top-level key corresponds to a wrapper element in the
43      // form, we check the $form array to confirm the type.
44      if (isset($form[$key]['#type']) && \in_array($form[$key]['#type'], $wrappers, TRUE)) {
45        // If it's a wrapper, merge its children into the main array.
46        if (\is_array($value)) {
47          $flatValues = \array_merge($flatValues, $value);
48        }
49      }
50      else {
51        // Otherwise, keep the value as is.
52        $flatValues[$key] = $value;
53      }
54    }
55
56    $configuration = [];
57
58    foreach ($flatValues as $key => $value) {
59      if (($key === 'exclude' || $key === 'status') && \is_array($value)) {
60        // Remove unchecked values.
61        $value = \array_filter($value);
62      }
63      $configuration[$key] = $value;
64    }
65
66    if ($this instanceof ConfigurableInterface) {
67      $this->setConfiguration($configuration);
68    }
69  }
70
71}