Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 95
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ControlsButtons
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 6
380
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
42
 hasButtons
0.00% covered (danger)
0.00%
0 / 21
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
 buildHighlightButton
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 buildFullscreenButton
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 buildThemeMenu
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 buildKeyboardButton
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder\Plugin\display_builder\Island;
6
7use Drupal\Core\StringTranslation\TranslatableMarkup;
8use Drupal\display_builder\Attribute\Island;
9use Drupal\display_builder\InstanceInterface;
10use Drupal\display_builder\IslandPluginToolbarButtonConfigurationBase;
11use Drupal\display_builder\IslandType;
12
13/**
14 * Controls button island plugin implementation.
15 */
16#[Island(
17  id: 'controls',
18  enabled_by_default: TRUE,
19  label: new TranslatableMarkup('Controls'),
20  description: new TranslatableMarkup('Control the building experience.'),
21  type: IslandType::Button,
22  default_region: 'end',
23)]
24class ControlsButtons extends IslandPluginToolbarButtonConfigurationBase {
25
26  /**
27   * {@inheritdoc}
28   */
29  public function build(InstanceInterface $builder, array $data = [], array $options = []): array {
30    $buttons = [];
31    $library = [];
32
33    if ($this->isButtonEnabled('highlight')) {
34      $buttons[] = $this->buildHighlightButton();
35      $library[] = 'display_builder/highlight';
36    }
37
38    if ($this->isButtonEnabled('fullscreen')) {
39      $buttons[] = $this->buildFullscreenButton();
40      $library[] = 'display_builder/fullscreen';
41    }
42
43    if ($this->isButtonEnabled('theme')) {
44      $buttons[] = $this->buildThemeMenu();
45    }
46
47    if ($this->isButtonEnabled('help')) {
48      $buttons[] = $this->buildKeyboardButton();
49    }
50
51    if (empty($buttons)) {
52      return [];
53    }
54
55    return [
56      '#type' => 'component',
57      '#component' => 'display_builder:button_group',
58      '#slots' => [
59        'buttons' => $buttons,
60      ],
61      '#attached' => [
62        'library' => $library,
63      ],
64    ];
65  }
66
67  /**
68   * {@inheritdoc}
69   */
70  protected function hasButtons(): array {
71    return [
72      'highlight' => [
73        'title' => $this->t('Highlight'),
74        'description' => $this->t('Highlight builder zones to ease drag and move around.'),
75        'default' => 'icon',
76      ],
77      'fullscreen' => [
78        'title' => $this->t('Fullscreen'),
79        'default' => 'icon',
80      ],
81      'theme' => [
82        'title' => $this->t('Theme'),
83        'description' => $this->t('Pick a theme mode as light/dark/system for the display builder.'),
84        'default' => 'icon',
85      ],
86      'help' => [
87        'title' => $this->t('Help'),
88        'description' => $this->t('Information about the available keyboard shortcuts.'),
89        'default' => 'icon',
90      ],
91    ];
92  }
93
94  /**
95   * Builds the highlight button.
96   *
97   * @return array
98   *   The highlight button render array.
99   */
100  private function buildHighlightButton(): array {
101    $highlight = $this->buildButton(
102      $this->showLabel('highlight') ? $this->t('Highlight') : '',
103      'highlight',
104      $this->showIcon('highlight') ? 'border' : '',
105      $this->t('Highlight components. (shortcut: H)'),
106      ['H' => $this->t('Toggle highlight (shift+H)')]
107    );
108    // Required for the library to work.
109    $highlight['#attributes']['data-set-highlight'] = TRUE;
110
111    return $highlight;
112  }
113
114  /**
115   * Builds the fullscreen button.
116   *
117   * @return array
118   *   The fullscreen button render array.
119   */
120  private function buildFullscreenButton(): array {
121    $fullscreen = $this->buildButton(
122      $this->showLabel('fullscreen') ? $this->t('Fullscreen') : '',
123      'fullscreen',
124      $this->showIcon('fullscreen') ? 'fullscreen' : '',
125      $this->t('Toggle fullscreen. (shortcut: F)'),
126      ['F' => $this->t('Toggle fullscreen (shift+F)')]
127    );
128    // Required for the library to work.
129    $fullscreen['#attributes']['data-set-fullscreen'] = TRUE;
130
131    return $fullscreen;
132  }
133
134  /**
135   * Builds the theme menu component.
136   *
137   * @return array
138   *   The theme menu component render array.
139   */
140  private function buildThemeMenu(): array {
141    return [
142      '#type' => 'component',
143      '#component' => 'display_builder:theme_menu',
144      '#attributes' => [
145        'placement' => 'bottom-end',
146        'data-theme-switch' => TRUE,
147      ],
148      '#slots' => [
149        'button' => $this->buildButton(
150          $this->showLabel('theme') ? $this->t('Theme') : '',
151          'theme',
152          $this->showIcon('theme') ? 'sun' : '',
153        ),
154      ],
155      '#props' => [
156        'tooltip' => $this->t('Switch the display builder interface theme.'),
157        'icon' => 'sun',
158      ],
159    ];
160  }
161
162  /**
163   * Builds the keyboard button.
164   *
165   * @return array
166   *   The keyboard button render array.
167   */
168  private function buildKeyboardButton(): array {
169    return $this->buildButton(
170      $this->showLabel('help') ? $this->t('Help') : '',
171      'help',
172      $this->showIcon('help') ? 'question-circle' : '',
173      '...'
174    );
175  }
176
177}