From 626bfd36e20a0cbb7a5a4e24861dc7745475e1c8 Mon Sep 17 00:00:00 2001 From: Dominic Tubach Date: Mon, 5 Aug 2024 15:16:57 +0200 Subject: [PATCH] Add support for internal links in forms --- modules/civiremote_entity/composer.json | 2 +- .../Form/Control/InternalLinkArrayFactory.php | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 modules/civiremote_entity/src/Form/Control/InternalLinkArrayFactory.php diff --git a/modules/civiremote_entity/composer.json b/modules/civiremote_entity/composer.json index a7bd57f..80d89d0 100644 --- a/modules/civiremote_entity/composer.json +++ b/modules/civiremote_entity/composer.json @@ -42,7 +42,7 @@ "require": { "php": "^7.4 || ^8", "beberlei/assert": "*", - "drupal/json_forms": "~0.1" + "drupal/json_forms": "~0.4" }, "require-dev": { "drupal/core-dev": "^9.5 || ^10" diff --git a/modules/civiremote_entity/src/Form/Control/InternalLinkArrayFactory.php b/modules/civiremote_entity/src/Form/Control/InternalLinkArrayFactory.php new file mode 100644 index 0000000..db810d9 --- /dev/null +++ b/modules/civiremote_entity/src/Form/Control/InternalLinkArrayFactory.php @@ -0,0 +1,86 @@ +. + */ + +declare(strict_types=1); + +namespace Drupal\civiremote_entity\Form\Control; + +use Assert\Assertion; +use Drupal\civiremote_entity\CiviCRMPage\CiviCRMUrlStorageInterface; +use Drupal\Core\Form\FormStateInterface; +use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory; +use Drupal\json_forms\Form\Control\Rule\StatesArrayFactory; +use Drupal\json_forms\Form\FormArrayFactoryInterface; +use Drupal\json_forms\JsonForms\Definition\DefinitionInterface; + +class InternalLinkArrayFactory extends AbstractConcreteFormArrayFactory { + + private CiviCRMUrlStorageInterface $civiCRMUrlManager; + + public function __construct(CiviCRMUrlStorageInterface $civiCRMUrlManager) { + $this->civiCRMUrlManager = $civiCRMUrlManager; + } + + /** + * {@inheritDoc} + */ + public function createFormArray( + DefinitionInterface $definition, + FormStateInterface $formState, + FormArrayFactoryInterface $formArrayFactory + ): array { + $label = $definition->getKeywordValue('label'); + Assertion::string($label); + $url = $definition->getKeywordValue('url'); + Assertion::string($url); + $description = $definition->getKeywordValue('description'); + Assertion::nullOrString($description); + $filename = $definition->getKeywordValue('filename'); + Assertion::nullOrString($filename); + + if (NULL !== $description) { + $description = ' - ' . $description; + } + + $form = [ + '#type' => 'container', + '#prefix' => '

', + '#suffix' => '

', + 'link' => [ + '#type' => 'link', + '#title' => $label, + '#url' => $this->civiCRMUrlManager->addRemoteUrl($url, $filename), + '#attributes' => ['target' => '_blank'], + ], + 'description' => ['#plain_text' => $description], + ]; + + if (NULL !== $definition->getRule()) { + $statesArrayFactory = new StatesArrayFactory(); + $form['#states'] = $statesArrayFactory->createStatesArray($definition->getRule()); + } + + return $form; + } + + public function supportsDefinition(DefinitionInterface $definition): bool { + return 'InternalLink' === $definition->getType(); + } + +}