I am having trouble getting the related category passed thru to the entry api endpoint.

I cant use :

   $criteria = craft()->elements->getCriteria(ElementType::Category);
        $criteria->id = $catId;
        $category = $criteria->first();

What is the equivalent in craft 3 api v2?

'api/v1/ship/category/<catId:\d+>.json' => function($catId) {
//old api craft 2
        $criteria = craft()->elements->getCriteria(ElementType::Category);
        $criteria->id = $catId;
        $category = $criteria->first();

//api craft 3
        return [
            'elementType' => Entry::class,
            'elementsPerPage' => 30,
            'criteria' => [
                'criteria' => ['section' => 'ship'],
                'relatedTo' => ['targetElement' => $category],
            ],
            'transformer' => function(Entry $entry) {
                return [
                    'title' => $entry->title,
                    'id' => (int) $entry->id,
                    'slug' => $entry->slug,
                    'postDate' => $entry->postDate
                ];
            },
            'pretty' => true
        ];
    },
  • Would you mind adding your solution as an official answer @joomkit? – Brad Bell Nov 17 at 1:17
up vote 0 down vote accepted

Solved it like this:

//categories
    'api/v1/ship/category/<catId:\d+>.json' => function($catId) {
        return [
            'elementType' => Category::class,
            'criteria'    => ['id'   => $catId],
            'paginate'    => false,
            'transformer' => function(Category $category) {
                $entries = Entry::find()->relatedTo($category)->all();

                $en=[];
                foreach($entries as $entry) {


                    //Final return
                    $en[]= [
                        "type" => "shipItem",
                        'sendtoseafarerhelp' => $entry->sendToSeafarerHelp,
                        'title' => $entry->title,
                        'url' => $entry->url,
                        'categoryTitle' => $entry->shipCategory[0]->title,
                        'categorySlug' => $entry->shipCategory[0]->slug,
                        'categoryUrl' => $entry->shipCategory[0]->uri,
                        'summary' => $entry->shipIntroText,
                        'descripton' => $entry->shipDescription,
                        'pdfs' => $pdfs,
                        'thumbs' => $thumbs,
                        'sponsors' => $sponsors                            
                    ];
                }

                return [
                    'title'   => $category->title,
                    'entries' => $en
                ];
            }
        ];
    },

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.