spicywebau / craft-embedded-assets

Manage YouTube videos, Instagram photos and more as first class assets in Craft CMS
MIT License
171 stars 35 forks source link

How to use in module/plugin #236

Closed espensgr closed 1 year ago

espensgr commented 1 year ago

What question would you like to ask?

I'm trying to add an api endpoint to Craft and want to get the same info in that as in the Twig template:

{% set embeddedAsset = craft.embeddedAssets.get(asset) %}
{{ embeddedAsset ? embeddedAsset.html }}

Is that possible and how to do that? Maybe add a section to det docs regarding this

ttempleton commented 1 year ago

That can be achieved with PHP using something like the following:

use spicyweb\embeddedassets\Plugin as EmbeddedAssets;

$embeddedAsset = EmbeddedAssets::$plugin->methods->getEmbeddedAsset($asset);
$html = $embeddedAsset ? $embeddedAsset->html : null;