Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IslandFormBase
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 getFormId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildForm
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 validateForm
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 submitForm
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getPlugin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder\Form;
6
7use Drupal\Core\Form\FormBase;
8use Drupal\Core\Form\FormStateInterface;
9use Drupal\display_builder\IslandWithFormInterface;
10
11/**
12 * Provides a display builder form for island plugin.
13 */
14final class IslandFormBase extends FormBase {
15
16  /**
17   * {@inheritdoc}
18   */
19  public function getFormId(): string {
20    return 'display_builder_island';
21  }
22
23  /**
24   * {@inheritdoc}
25   */
26  public function buildForm(array $form, FormStateInterface $form_state): array {
27    $island_args = $form_state->getBuildInfo()['args'][0];
28    $plugin = IslandFormBase::getPlugin($island_args);
29
30    if (!$plugin instanceof IslandWithFormInterface) {
31      return $form;
32    }
33    $plugin->setBuilderId($island_args['builder_id']);
34    $plugin->buildForm($form, $form_state);
35
36    return $form;
37  }
38
39  /**
40   * {@inheritdoc}
41   */
42  public function validateForm(array &$form, FormStateInterface $form_state): void {
43    $island_args = $form_state->getBuildInfo()['args'][0];
44    $plugin = IslandFormBase::getPlugin($island_args);
45
46    if (!$plugin instanceof IslandWithFormInterface) {
47      return;
48    }
49    $plugin->validateForm($form, $form_state);
50  }
51
52  /**
53   * {@inheritdoc}
54   */
55  public function submitForm(array &$form, FormStateInterface $form_state): void {
56    $island_args = $form_state->getBuildInfo()['args'][0];
57    $plugin = IslandFormBase::getPlugin($island_args);
58
59    if (!$plugin instanceof IslandWithFormInterface) {
60      return;
61    }
62    $plugin->submitForm($form, $form_state);
63  }
64
65  /**
66   * Get Island plugin.
67   *
68   * @param array $args
69   *   Arguments from form_state which allow to load plugin.
70   *
71   * @return object|\Drupal\display_builder\IslandInterface
72   *   The Island plugin.
73   */
74  protected static function getPlugin(array $args) {
75    return \Drupal::service('plugin.manager.db_island')->createInstance($args['island_id'], $args['instance']);
76  }
77
78}