1

I’m experimenting with the Element API plugin and working with querying the tags of the assets that appear in matrix blocks in a set of entries. Essentially I’m hoping to be able to use a tag in a query string to return a list of entries where any asset with that tag appears. I’ve gotten as far as returning all of the information I need, but I’m having trouble figuring out how (or whether I can?) use a search function GET request to retrieve the list of entries I need.

Here is where I am:

'endpoints' => [
    'api/projects.json' => [
        'elementType' => Entry::class,
        'pretty' => true,
        'criteria' => [
            'section' => 'projects',
            'type' => 'projectDetail',
        ],
        'transformer' => function(Entry $entry) {
            $relatedAssets = [];
            foreach ($entry->projectDetailMatrix as $block) {
                $image = '';
                $id = '';
                $tags = [];
                $asset = $block->imageSelection[0];
                if (!is_null($block->imageSelection)) {
                    $image = $asset->getUrl();
                    $id = $asset->id;
                    foreach ($asset->tags as $tag) {
                        $tags[] = $tag->slug;
                    }
                    $relatedAssets[] = [
                        'assetUrl' => $image,
                        'assetId' => $id,
                        'assetTags' => $tags
                    ];
                }
            }
            return [
                'title' => $entry->title,
                'url' => $entry->url,
                'relatedAssets' => $relatedAssets,
            ];
        },
    ],
]

And here's what I get:

{
    "data": [
        {
            "title": "Project1",
            "url": "http://site.dev/project1",
            "relatedAssets": [
                {
                    "assetUrl": "//site.dev/uploads/image1.png",
                    "assetId": "12",
                    "assetTags": [
                        "red",
                        "dark"
                    ]
                },
                {
                    "assetUrl": "//site.dev/uploads/image2.png",
                    "assetId": "13",
                    "assetTags": [
                        "red"
                    ]
                }
            ]
        },
        {
            "title": "Project2",
            "url": "http://site.dev/project2",
            "relatedAssets": [
                {
                    "assetUrl": "//site.dev/uploads/image3.jpg",
                    "assetId": "14",
                    "assetTags": [
                        "blue",
                        "dark"
                    ]
                },
            ]
        }
    ]
}

So my end goal would be

/api/projects.json?t=dark

returning the Project entries whose matrix content includes an asset with the tag "dark". Is this possible, or too much of a rabbit hole at this point? Any pointers appreciated!

1
  • How many assets with this tag will you likely have? 100? 1000? 10.000? If it's only a "few" you could grab all assets with the tag $assetIds = Asset::find()->relatedTo($tag)->ids() and then grab all entries that are related to this asset within a matrix element Jun 20, 2018 at 20:13

0

Your Answer

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