Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 36 |
|
0.00% |
0 / 29 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| IslandPluginToolbarButtonConfigurationBase | |
0.00% |
0 / 52 |
|
0.00% |
0 / 36 |
|
0.00% |
0 / 29 |
|
0.00% |
0 / 9 |
552 | |
0.00% |
0 / 1 |
| defaultConfiguration | |
0.00% |
0 / 4 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| buildConfigurationForm | |
0.00% |
0 / 19 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| configurationSummary | |
0.00% |
0 / 12 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| getButtonSummary | |
0.00% |
0 / 8 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
30 | |||
| isButtonEnabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| showLabel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| showIcon | |
0.00% |
0 / 2 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| hasButtons | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getButtonValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder; |
| 6 | |
| 7 | use Drupal\Core\Form\FormStateInterface; |
| 8 | use 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 | */ |
| 16 | abstract 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 | if (isset($button['remove'])) { |
| 50 | unset($options[$button['remove']]); |
| 51 | } |
| 52 | $default_value = $configuration[$button_id][self::KEY_VALUE] ?? $button['default'] ?? 'hidden'; |
| 53 | $form[$button_id][self::KEY_VALUE] = [ |
| 54 | '#type' => 'select', |
| 55 | '#title' => $this->t('@name button', ['@name' => $button['title']]), |
| 56 | '#options' => $options, |
| 57 | '#default_value' => $default_value, |
| 58 | '#description' => $button['description'] ?? '', |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | return $form; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | */ |
| 68 | public function configurationSummary(): array { |
| 69 | $configuration = $this->getConfiguration(); |
| 70 | $buttons = []; |
| 71 | |
| 72 | foreach ($this->hasButtons() as $button_id => $button) { |
| 73 | if (empty($configuration[$button_id]['value'])) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | if ($summary = $this->getButtonSummary($button_id, $button, $configuration[$button_id])) { |
| 78 | $buttons[] = $summary; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (empty($buttons)) { |
| 83 | return [$this->t('No buttons. Will not be displayed.')]; |
| 84 | } |
| 85 | |
| 86 | return [ |
| 87 | $this->t('Buttons: @buttons.', ['@buttons' => \implode(', ', $buttons)]), |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get summary of a single button. |
| 93 | * |
| 94 | * @param string $button_id |
| 95 | * The button ID. |
| 96 | * @param array $definition |
| 97 | * The button definition according to ::hasButtons() |
| 98 | * @param array $configuration |
| 99 | * The button current configuration. |
| 100 | * |
| 101 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup|null |
| 102 | * The configuration summary of the button. |
| 103 | */ |
| 104 | protected function getButtonSummary(string $button_id, array $definition, array $configuration): ?TranslatableMarkup { |
| 105 | $value = $configuration[self::KEY_VALUE] ?? 'label'; |
| 106 | $title = $definition['title'] ?? $button_id; |
| 107 | |
| 108 | return match ($value) { |
| 109 | 'label' => $this->t('@button (label only)', ['@button' => $title]), |
| 110 | 'icon' => $this->t('@button (icon only)', ['@button' => $title]), |
| 111 | 'icon_label' => $this->t('@button (icon and label)', ['@button' => $title]), |
| 112 | default => NULL, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Is the button enabled? |
| 118 | * |
| 119 | * @param string $button_id |
| 120 | * The button ID. |
| 121 | * |
| 122 | * @return bool |
| 123 | * The button is enabled if it has a label or an icon. |
| 124 | */ |
| 125 | protected function isButtonEnabled(string $button_id): bool { |
| 126 | $value = $this->getButtonValue($button_id); |
| 127 | |
| 128 | return $value !== NULL && $value !== 'hidden'; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Shows if the label is enabled for a given button. |
| 133 | * |
| 134 | * @param string $button_id |
| 135 | * The button ID. |
| 136 | * |
| 137 | * @return bool |
| 138 | * TRUE if the label is enabled, FALSE otherwise. |
| 139 | */ |
| 140 | protected function showLabel(string $button_id): bool { |
| 141 | $value = $this->getButtonValue($button_id); |
| 142 | |
| 143 | return $value === 'label' || $value === 'icon_label'; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Shows if the label is enabled for a given button. |
| 148 | * |
| 149 | * @param string $button_id |
| 150 | * The button ID. |
| 151 | * |
| 152 | * @return bool |
| 153 | * TRUE if the label is enabled, FALSE otherwise. |
| 154 | */ |
| 155 | protected function showIcon(string $button_id): bool { |
| 156 | $value = $this->getButtonValue($button_id); |
| 157 | |
| 158 | return $value === 'icon' || $value === 'icon_label'; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the list of buttons provided by this plugin. |
| 163 | * |
| 164 | * Each button is an array with two keys: 'label' and 'icon', both boolean. |
| 165 | * The keys indicate if the label or icon can be configured to be shown. |
| 166 | * For example: |
| 167 | * |
| 168 | * @code |
| 169 | * return [ |
| 170 | * 'my_button_id' => ['label' => TRUE, 'icon' => FALSE], |
| 171 | * 'my_other_button_id' => ['label' => TRUE, 'icon'=> TRUE], |
| 172 | * ]; |
| 173 | * |
| 174 | * @endcode |
| 175 | * |
| 176 | * @return array |
| 177 | * An associative array of button IDs and their configuration options. |
| 178 | */ |
| 179 | protected function hasButtons(): array { |
| 180 | return []; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Gets the configuration value for a given button. |
| 185 | * |
| 186 | * @param string $button_id |
| 187 | * The button ID. |
| 188 | * |
| 189 | * @return string|null |
| 190 | * The configuration value, or null if not set. |
| 191 | */ |
| 192 | private function getButtonValue(string $button_id): ?string { |
| 193 | $configuration = $this->getConfiguration(); |
| 194 | |
| 195 | return $configuration[$button_id][self::KEY_VALUE] ?? NULL; |
| 196 | } |
| 197 | |
| 198 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 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) { |
| 48 | foreach ($this->hasButtons() as $button_id => $button) { |
| 48 | foreach ($this->hasButtons() as $button_id => $button) { |
| 49 | if (isset($button['remove'])) { |
| 50 | unset($options[$button['remove']]); |
| 51 | } |
| 52 | $default_value = $configuration[$button_id][self::KEY_VALUE] ?? $button['default'] ?? 'hidden'; |
| 48 | foreach ($this->hasButtons() as $button_id => $button) { |
| 49 | if (isset($button['remove'])) { |
| 50 | unset($options[$button['remove']]); |
| 51 | } |
| 52 | $default_value = $configuration[$button_id][self::KEY_VALUE] ?? $button['default'] ?? 'hidden'; |
| 48 | foreach ($this->hasButtons() as $button_id => $button) { |
| 49 | if (isset($button['remove'])) { |
| 50 | unset($options[$button['remove']]); |
| 51 | } |
| 52 | $default_value = $configuration[$button_id][self::KEY_VALUE] ?? $button['default'] ?? 'hidden'; |
| 53 | $form[$button_id][self::KEY_VALUE] = [ |
| 54 | '#type' => 'select', |
| 55 | '#title' => $this->t('@name button', ['@name' => $button['title']]), |
| 56 | '#options' => $options, |
| 57 | '#default_value' => $default_value, |
| 58 | '#description' => $button['description'] ?? '', |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | return $form; |
| 69 | $configuration = $this->getConfiguration(); |
| 70 | $buttons = []; |
| 71 | |
| 72 | foreach ($this->hasButtons() as $button_id => $button) { |
| 72 | foreach ($this->hasButtons() as $button_id => $button) { |
| 72 | foreach ($this->hasButtons() as $button_id => $button) { |
| 73 | if (empty($configuration[$button_id]['value'])) { |
| 74 | continue; |
| 77 | if ($summary = $this->getButtonSummary($button_id, $button, $configuration[$button_id])) { |
| 72 | foreach ($this->hasButtons() as $button_id => $button) { |
| 73 | if (empty($configuration[$button_id]['value'])) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | if ($summary = $this->getButtonSummary($button_id, $button, $configuration[$button_id])) { |
| 78 | $buttons[] = $summary; |
| 72 | foreach ($this->hasButtons() as $button_id => $button) { |
| 73 | if (empty($configuration[$button_id]['value'])) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | if ($summary = $this->getButtonSummary($button_id, $button, $configuration[$button_id])) { |
| 78 | $buttons[] = $summary; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (empty($buttons)) { |
| 83 | return [$this->t('No buttons. Will not be displayed.')]; |
| 87 | $this->t('Buttons: @buttons.', ['@buttons' => \implode(', ', $buttons)]), |
| 26 | $configuration = []; |
| 27 | |
| 28 | foreach ($this->hasButtons() as $button_id => $button) { |
| 28 | foreach ($this->hasButtons() as $button_id => $button) { |
| 28 | foreach ($this->hasButtons() as $button_id => $button) { |
| 28 | foreach ($this->hasButtons() as $button_id => $button) { |
| 29 | $configuration[$button_id][self::KEY_VALUE] = $button['default'] ?? 'label'; |
| 30 | } |
| 31 | |
| 32 | return $configuration; |
| 104 | protected function getButtonSummary(string $button_id, array $definition, array $configuration): ?TranslatableMarkup { |
| 105 | $value = $configuration[self::KEY_VALUE] ?? 'label'; |
| 106 | $title = $definition['title'] ?? $button_id; |
| 107 | |
| 108 | return match ($value) { |
| 109 | 'label' => $this->t('@button (label only)', ['@button' => $title]), |
| 110 | 'icon' => $this->t('@button (icon only)', ['@button' => $title]), |
| 111 | 'icon_label' => $this->t('@button (icon and label)', ['@button' => $title]), |
| 112 | default => NULL, |
| 112 | default => NULL, |
| 192 | private function getButtonValue(string $button_id): ?string { |
| 193 | $configuration = $this->getConfiguration(); |
| 194 | |
| 195 | return $configuration[$button_id][self::KEY_VALUE] ?? NULL; |
| 180 | return []; |
| 125 | protected function isButtonEnabled(string $button_id): bool { |
| 126 | $value = $this->getButtonValue($button_id); |
| 127 | |
| 128 | return $value !== NULL && $value !== 'hidden'; |
| 128 | return $value !== NULL && $value !== 'hidden'; |
| 128 | return $value !== NULL && $value !== 'hidden'; |
| 155 | protected function showIcon(string $button_id): bool { |
| 156 | $value = $this->getButtonValue($button_id); |
| 157 | |
| 158 | return $value === 'icon' || $value === 'icon_label'; |
| 158 | return $value === 'icon' || $value === 'icon_label'; |
| 158 | return $value === 'icon' || $value === 'icon_label'; |
| 140 | protected function showLabel(string $button_id): bool { |
| 141 | $value = $this->getButtonValue($button_id); |
| 142 | |
| 143 | return $value === 'label' || $value === 'icon_label'; |
| 143 | return $value === 'label' || $value === 'icon_label'; |
| 143 | return $value === 'label' || $value === 'icon_label'; |