Select first object when forwarding in a TYPO3 extension

708 views Asked by At

Currently, my code looks like this:

public function listAction() {
    $this->forward('show', NULL, NULL, array('myArgument' => 14));
}

It's forwarding the user directly to the 'show'-view. It's always forwarding to the object with the UID 14. When the user deletes this object, an error will occur.

The code is supposed to forward the user to the first element in the list. Is there something like forward('show', NULL, NULL, array('myArgument' => first))?

Using 'show' as default action in the ext_localconf.php causes an error as well because no arguments are defined, I guess.

2

There are 2 answers

0
rob-ot On BEST ANSWER
public function listAction() {
    $firstObject = $this->myobjectRepository->findAll()->getFirst(); // just pseudocode

    $this->forward('show', NULL, NULL, array('myArgument' => $firstObject->getUid()));
}

With this you will send the uid of your first object to the other method (for ex: show), thus you will need to get the object back with a call to your repository (a findByUid for example)

You can also use correct annotation to your show method so that it will treat your argument as an object

/**
     * action show
     *
     * @param \vendor\ext\Domain\Model\ModelName $modelName
     *
     * @return void
     */
    public function showAction(\vendor\ext\Domain\Model\ModelName $modelName) {
        $this->view->assign('modelName', $modelName);
    }
0
Marek Skopal On

There isn't anythink like that, but is there any reason why you can't get UID by database?

public function listAction() {
    $firstObject = $this->myobjectRepository->findFirstObject(); // just pseudocode

    $this->forward('show', NULL, NULL, array('myArgument' => $firstObject->getUid()));
}