From 1a5ad6f796beff4e3bf197441cb06023e473351b Mon Sep 17 00:00:00 2001 From: Clayton Liddell Date: Wed, 28 Jul 2021 15:27:50 -0500 Subject: [PATCH 1/3] Fix File URL Type widget unable to determine item type. --- .../src/Element/UploadOrLink.php | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/modules/json_form_widget/src/Element/UploadOrLink.php b/modules/json_form_widget/src/Element/UploadOrLink.php index 69caa0e91d..fe86b4fe8c 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 || !empty($element['#value']['fids'])) { parent::validateManagedFile($element, $form_state, $complete_form); if ($element_parents = $form_state->get('upload_or_link_element')) { $element_parents[] = $element['#parents']; @@ -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'])) { From cd337e8f8b68e1ef116bc3b2041e930d559bac10 Mon Sep 17 00:00:00 2001 From: Clayton Liddell Date: Fri, 30 Jul 2021 10:02:10 -0500 Subject: [PATCH 2/3] Fix File URL Type widget unable to determine item type. --- modules/json_form_widget/src/Element/UploadOrLink.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/json_form_widget/src/Element/UploadOrLink.php b/modules/json_form_widget/src/Element/UploadOrLink.php index fe86b4fe8c..65a67af50b 100644 --- a/modules/json_form_widget/src/Element/UploadOrLink.php +++ b/modules/json_form_widget/src/Element/UploadOrLink.php @@ -229,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 (static::getUrlType($element) === 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']; @@ -249,6 +249,9 @@ protected static function getUrlType($element) { if (isset($element['#value']['file_url_type'])) { return $element['#value']['file_url_type']; } + elseif (!empty($element['#value']['fids'])) { + return static::TYPE_UPLOAD; + } elseif (isset($element['#uri'])) { if (static::checkIfLocalFile($element['#uri'])) { return static::TYPE_UPLOAD; From 03bd7e5973504564f2987325689f85f7c8c5d08c Mon Sep 17 00:00:00 2001 From: Clayton Liddell Date: Fri, 30 Jul 2021 10:14:00 -0500 Subject: [PATCH 3/3] Satisfying CodeClimate. --- .../json_form_widget/src/Element/UploadOrLink.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/modules/json_form_widget/src/Element/UploadOrLink.php b/modules/json_form_widget/src/Element/UploadOrLink.php index 65a67af50b..347eb9b44e 100644 --- a/modules/json_form_widget/src/Element/UploadOrLink.php +++ b/modules/json_form_widget/src/Element/UploadOrLink.php @@ -246,21 +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'])) { - return static::TYPE_UPLOAD; + $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; } /**