-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for internal links in forms
- Loading branch information
Dominic Tubach
committed
Aug 5, 2024
1 parent
9147a12
commit 36c4922
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
modules/civiremote_entity/src/Form/Control/InternalLinkArrayFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (C) 2024 SYSTOPIA GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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' => '<p>', | ||
'#suffix' => '</p>', | ||
'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(); | ||
} | ||
|
||
} |