diff --git a/modules/json_form_widget/src/Element/UploadOrLink.php b/modules/json_form_widget/src/Element/UploadOrLink.php index 69caa0e91d..347eb9b44e 100644 --- a/modules/json_form_widget/src/Element/UploadOrLink.php +++ b/modules/json_form_widget/src/Element/UploadOrLink.php @@ -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; } @@ -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']; @@ -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; } /** @@ -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'])) {