Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Usage with Element API #1

Closed
umkasanki opened this issue Nov 5, 2020 · 3 comments
Closed

[Question] Usage with Element API #1

umkasanki opened this issue Nov 5, 2020 · 3 comments

Comments

@umkasanki
Copy link

Hey.
Could you please show an example of how to use your plugin with the Element API plugin?
Thanks

@croxton
Copy link
Owner

croxton commented Nov 7, 2020

Not forgotten this, just been super busy. I'll try to find time to add an example next week.

@croxton
Copy link
Owner

croxton commented Nov 14, 2020

Sorry for delay! Here's an example of using Imgixer with the Element API, assuming your image field is named 'image'. Basically, pass the image path as the first argument to the function, and an array of Imgix parameters as the second argument.

<?php

use craft\elements\Entry;
use craft\helpers\UrlHelper;
use croxton\imgixer\twigextensions\ImgixerTwigExtension;

return [
    'endpoints' => [
        'news.json' => function() {
            return [
                'elementType' => Entry::class,
                'criteria' => ['section' => 'news'],
                'transformer' => function(Entry $entry) {
                    $asset = $entry->image->one();
                    return [
                        'title' => $entry->title,
                        'url' => $entry->url,
                        'jsonUrl' => UrlHelper::url("news/{$entry->id}.json"),
                        'summary' => $entry->summary,
                        'image'   => (new ImgixerTwigExtension)->imgix($asset->path, array(
                            'ar' => '16:9',
                            'w' => 2000,
                            'signed' => true
                        ))
                    ];
                },
            ];
        }
    ]
];

@croxton croxton pinned this issue Nov 14, 2020
@umkasanki
Copy link
Author

Thanks!

@croxton croxton closed this as completed Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants