Skip to content

Commit

Permalink
Fix File URL Type widget unable to determine item type (#3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clayton Liddell authored Aug 2, 2021
1 parent 898d520 commit 73a398b
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions modules/json_form_widget/src/Element/UploadOrLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,18 @@ private static function loadRemoteFile($element, $file_url_remote) {
* Helper function to load files when local.
*/
private static function loadLocalFilesWhenDefault($element) {
if (empty($element['#value']['fids']) && !empty($element['#uri'])) {
$file = static::checkIfLocalFile($element['#uri']);
if ($file && $element['#value']['file_url_type'] !== static::TYPE_REMOTE) {
$element['#files'][$file->id()] = $file;
$element['#value']['fids'] = [$file->id()];
$element['#value']['file_url_type'] = "upload";
$element['fids']['#type'] = 'hidden';
$element['fids']['#value'] = [$file->id()];
$file_link = [
'#theme' => 'file_link',
'#file' => $file,
];
$element['file_' . $file->id()]['filename'] = $file_link + ['#weight' => -10];
}
if (empty($element['#value']['fids']) && !empty($element['#uri']) &&
$file = static::checkIfLocalFile($element['#uri'])) {
$element['#files'][$file->id()] = $file;
$element['#value']['fids'] = [$file->id()];
$element['#value']['file_url_type'] = static::TYPE_UPLOAD;
$element['fids']['#type'] = 'hidden';
$element['fids']['#value'] = [$file->id()];
$file_link = [
'#theme' => 'file_link',
'#file' => $file,
];
$element['file_' . $file->id()]['filename'] = $file_link + ['#weight' => -10];
}
return $element;
}
Expand Down Expand Up @@ -231,7 +229,7 @@ private static function overrideUploadSubfield($element, $file_url_type_selector
*/
public static function validateManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
$uri = static::getDefaultUri($element, $form_state);
if (($element['#value']['file_url_type'] ?? NULL) === static::TYPE_UPLOAD || !empty($element['#value']['fids'])) {
if (static::getUrlType($element) === static::TYPE_UPLOAD) {
parent::validateManagedFile($element, $form_state, $complete_form);
if ($element_parents = $form_state->get('upload_or_link_element')) {
$element_parents[] = $element['#parents'];
Expand All @@ -248,18 +246,18 @@ public static function validateManagedFile(&$element, FormStateInterface $form_s
* Helper function for getting the url type.
*/
protected static function getUrlType($element) {
$type = NULL;
if (isset($element['#value']['file_url_type'])) {
return $element['#value']['file_url_type'];
$type = $element['#value']['file_url_type'];
}
elseif (!empty($element['#value']['fids'])) {
$type = static::TYPE_UPLOAD;
}
elseif (isset($element['#uri'])) {
if (static::checkIfLocalFile($element['#uri'])) {
return static::TYPE_UPLOAD;
}
else {
return static::TYPE_REMOTE;
}
$type = static::checkIfLocalFile($element['#uri']) ?
static::TYPE_UPLOAD : static::TYPE_REMOTE;
}
return NULL;
return $type;
}

/**
Expand All @@ -272,10 +270,7 @@ protected static function getDefaultUri($element, FormStateInterface $form_state
return '';
}

if ((isset($element['#value']['file_url_type'])
&& $element['#value']['file_url_type'] == static::TYPE_UPLOAD)
|| !empty($element['#value']['fids'])
) {
if (static::getUrlType($element) == static::TYPE_UPLOAD) {
return static::getLocalFileUrl($element);
}
elseif (!empty($element['#value']['file_url_remote'])) {
Expand Down

0 comments on commit 73a398b

Please sign in to comment.