Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ControlsButtons
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 hasButtons
0.00% covered (danger)
0.00%
0 / 17
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
 buildExpandButton
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\Island\IslandPluginToolbarButtonConfigurationBase;
11use Drupal\display_builder\Island\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('expand')) {
34      $buttons[] = $this->buildExpandButton();
35      $library[] = 'display_builder/expand';
36    }
37
38    if ($this->isButtonEnabled('theme')) {
39      $buttons[] = $this->buildThemeMenu();
40    }
41
42    if ($this->isButtonEnabled('help')) {
43      $buttons[] = $this->buildKeyboardButton();
44    }
45
46    if (empty($buttons)) {
47      return [];
48    }
49
50    return [
51      '#type' => 'component',
52      '#component' => 'display_builder:button_group',
53      '#slots' => [
54        'buttons' => $buttons,
55      ],
56      '#attached' => [
57        'library' => $library,
58      ],
59    ];
60  }
61
62  /**
63   * {@inheritdoc}
64   */
65  protected function hasButtons(): array {
66    return [
67      'expand' => [
68        'title' => $this->t('Expand'),
69        'description' => $this->t('Expand the builder to cover the current viewport.'),
70        'default' => 'icon',
71      ],
72      'theme' => [
73        'title' => $this->t('Theme'),
74        'description' => $this->t('Pick a theme mode as light/dark/system for the display builder.'),
75        'default' => 'icon',
76      ],
77      'help' => [
78        'title' => $this->t('Help'),
79        'description' => $this->t('Information about the available keyboard shortcuts.'),
80        'default' => 'icon',
81      ],
82    ];
83  }
84
85  /**
86   * Builds the expand button.
87   *
88   * @return array
89   *   The expand button render array.
90   */
91  private function buildExpandButton(): array {
92    $expand = $this->buildButton(
93      $this->showLabel('expand') ? $this->t('Expand') : '',
94      'expand',
95      $this->showIcon('expand') ? 'arrows-fullscreen' : '',
96      $this->t('Expand to cover the viewport. (shortcut: Shift+E)'),
97      ['shift+e' => $this->t('Toggle expand')]
98    );
99    // Required for the library to work.
100    $expand['#attributes']['data-set-expand'] = TRUE;
101
102    return $expand;
103  }
104
105  /**
106   * Builds the theme menu component.
107   *
108   * @return array
109   *   The theme menu component render array.
110   */
111  private function buildThemeMenu(): array {
112    return [
113      '#type' => 'component',
114      '#component' => 'display_builder:theme_menu',
115      '#attributes' => [
116        'placement' => 'bottom-end',
117        'data-theme-switch' => TRUE,
118      ],
119      '#slots' => [
120        'button' => $this->buildButton(
121          $this->showLabel('theme') ? $this->t('Theme') : '',
122          'theme',
123          $this->showIcon('theme') ? 'sun' : '',
124        ),
125      ],
126      '#props' => [
127        'tooltip' => $this->t('Switch the display builder interface theme.'),
128        'icon' => 'sun',
129      ],
130    ];
131  }
132
133  /**
134   * Builds the keyboard button.
135   *
136   * @return array
137   *   The keyboard button render array.
138   */
139  private function buildKeyboardButton(): array {
140    return $this->buildButton(
141      $this->showLabel('help') ? $this->t('Help') : '',
142      'help',
143      $this->showIcon('help') ? 'question-circle' : '',
144      '...'
145    );
146  }
147
148}