TransWikia.com

Using multiple elementTypes in Element API

Craft CMS Asked on October 4, 2021

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,
                ];
            },
          ];

        },
    ]
];

One Answer

The results query could not be the same, you have to separate them.

use craftcommerceelementsProduct;
use craftelementsEntry;

return [
    'endpoints' => [
        'search/product.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,
                    ];
                },
            ];
        },
        'search/news.json' => function () {
            // settings
            $phrase   = Craft::$app->request->getParam('query');
            $criteria = [
                'search' => 'title:' . $phrase,
            ];

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

UPDATED: 18/12/2020

Anyway, I've found a new way that allows you o use the multiple entries in an endpoint.

<?php

use craftcommerceelementsProduct;
use craftelementsEntry;

return [
    'endpoints' => [
        'search.json' => function () {
            // settings
            $phrase         = Craft::$app->request->getParam('query');
            $articles       = Entry::find()->search($phrase)->limit(10)->all();
            $articleObjects = [];

            if ($articles) {
                $articleTemplateName = ''; // your article template path
                foreach ($articles as $article) {
                    $articleObjects[] = Craft::$app->getView()->renderTemplate($articleTemplateName, ['post' => $article]);
                }
            }

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

Answered by Mostafa Soufi on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP