Home > OS >  TYPO3 V11 : "PHP Warning: Undefined array key" , $this->request->getArguments() is e
TYPO3 V11 : "PHP Warning: Undefined array key" , $this->request->getArguments() is e

Time:07-13

I'm a new user of typo3 and I made a plugin to display users with a searchbar to filter them, but I have this error when I want to display my page :

(1/1) #1476107295 TYPO3\CMS\Core\Error\Exception
PHP Warning: Undefined array key "word" in MyPath/Controller/UserlistController.php line 44

In my controller, I try to get arguments to use it in my filter like this :

   public function listAction(int $currentPage = 1)
{
   $arguments = $this->request->getArguments();
   $users = $this->userlistRepository->findBySearch($arguments['word'] ? $arguments['word'] : '');

somecode ...

}

I tried a dump of $arguments, but it's empty

there is a part of my repository :

/**
     * @param string $word
     * @return object[]|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
     */
    public function findBySearch(string $word) {
        $query = $this->persistenceManager->createQueryForType(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser::class);
        $querySettings = $query->getQuerySettings();
        $querySettings->setStoragePageIds([26]);
        $query->setQuerySettings($querySettings);

        $query->setOrderings([
            'lastName' => QueryInterface::ORDER_ASCENDING
        ]);

Someone know why I can't get arguments ? thanks

If you need more part of code, please tell me

CodePudding user response:

If you just call the List action without sending the filter form the arguments are empty.

You should test each expected argument before accessing it like this:

if($this->request->hasArgument('word')) {
        $searchOption = $this->request->getArgument('word'));
}

CodePudding user response:

You could try using the null coalescencing operator here:

$users = $this->userlistRepository->findBySearch($arguments['word'] ?? '');

If that doesn't do the trick, you check if the value is set before (see the answer of Tobias Gaertner). Also keep in mind that

$this->request->getArguments();

only gets the arguments in your current plugin context, so any parameters looking like tx_yourplugin_p1[argument]. You have to make sure you properly pass this parameter. If you have to use the general GET parameters, you can use

\TYPO3\CMS\Core\Utility\GeneralUtility::_GET();

CodePudding user response:

While the handling of the 'word' variable is one issue, I consider that if no search word is sent, perhaps another Repository-Method should be called. Below I entered $this->userlistRepository->findAll() but it could also show something else, perhaps just a template that states that a choice is required.

public function listAction(int $currentPage = 1)
{
   $arguments = $this->request->getArguments();
   if($this->request->hasArgument('word')) {
       $searchOption = $this->request->getArgument('word');
       $users = $this->userlistRepository->findBySearch($searchOption);
   }
   else {
      $users = $this->userlistRepository->findAll();
      somecode ...
   }
}
  • Related