2

I am currently trying to create an autocomplete search with the element-api. I need results from two different elementTypes:

  • Product::class
  • Entry::class

How do I get results out of both elementTypes with only one endpoint?

Thats my working Product::class element-api.php config file:

return [
    'endpoints' => [
        'search.json' => function() {
        // settings
        $phrase = Craft::$app->request->getParam('query');

        $criteria = [
            'search' => 'title:'.$phrase,
        ];

            return [
                'elementType' => Product::class,
                'criteria' => $criteria,
                'paginate' => false,
                'transformer' => function(Product $product) {
                return [
                    'title' => $product->title,
                    'url' => $product->url,
                ];
            },
          ];

        },
    ]
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.