Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 21 |
|
0.00% |
0 / 16 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| VisibilityConditionsPanel | |
0.00% |
0 / 28 |
|
0.00% |
0 / 21 |
|
0.00% |
0 / 16 |
|
0.00% |
0 / 8 |
210 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildForm | |
0.00% |
0 / 14 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| validateForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| alterElement | |
0.00% |
0 / 5 |
|
0.00% |
0 / 5 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder\Plugin\display_builder\Island; |
| 6 | |
| 7 | use Drupal\Core\Executable\ExecutableManagerInterface; |
| 8 | use Drupal\Core\Form\FormStateInterface; |
| 9 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 10 | use Drupal\display_builder\Attribute\Island; |
| 11 | use Drupal\display_builder\IslandPluginBase; |
| 12 | use Drupal\display_builder\IslandType; |
| 13 | use Drupal\display_builder\IslandWithFormInterface; |
| 14 | use Drupal\display_builder\IslandWithFormTrait; |
| 15 | use Drupal\display_builder\RenderableAltererInterface; |
| 16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 17 | |
| 18 | /** |
| 19 | * Skins island plugin implementation. |
| 20 | */ |
| 21 | #[Island( |
| 22 | id: 'visibility_conditions', |
| 23 | label: new TranslatableMarkup('Visibility'), |
| 24 | description: new TranslatableMarkup('Set visibility conditions for the active component or block.'), |
| 25 | type: IslandType::Contextual, |
| 26 | )] |
| 27 | class VisibilityConditionsPanel extends IslandPluginBase implements IslandWithFormInterface, RenderableAltererInterface { |
| 28 | |
| 29 | use IslandWithFormTrait; |
| 30 | |
| 31 | /** |
| 32 | * The condition manager. |
| 33 | */ |
| 34 | protected ExecutableManagerInterface $conditionManager; |
| 35 | |
| 36 | /** |
| 37 | * {@inheritdoc} |
| 38 | */ |
| 39 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 40 | $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); |
| 41 | $instance->conditionManager = $container->get('plugin.manager.condition'); |
| 42 | |
| 43 | return $instance; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritdoc} |
| 48 | */ |
| 49 | public function buildForm(array &$form, FormStateInterface $form_state): void { |
| 50 | $form['#tree'] = TRUE; |
| 51 | |
| 52 | $data = $form_state->getBuildInfo()['args'][0]; |
| 53 | $instance = $data['instance'] ?? []; |
| 54 | $conditions = $this->conditionManager->getDefinitions(); |
| 55 | unset($conditions['response_status']); |
| 56 | |
| 57 | foreach ($conditions as $condition_id => $definition) { |
| 58 | if (\str_starts_with($condition_id, 'entity_bundle:')) { |
| 59 | continue; |
| 60 | } |
| 61 | /** @var \Drupal\Core\Condition\ConditionInterface $condition */ |
| 62 | $condition = $this->conditionManager->createInstance($condition_id, $instance[$condition_id] ?? []); |
| 63 | $form_state->set(['conditions', $condition_id], $condition); |
| 64 | $condition_form = $condition->buildConfigurationForm([], $form_state); |
| 65 | $condition_form['#type'] = 'details'; |
| 66 | $condition_form['#title'] = (\is_array($definition) && isset($definition['label'])) ? $definition['label'] : ''; |
| 67 | $form[$condition_id] = $condition_form; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function validateForm(array &$form, FormStateInterface $form_state): void { |
| 75 | // Those two lines are necessary to prevent the form from being rebuilt. |
| 76 | // if rebuilt, the form state values will have both the computed ones |
| 77 | // and the raw ones (wrapper key and values). |
| 78 | $form_state->setRebuild(FALSE); |
| 79 | $form_state->setExecuted(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * {@inheritdoc} |
| 84 | */ |
| 85 | public function alterElement(array $element, array $data = []): array { |
| 86 | foreach (\array_keys($data) as $condition_id) { |
| 87 | /** @var \Drupal\Core\Condition\ConditionInterface $condition */ |
| 88 | $condition = $this->conditionManager->createInstance($condition_id, $data[$condition_id] ?? []); |
| 89 | |
| 90 | if (!$this->conditionManager->execute($condition)) { |
| 91 | return []; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return $element; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * {@inheritdoc} |
| 100 | */ |
| 101 | public function onAttachToRoot(string $builder_id, string $instance_id): array { |
| 102 | return $this->reloadWithInstanceData($builder_id, $instance_id); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * {@inheritdoc} |
| 107 | */ |
| 108 | public function onAttachToSlot(string $builder_id, string $instance_id, string $parent_id): array { |
| 109 | return $this->reloadWithInstanceData($builder_id, $instance_id); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritdoc} |
| 114 | */ |
| 115 | public function onActive(string $builder_id, array $data): array { |
| 116 | return $this->reloadWithLocalData($builder_id, $data); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * {@inheritdoc} |
| 121 | */ |
| 122 | public function onDelete(string $builder_id, string $parent_id): array { |
| 123 | return $this->reloadWithLocalData($builder_id, []); |
| 124 | } |
| 125 | |
| 126 | } |