Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
IslandPluginToolbarButtonConfigurationBase
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 9
506
0.00% covered (danger)
0.00%
0 / 1
 defaultConfiguration
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 buildConfigurationForm
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 configurationSummary
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 getButtonSummary
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
30
 isButtonEnabled
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 showLabel
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 showIcon
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 hasButtons
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
 getButtonValue
0.00% covered (danger)
0.00%
0 / 2
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
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder;
6
7use Drupal\Core\Form\FormStateInterface;
8use Drupal\Core\StringTranslation\TranslatableMarkup;
9
10/**
11 * Base class for island plugins toolbar with button.
12 *
13 * This class provide a default configuration form to enable/disable
14 * the label and icon for each button provided by the plugin.
15 */
16abstract class IslandPluginToolbarButtonConfigurationBase extends IslandPluginBase implements IslandConfigurationFormInterface {
17
18  use IslandConfigurationFormTrait;
19
20  public const KEY_VALUE = 'value';
21
22  /**
23   * {@inheritdoc}
24   */
25  public function defaultConfiguration(): array {
26    $configuration = [];
27
28    foreach ($this->hasButtons() as $button_id => $button) {
29      $configuration[$button_id][self::KEY_VALUE] = $button['default'] ?? 'label';
30    }
31
32    return $configuration;
33  }
34
35  /**
36   * {@inheritdoc}
37   */
38  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
39    $configuration = $this->getConfiguration();
40
41    $options = [
42      'label' => $this->t('Label'),
43      'icon' => $this->t('Icon'),
44      'icon_label' => $this->t('Icon + Label'),
45      'hidden' => $this->t('Hidden'),
46    ];
47
48    foreach ($this->hasButtons() as $button_id => $button) {
49      $default_value = $configuration[$button_id][self::KEY_VALUE] ?? $button['default'] ?? 'hidden';
50      $form[$button_id][self::KEY_VALUE] = [
51        '#type' => 'select',
52        '#title' => $this->t('@name button', ['@name' => $button['title']]),
53        '#options' => $options,
54        '#default_value' => $default_value,
55        '#description' => $button['description'] ?? '',
56      ];
57    }
58
59    return $form;
60  }
61
62  /**
63   * {@inheritdoc}
64   */
65  public function configurationSummary(): array {
66    $configuration = $this->getConfiguration();
67    $buttons = [];
68
69    foreach ($this->hasButtons() as $button_id => $button) {
70      if (empty($configuration[$button_id]['value'])) {
71        continue;
72      }
73
74      if ($summary = $this->getButtonSummary($button_id, $button, $configuration[$button_id])) {
75        $buttons[] = $summary;
76      }
77    }
78
79    if (empty($buttons)) {
80      return [$this->t('No buttons. Will not be displayed.')];
81    }
82
83    return [
84      $this->t('Buttons: @buttons.', ['@buttons' => \implode(', ', $buttons)]),
85    ];
86  }
87
88  /**
89   * Get summary of a single button.
90   *
91   * @param string $button_id
92   *   The button ID.
93   * @param array $definition
94   *   The button definition according to ::hasButtons()
95   * @param array $configuration
96   *   The button current configuration.
97   *
98   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|null
99   *   The configuration summary of the button.
100   */
101  protected function getButtonSummary(string $button_id, array $definition, array $configuration): ?TranslatableMarkup {
102    $value = $configuration[self::KEY_VALUE] ?? 'label';
103    $title = $definition['title'] ?? $button_id;
104
105    return match ($value) {
106      'label' => $this->t('@button (label only)', ['@button' => $title]),
107      'icon' => $this->t('@button (icon only)', ['@button' => $title]),
108      'icon_label' => $this->t('@button (icon and label)', ['@button' => $title]),
109      default => NULL,
110    };
111  }
112
113  /**
114   * Is the button enabled?
115   *
116   * @param string $button_id
117   *   The button ID.
118   *
119   * @return bool
120   *   The button is enabled if it has a label or an icon.
121   */
122  protected function isButtonEnabled(string $button_id): bool {
123    $value = $this->getButtonValue($button_id);
124
125    return $value !== NULL && $value !== 'hidden';
126  }
127
128  /**
129   * Shows if the label is enabled for a given button.
130   *
131   * @param string $button_id
132   *   The button ID.
133   *
134   * @return bool
135   *   TRUE if the label is enabled, FALSE otherwise.
136   */
137  protected function showLabel(string $button_id): bool {
138    $value = $this->getButtonValue($button_id);
139
140    return $value === 'label' || $value === 'icon_label';
141  }
142
143  /**
144   * Shows if the label is enabled for a given button.
145   *
146   * @param string $button_id
147   *   The button ID.
148   *
149   * @return bool
150   *   TRUE if the label is enabled, FALSE otherwise.
151   */
152  protected function showIcon(string $button_id): bool {
153    $value = $this->getButtonValue($button_id);
154
155    return $value === 'icon' || $value === 'icon_label';
156  }
157
158  /**
159   * Returns the list of buttons provided by this plugin.
160   *
161   * Each button is an array with two keys: 'label' and 'icon', both boolean.
162   * The keys indicate if the label or icon can be configured to be shown.
163   * For example:
164   *
165   * @code
166   * return [
167   *  'my_button_id' => ['label' => TRUE, 'icon' => FALSE],
168   * 'my_other_button_id' => ['label' => TRUE, 'icon'=> TRUE],
169   * ];
170   *
171   * @endcode
172   *
173   * @return array
174   *   An associative array of button IDs and their configuration options.
175   */
176  protected function hasButtons(): array {
177    return [];
178  }
179
180  /**
181   * Gets the configuration value for a given button.
182   *
183   * @param string $button_id
184   *   The button ID.
185   *
186   * @return string|null
187   *   The configuration value, or null if not set.
188   */
189  private function getButtonValue(string $button_id): ?string {
190    $configuration = $this->getConfiguration();
191
192    return $configuration[$button_id][self::KEY_VALUE] ?? NULL;
193  }
194
195}