Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix File URL Type widget unable to determine item type #3591

Merged
merged 3 commits into from
Aug 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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