Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| UiStylesPanel | |
0.00% |
0 / 28 |
|
0.00% |
0 / 13 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 10 |
156 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| label | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildForm | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateForm | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| alterElement | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onAttachToRoot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onAttachToSlot | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onDelete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isApplicable | |
0.00% |
0 / 2 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder\Plugin\display_builder\Island; |
| 6 | |
| 7 | use Drupal\Core\Form\FormStateInterface; |
| 8 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 9 | use Drupal\display_builder\Attribute\Island; |
| 10 | use Drupal\display_builder\IslandPluginBase; |
| 11 | use Drupal\display_builder\IslandType; |
| 12 | use Drupal\display_builder\IslandWithFormInterface; |
| 13 | use Drupal\display_builder\IslandWithFormTrait; |
| 14 | use Drupal\display_builder\RenderableAltererInterface; |
| 15 | use Drupal\ui_styles\StylePluginManagerInterface; |
| 16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 17 | |
| 18 | /** |
| 19 | * Styles island plugin implementation. |
| 20 | * |
| 21 | * @todo must move to UI Styles module. |
| 22 | */ |
| 23 | #[Island( |
| 24 | id: 'ui_styles', |
| 25 | label: new TranslatableMarkup('UI Styles'), |
| 26 | description: new TranslatableMarkup('Apply style utilities to the active component or block'), |
| 27 | type: IslandType::Contextual, |
| 28 | )] |
| 29 | class UiStylesPanel extends IslandPluginBase implements IslandWithFormInterface, RenderableAltererInterface { |
| 30 | |
| 31 | use IslandWithFormTrait; |
| 32 | |
| 33 | /** |
| 34 | * The UI Styles styles manager. |
| 35 | */ |
| 36 | protected StylePluginManagerInterface $stylesManager; |
| 37 | |
| 38 | /** |
| 39 | * {@inheritdoc} |
| 40 | */ |
| 41 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 42 | $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
| 43 | $instance->stylesManager = $container->get('plugin.manager.ui_styles'); |
| 44 | |
| 45 | return $instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * {@inheritdoc} |
| 50 | */ |
| 51 | public function label(): string { |
| 52 | return 'Styles'; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * {@inheritdoc} |
| 57 | */ |
| 58 | public function buildForm(array &$form, FormStateInterface $form_state): void { |
| 59 | // The data to be received here is in the form of: |
| 60 | // ['styles' => ['selected' => [], 'extra' => '']]. |
| 61 | $form += [ |
| 62 | 'styles' => [ |
| 63 | '#type' => 'ui_styles_styles', |
| 64 | '#title' => $this->t('Styles'), |
| 65 | '#wrapper_type' => 'div', |
| 66 | '#default_value' => \array_merge(['selected' => [], 'extra' => ''], $this->data ?? []), |
| 67 | ], |
| 68 | '#tree' => TRUE, |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritdoc} |
| 74 | */ |
| 75 | public function validateForm(array &$form, FormStateInterface $form_state): void { |
| 76 | // The styles key in the values is added by BlockStylesForm. |
| 77 | // Inside the styles key, there are two keys: selected and extra. |
| 78 | // The structure here is the one produced by UI styles Form API element. |
| 79 | $values = $form_state->getValue('styles'); |
| 80 | $form_state->setValue('selected', $values['selected'] ?? []); |
| 81 | $form_state->setValue('extra', $values['extra'] ?? ''); |
| 82 | $form_state->unsetValue('styles'); |
| 83 | // Those two lines are necessary to prevent the form from being rebuilt. |
| 84 | // if rebuilt, the form state values will have both the computed ones |
| 85 | // and the raw ones (wrapper key and values). |
| 86 | $form_state->setRebuild(FALSE); |
| 87 | $form_state->setExecuted(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * {@inheritdoc} |
| 92 | */ |
| 93 | public function alterElement(array $element, array $data = []): array { |
| 94 | $selected = $data['selected'] ?? []; |
| 95 | $extra = $data['extra'] ?? ''; |
| 96 | |
| 97 | return $this->stylesManager->addClasses($element, $selected, $extra); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * {@inheritdoc} |
| 102 | */ |
| 103 | public function onAttachToRoot(string $builder_id, string $instance_id): array { |
| 104 | return $this->reloadWithInstanceData($builder_id, $instance_id); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * {@inheritdoc} |
| 109 | */ |
| 110 | public function onAttachToSlot(string $builder_id, string $instance_id, string $parent_id): array { |
| 111 | return $this->reloadWithInstanceData($builder_id, $instance_id); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * {@inheritdoc} |
| 116 | */ |
| 117 | public function onActive(string $builder_id, array $data): array { |
| 118 | return $this->reloadWithLocalData($builder_id, $data); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * {@inheritdoc} |
| 123 | */ |
| 124 | public function onDelete(string $builder_id, string $parent_id): array { |
| 125 | return $this->reloadWithLocalData($builder_id, []); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * {@inheritdoc} |
| 130 | */ |
| 131 | public function isApplicable(): bool { |
| 132 | return parent::isApplicable() && !empty($this->data) && \Drupal::service('module_handler') |
| 133 | ->moduleExists('ui_styles'); |
| 134 | } |
| 135 | |
| 136 | } |