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

"Save and next" / "Save and prev" takes over UploadField-Content #27

Open
MLindenhofer opened this issue Dec 20, 2022 · 4 comments
Open

Comments

@MLindenhofer
Copy link

MLindenhofer commented Dec 20, 2022

Hi,

I'm using CMS 4.11 and 1.3.8 of this module. I've problems with my UploadField in my DataObject-class here:

private static $has_one = [
    'Icon' => File::class
];

private static $owns = [
    'Icon'
];


public function getCMSFields()
{
    $fields = parent::getCMSFields();

    $fields->addFieldsToTab('Root.Main', [
        UploadField::create('Icon', 'Icon or Image')->setAllowedExtensions(['svg', 'png', 'jpg', 'jpeg'])
    ]);

The upload itself works but, if I hit "Save and next" / "Save and prev" or hit the arrows to navigate to the next DataObject of the class, the Icon is taken over as well. Seems to be a bug in my opinion.

Thanks in advance

@lekoala
Copy link
Owner

lekoala commented Dec 20, 2022

hi @MLindenhofer that looks like a bug :-)
but it may be a framework issue and not something that can be fixed easily in this module. Maybe the state is not updated properly (I've seen a lot of these in the issues on the framework) and I remember reading somewhere a similar issue (but didn't manage to find it back)

as you can see, the code for this is rather simple

public function doSaveAndNext($data, $form)
{
$record = $this->owner->record;
$this->owner->doSave($data, $form);
// Redirect after save
$controller = $this->getToplevelController();
$controller->getResponse()->addHeader("X-Pjax", "Content");
$getNextRecordID = $this->getCustomNextRecordID($record);
$class = get_class($record);
$next = $class::get()->byID($getNextRecordID);
$link = $this->owner->getEditLink($getNextRecordID);
// Link to a specific tab if set, see cms-actions.js
if ($next && !empty($data['_activetab'])) {
$link .= sprintf('#%s', $data['_activetab']);
}
return $controller->redirect($link);
}

it's basically a redirect with the next id. This looks like something the framework should take care of. I'll have a look at this when I get some time and see if I can pinpoint the exact cause.

@lekoala
Copy link
Owner

lekoala commented Jan 3, 2023

After reviewing the issue, i think this is linked to this
silverstripe/silverstripe-asset-admin#960

and more specifically this part
https://github.com/silverstripe/silverstripe-asset-admin/blob/487e62c4550fe187f643e1a8226f840b7e9f9260/client/src/components/UploadField/UploadField.js#L67-L79

where the state is hashed. as far as i can tell, the state is correct (empty files array) but the hash state is not updated properly (you also don't see any load event which is why i think this comes from the internal component state)

@lekoala
Copy link
Owner

lekoala commented Jan 3, 2023

@MLindenhofer this basic issue is that the id is the same and therefore is served the same files from the component. One temporary fix is to set a custom id for the field.

For example, you create a subclass of UploadField with this

/**
 * Temp fix
 * @link https://github.com/lekoala/silverstripe-cms-actions/issues/27
 * @return string
 */
public function ID()
{
    return parent::ID() . "-" . random_int(1, PHP_INT_MAX);
}

I don't think there is any side effect to this...

@lekoala
Copy link
Owner

lekoala commented Jan 3, 2023

I've opened a distinct issue here
silverstripe/silverstripe-asset-admin#1315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants