1

Is there a way to order the Craft Element-API results by the category title?

I'm just getting the error

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

The "hostingCompany" field is a category field.

'hostings.json' => [
        'elementType' => Entry::class,
        'criteria' => [
            'section'   => 'hostings',
            'orderBy' => 'hostingCompany desc'
        ],
        'paginate' => true,
        'transformer' => function(Entry $entry) {

            return [
                'hostingDomain'  => $entry->hostingDomain,
                'hostingCompany' => (string)$entry->hostingCompany[0],
                'url'            => $entry->url,
            ];
        },
    ],

Updated error message:

PHP Warning 'yii\base\ErrorException' with message 'Header may not contain more than a single header, new line detected' 
in /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php:380
Stack trace:
0 /Applications/AMPPS/www/crm.local/vendor/craftcms/cms/src/web/ErrorHandler.php(81): yii\base\ErrorHandler->handleError(2, 'Header may not ...', '/Applications/A...', 380)
1 [internal function]: craft\web\ErrorHandler->handleError(2, 'Header may not ...', '/Applications/A...', 380, Array)
2 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php(380): header('HTTP/1.1 404 SQ...')
3 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
4 /Applications/AMPPS/www/crm.local/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
5 /Applications/AMPPS/www/crm.local/web/index.php(21): yii\base\Application->run()
6 {main}

2 Answers 2

1

This seems more like it may be an error in your code somewhere, do you have dev mode turned on?

I think you may want to output the title of the category:

'hostingCompany' => (string)$entry->hostingCompany[0]->title,

See if that helps, if not I would update your question with your complete element-api.php

4
  • The output of the title is not the problem. It is displayed correctly. I just want to sort the JSON output by this field.
    – Steve
    Commented Aug 23, 2019 at 11:29
  • You’re showing an error that suggests the json is incorrect, if you take your sorting code out are you saying the json outputs fine? (Albeit not in the order you want)
    – dmatthams
    Commented Aug 23, 2019 at 11:32
  • Yes. If i delete the orderBy criteria all just works fine, except the sorting. If i open the JSON url in chrome i get the error above (updated question).
    – Steve
    Commented Aug 23, 2019 at 11:34
  • Still seems like a syntax error where something is outputting a line break, surprised if it was that line but have posted another answer using 'groupBy'
    – dmatthams
    Commented Aug 23, 2019 at 13:23
1

Have you tried grouping by category:

'hostings.json' => [
        'elementType' => Entry::class,
        'criteria' => [
            'section'   => 'hostings',
            'groupBy' => 'hostingCompany[0].title'
        ],
        'paginate' => true,
        'transformer' => function(Entry $entry) {

            return [
                'hostingDomain'  => $entry->hostingDomain,
                'hostingCompany' => (string)$entry->hostingCompany[0],
                'url'            => $entry->url,
            ];
        },
    ],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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