silverstripe-gridfield-betterbuttons icon indicating copy to clipboard operation
silverstripe-gridfield-betterbuttons copied to clipboard

Redirect to DataObject edit page inside BetterButton custom action

Open g4b0 opened this issue 10 years ago • 3 comments

I'm trying to create a custom button that clones a DataObject. Everything is working fine but at the end I would like to redirect the user to the newly created DataObject edit page instead of refresh/go back. Like stated here it's not possible.

Here is my custom button code:

class GridFieldCloneBetterButton extends DataExtension {

    private static $better_buttons_actions = array(
        'clone_do'
    );

    public function updateBetterButtonsActions($actions) {
        $actions->push(
            BetterButtonCustomAction::create('clone_do', 'Clone')
                ->setSuccessMessage('Object cloned')
                ->setRedirectType(BetterButtonCustomAction::GOBACK)
        );
        return $actions;
    }

    public function clone_do() {
        $current_record = $this->owner;
        $clone = $this->owner->duplicate();
    }

}

g4b0 avatar Nov 12 '14 13:11 g4b0

Can you please try the new setRedirectURL() method in c364d1fbc11a0438551499e12f8f7412013225b6 ?

Let me know how that works and I'll tag a release.

unclecheese avatar Nov 12 '14 22:11 unclecheese

First of all thanks for your interest!

https://github.com/unclecheese/silverstripe-gridfield-betterbuttons/commit/c364d1fbc11a0438551499e12f8f7412013225b6 is just half of the solution to the problem, because in order to redirect to a DO edit page I need to access GridFieldDetailForm_ItemRequest instance, that is protected in BetterButtonAction.

Maybe I don't know the right way to do this redirect? How would you do the redirect into the function clone_do?

g4b0 avatar Nov 13 '14 08:11 g4b0

Hi, @g4b0, I've implemented an API change on the master branch that will allow you to do what you're looking for.

<?php

class MyDataObject extends DataObject {

    private static $better_buttons_actions = array (
        'replicate'
    );

    public function replicate($action, $controller, $request) {
        $new = $this->duplicate();
        $new->write();

        $action->setRedirectURL(
            $controller->getEditLink($new->ID)
        );

        return "Duplicated!";
    }

}

Keep in mind this is all unreleased, so use at your own risk. It implements several API changes, so this will end up going on the 1.3 release.

unclecheese avatar May 07 '15 22:05 unclecheese