0

I have a form with submit buttons save, save and close, save and view, and save and add, as usual in TYPO3. Each button is a <input type='image'> item, and the only difference is the name argument of the input. In my controller, how can I determine which submit button was clicked, in order to redirect to the right action?

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124

2 Answers2

0

You should not redirect to actions from the controller. It's better to call the correct action when a certain button is clicked. To keep the logic clear, you could use the f:link.action viewhelper for instance. Here is a good documentation about the viewhelpers: ViewHelper Reference. You can set an action and a controller attribute to this viewhelper. So no need to decide which button was clicked in the controller. To keep and pass your form data you should use the f:form ViewHelpers and write the form data onto an object.

Wipster
  • 1,510
  • 1
  • 15
  • 32
  • My form is submitted to the 'create' or the 'update' action. From that action, I need to redirect to 'list', or 'new' action, depending on which submit button was clicked. – Charles Brunet Jun 27 '13 at 17:00
0

The first problem I had was the name of the <input> that wasn't right. In order to get the right name, I have to build the tag using a ViewHelper derived from AbstractFormFieldViewHelper.

Now that the <input> tag is rendered the right way, I can see the name of the clicked <input> into $this->request->arguments.

For completeness, here is the code of the ViewHelper I used:

class IconSubmitViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper {

    /**
     * @var string
     */
    protected $tagName = 'input';

    /**
     * Initialize the arguments.
     *
     * @return void
     * @api
     */
    public function initializeArguments() {
        parent::initializeArguments();
        $this->registerArgument('icon', 'string', 'Icon name', true, 'actions-document-close');
        $this->registerTagAttribute('src', 'string', 'Image source', false, 'clear.gif');
        $this->registerUniversalTagAttributes();
    }

    /**
     * Renders an icon link as known from the TYPO3 backend
     *
     * @return string the rendered icon link
     */
    public function render() {
        $name = $this->getName();
        $this->registerFieldNameForFormTokenGeneration($name);

        $this->tag->addAttribute('type', 'image');
        $this->tag->addAttribute('name', $name);
        $this->tag->addAttribute('class', 'c-inputButton');

        return \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon($this->arguments['icon'], array('title' => $this->arguments['title'], 'html' => $this->tag->render()));
    }
}

And here is the code in the Controller that redirect to the right page:

private function submitRedirect($myobject) {
    if ($this->request->hasArgument('_savedok')) {
        $this->redirect('edit', NULL, NULL, array('myobject'=>$myobject));
    }
    if ($this->request->hasArgument('_savedokclose')) {
        $this->redirect('list');
    }
    if ($this->request->hasArgument('_savedoknew')) {
        $this->redirect('new');
    }
}
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124