2

We are building a React application for a client, where we are using Craft as the API/Backend. I am building the API as a custom Craft plugin, which will return specific JSON data to React, dependent upon the action route it requests.

I was wondering if there was a way that anyone has managed to prevent the default Entry properties from being included in the response, as it just adds a lot of unnecessary clutter. I have included a visual example at the bottom of this question to further clarify what I mean.

I did try to research if this was answered already, and found this link Ignore fields in search index I did see it was back in 2016, so was hoping something has changed.

Example:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = "journeys";
$journeys = $criteria->find();

$responseObj = new \stdClass;
$responseObj->journeyOne = $journeys[0];
$this->returnJson($responseObj);

enter image description here

I literally won't be using any of these, as I only require the data from my custom fields(which haven't been created yet).

4

Unfortunately this is one of Crafts disadvantages, you'll always fetch all fields in your craft_content table no matter if you need those or not, thus it's always a bit slow and query heavy.

Craft 3 introduces a way to solve that issue since it loops the field layout and only fetches the values you need and not all of them.

Anyway: you could do this:

// get an array of all fields in craft_content (note: that only works for fields
// with a record in `craft_content` so you won't fetch relations with that
$attributes = $journeys[0]->getContent()->getAttributes();
$this->returnJson([
    'title'                 => $journeys[0]->getTitle(),
    'myCustomField'         => $attributes['myCustomField'],
    'myAwesomeField'        => $attributes['myAwesomeField'],
    'anotherField'          => $attributes['anotherField'],
    'idsOfARelationField'   => $journeys[0]->relationFieldHandle->ids()
]);

That way you can define which values you need and return them in an array. If you don't need all the other values just leave them empty. If you only want to fetch all custom fields you can use

$this->returnJson($attributes);

Not sure if you'll receive the title that way, you need to try. Othewise - if you need the title and if it's not included

$attributes['title'] = $journes[0]->getTitle();
$this->returnJson($attributes);

solves that.

Edit: by the way if you only need the first element you can do $criteria->first() instead of $criteria->find();

  • I like your idea of just picking off which pieces you need, and constructing your own response, which is exactly what I am going to do. As for the first() reference, there are going to be multiple entries, I just used [0] as an example in my code. Thanks again. – Dan Zuzevich Jan 25 '18 at 16:10
  • @DanielZuzevich you could also look into implementing Fractal fractal.thephpleague.com, which the Element API plugin also uses – carlcs Jan 25 '18 at 16:13
  • Not a bad idea. If I only had the time lol. – Dan Zuzevich Jan 25 '18 at 16:16

Your Answer

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

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