1

Here's the whole picture:

I'm trying to make an API endpoint which should output everything from an entry:

First I did this:

    public function transform(Entry $entry)
    {

        return $entry->getAttributes();
    }

That outputs every field on the entry. But, in the fields containing related items like categories or header_images, In the response I got,looks like the query without being executed:

{
    ...
    "from": null,
    "groupBy": null,
    "join": [
        [
            "INNER JOIN",
            "{{%relations}} relations", [
                "and",
                "[[relations.targetId]] = [[elements.id]]",
                {
                    "relations.sourceId": "35",
                    "relations.fieldId": "15"
                },
                [
                    "or",
                    {
                        "relations.sourceSiteId": null
                    },
                    {
                        "relations.sourceSiteId": "4"
                    }
                ]
            ]
        ]
    ]
    ...
}

I found out that doing ->all(), I get the correct response.

    public function transform(Entry $entry)
    {
        $attributes = $entry->getAttributes();

        $attributes['categories'] = $entry->categories->all();

        $attributes['header_image'] = $entry->header_image->all();

        foreach ($attributes['header_image'] as $key => $image) {
            $attributes['header_image'][$key] = $image->toArray();
            $attributes['header_image'][$key]['folder'] = $image->getVolume();
        }

        return $attributes;
    }

But of course that limits me cause I have to know the field handle (header_image).

First: is there a smart/better way to get the related?

The idea I got to solve this is: Loop through all the fields, and if the type is Asset or Category, do the ->all(). But I cannot get the type of the field. I'm trying stuff like:

        $fields = $entry->getFieldLayout()->getFields();

        foreach ($fields as $field) {
            $field->getContentColumnType(); //Or something
        }

But well, doesn't really work.

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.