Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ControlsButtons
0.00% covered (danger)
0.00%
0 / 82
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 defaultConfiguration
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 buildConfigurationForm
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 configurationSummary
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 build
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder\Plugin\display_builder\Island;
6
7use Drupal\Core\Form\FormStateInterface;
8use Drupal\Core\StringTranslation\TranslatableMarkup;
9use Drupal\display_builder\Attribute\Island;
10use Drupal\display_builder\InstanceInterface;
11use Drupal\display_builder\IslandConfigurationFormInterface;
12use Drupal\display_builder\IslandConfigurationFormTrait;
13use Drupal\display_builder\IslandPluginBase;
14use Drupal\display_builder\IslandType;
15
16/**
17 * Controls button island plugin implementation.
18 */
19#[Island(
20  id: 'controls',
21  enabled_by_default: TRUE,
22  label: new TranslatableMarkup('Controls'),
23  description: new TranslatableMarkup('Control the building experience.'),
24  type: IslandType::Button,
25)]
26class ControlsButtons extends IslandPluginBase implements IslandConfigurationFormInterface {
27
28  use IslandConfigurationFormTrait;
29
30  /**
31   * {@inheritdoc}
32   */
33  public function defaultConfiguration(): array {
34    return [
35      'toggle_highlight' => TRUE,
36      'toggle_fullscreen' => TRUE,
37      'keyboard_help' => FALSE,
38      'theme_mode' => FALSE,
39    ];
40  }
41
42  /**
43   * {@inheritdoc}
44   */
45  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
46    $configuration = $this->getConfiguration();
47
48    $form['toggle_highlight'] = [
49      '#type' => 'checkbox',
50      '#title' => $this->t('Toggle highlight'),
51      '#description' => $this->t('Toggle the builder highlight zones to ease drag and move around.'),
52      '#default_value' => $configuration['toggle_highlight'],
53    ];
54    $form['toggle_fullscreen'] = [
55      '#type' => 'checkbox',
56      '#title' => $this->t('Toggle fullscreen'),
57      '#description' => $this->t('Toggle the builder as fullscreen.'),
58      '#default_value' => $configuration['toggle_fullscreen'],
59    ];
60    $form['keyboard_help'] = [
61      '#type' => 'checkbox',
62      '#title' => $this->t('Keyboard help'),
63      '#description' => $this->t('Information on the available keyboard shortcuts.'),
64      '#default_value' => $configuration['keyboard_help'],
65    ];
66    $form['theme_mode'] = [
67      '#type' => 'checkbox',
68      '#title' => $this->t('Theme mode selector'),
69      '#description' => $this->t('Pick a theme mode as light/dark/system for the display builder.'),
70      '#default_value' => $configuration['theme_mode'],
71    ];
72
73    return $form;
74  }
75
76  /**
77   * {@inheritdoc}
78   */
79  public function configurationSummary(): array {
80    $configuration = $this->getConfiguration();
81
82    return \array_filter([
83      $configuration['toggle_highlight'] ? $this->t('With toggle highlight.') : '',
84      $configuration['toggle_fullscreen'] ? $this->t('With toggle fullscreen.') : '',
85      $configuration['keyboard_help'] ? $this->t('With keyboard help.') : '',
86      $configuration['theme_mode'] ? $this->t('With theme mode selector.') : '',
87    ]);
88  }
89
90  /**
91   * {@inheritdoc}
92   */
93  public function build(InstanceInterface $builder, array $data = [], array $options = []): array {
94    $configuration = $this->getConfiguration();
95    $buttons = $library = [];
96
97    if ($configuration['toggle_highlight']) {
98      $highlight = $this->buildButton('', 'highlight', 'border', $this->t('Highlight components. (shortcut: H)'), ['H' => $this->t('Toggle highlight (shift+H)')]);
99      // Required for the library to work.
100      $highlight['#attributes']['data-set-highlight'] = TRUE;
101      $library[] = 'display_builder/highlight';
102      $buttons[] = $highlight;
103    }
104
105    if ($configuration['toggle_fullscreen']) {
106      $fullscreen = $this->buildButton('', 'fullscreen', 'fullscreen', $this->t('Toggle fullscreen. (shortcut: F)'), ['F' => $this->t('Toggle fullscreen (shift+M)')]);
107      // Required for the library to work.
108      $fullscreen['#attributes']['data-set-fullscreen'] = TRUE;
109      $library[] = 'display_builder/fullscreen';
110      $buttons[] = $fullscreen;
111    }
112
113    if ($configuration['theme_mode']) {
114      $buttons[] = [
115        '#type' => 'component',
116        '#component' => 'display_builder:theme_menu',
117        '#attributes' => [
118          'placement' => 'bottom-end',
119          'data-theme-switch' => TRUE,
120        ],
121        '#slots' => [
122          'button' => $this->buildButton('', 'theme', 'sun'),
123        ],
124        '#props' => [
125          'tooltip' => $this->t('Switch the display builder interface theme.'),
126          'icon' => 'sun',
127        ],
128      ];
129    }
130
131    if ($configuration['keyboard_help']) {
132      $keyboard = $this->buildButton('', 'help', 'question-circle', '...');
133      $buttons[] = $keyboard;
134    }
135
136    if (empty($buttons)) {
137      return [];
138    }
139
140    return [
141      '#type' => 'component',
142      '#component' => 'display_builder:button_group',
143      '#slots' => [
144        'buttons' => $buttons,
145      ],
146      '#attached' => [
147        'library' => $library,
148      ],
149    ];
150  }
151
152}