Home > Blockchain >  Typo3 Rendering FieldControl 'Too few arguments'
Typo3 Rendering FieldControl 'Too few arguments'

Time:07-28

I've build a Typo3 plugin for a retailer/store database. Every store should have latitude and longitude. So that I don't have to enter every latitude and longitude manually I added a button (with fieldcontrol in TCA) to enter the data automatically. It's working great when you first save the store and then retrieve the location data. But it would be more convenient to do it before saving. Therefore I wanted to trigger a data import via ajax. I found this example in the Typo3 docs: https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/FormEngine/Rendering/Index.html#add-fieldcontrol-example

Now I'm facing a problem that apparently concers the ajax controller. I get the following error: 'Too few arguments to function Vendor\MyExt\Controller\Ajax\LocateAddressController::locateAddressAction(), 1 passed in /.../typo3/sysext/backend/Classes/Http/RouteDispatcher.php on line 91 and exactly 2 expected'

Here is an excerpt of my code: LocateAddress.js:

/**
   * @param {int} id
   */
   LocateAddress.import = function (id) {
      $.ajax({
         type: 'POST',
         url: TYPO3.settings.ajaxUrls['locate-address'],
         data: {
            'id': id
         }
      }).done(function (response) {
         if (response.success) {
            top.TYPO3.Notification.success('Import Done', response.output);
         } else {
            top.TYPO3.Notification.error('Import Error!');
         }
      });
   };

AjaxRoutes.php:

return [
   'locate-address' => [
      'path' => '/locate-address',
      'target' => \Vendor\MyExt\Controller\Ajax\LocateAddressController::class . '::locateAddressAction'
   ],
];

LocateAddressController.php:

use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;

    class LocateAddressController
    {
        /**
        * @param ServerRequestInterface $request
        * @param ResponseInterface $response
        * @return ResponseInterface
        */
        public function locateAddressAction(ServerRequestInterface $request, ResponseInterface $response)
        {
            $queryParameters = $request->getParsedBody();

            $response->getBody()->write(json_encode(['success' => true]));
            return $response;
        }

    }

CodePudding user response:

The 2nd argument for backend controllers (ResponseInterface $response) has been deprecated with TYPO3 9 (see documentation) and removed with TYPO3 v10. The example in the extbase documentation seems to be outdated.

Simply remove the 2nd argument and create the response object in your method to return it.

public function locateAddressAction(ServerRequestInterface $request)
{
    ...
    return new JsonResponse(['success' => true]));
}
  • Related