Skip to content

Commit

Permalink
FileArrayFactory: Allow default value to be array
Browse files Browse the repository at this point in the history
The default value is an array, if it was fetched from the temporary
values.
  • Loading branch information
Dominic Tubach committed Oct 4, 2024
1 parent 33dc2f0 commit 9d4669c
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions modules/civiremote_entity/src/Form/Control/FileArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,30 @@ public function createFormArray(
] + BasicFormPropertiesFactory::createFieldProperties($definition, $formState),
];

if (($form['file']['#default_value'] ?? NULL) instanceof \stdClass
&& is_string($form['file']['#default_value']->url ?? NULL)
&& is_string($form['file']['#default_value']->filename ?? NULL)
) {
$url = $form['file']['#default_value']->url;
$filename = $form['file']['#default_value']->filename;

$form['file']['#required'] = FALSE;

$form['link'] = [
'#type' => 'link',
'#title' => $filename,
'#url' => $this->civiCRMUrlManager->addRemoteUrl($url, $filename),
'#attributes' => ['target' => '_blank'],
'#prefix' => '<p>',
'#suffix' => '</p>',
];

if (isset($form['file']['#states'])) {
$form['link']['#states'] = $form['file']['#states'];
if (isset($form['file']['#default_value'])) {
// If the default value was fetched from the temporary values, it should
// be an array. If it was fetched from the field definition, it should be
// an \stdClass.
/** @phpstan-var \stdClass|array<string, mixed> $defaultValue */
$defaultValue = $form['file']['#default_value'];
$url = $defaultValue->url ?? $defaultValue['url'] ?? NULL;
$filename = $defaultValue->filename ?? $defaultValue['filename'] ?? NULL;

if (is_string($url) && is_string($filename)) {
$form['file']['#required'] = FALSE;

$form['link'] = [
'#type' => 'link',
'#title' => $filename,
'#url' => $this->civiCRMUrlManager->addRemoteUrl($url, $filename),
'#attributes' => ['target' => '_blank'],
'#prefix' => '<p>',
'#suffix' => '</p>',
];

if (isset($form['file']['#states'])) {
$form['link']['#states'] = $form['file']['#states'];
}
}
}

Expand Down

0 comments on commit 9d4669c

Please sign in to comment.