src/Controller.php
public function actionPreview(): Response
	{
		$assetsService = Craft::$app->getAssets();
		$requestService = Craft::$app->getRequest();
		$viewService = Craft::$app->getView();
		$viewService->registerAssetBundle(PreviewAsset::class);

		$url = $requestService->getParam('url');
		$assetId = $requestService->getParam('assetId');
		$callback = $requestService->getParam('callback');
		$showContent = (bool)$requestService->getParam('showContent', true);

		if ($url)
		{
			$embeddedAsset = EmbeddedAssets::$plugin->methods->requestUrl($url);
		}
		else if ($assetId)
		{
			$asset = $assetsService->getAssetById($assetId);

			if (!$asset)
			{
				throw new BadRequestHttpException("Could not find asset with ID: $assetId");
			}

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

			if (!$embeddedAsset)
			{
				throw new BadRequestHttpException("Could not find embedded asset from asset $assetId ($asset->filename)");
			}
		}
		else
		{
			throw new BadRequestHttpException("URL or asset ID are missing from the request");
		}

		$template = $viewService->renderTemplate('embeddedassets/_preview', [
			'embeddedAsset' => $embeddedAsset,
			'callback' => $callback,
			'showContent' => $showContent,
		]);

		$response = $this->asRaw($template);
		$headers = $response->getHeaders();
		$headers->set('content-type', 'text/html; charset=utf-8');

		return $response;
	}
public function actionPreview(): Response
	{
		$assetsService = Craft::$app->getAssets();
		$requestService = Craft::$app->getRequest();
		$viewService = Craft::$app->getView();
		$viewService->registerAssetBundle(PreviewAsset::class);

		$url = $requestService->getParam('url');
		$assetId = $requestService->getParam('assetId');
		$callback = $requestService->getParam('callback');
		$showContent = (bool)$requestService->getParam('showContent', true);

		if ($url)
		{
			$embeddedAsset = EmbeddedAssets::$plugin->methods->requestUrl($url);
		}
		else if ($assetId)
		{
			$asset = $assetsService->getAssetById($assetId);

			if (!$asset)
			{
				throw new BadRequestHttpException("Could not find asset with ID: $assetId");
			}

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

			if (!$embeddedAsset)
			{
				throw new BadRequestHttpException("Could not find embedded asset from asset $assetId ($asset->filename)");
			}
		}
		else
		{
			throw new BadRequestHttpException("URL or asset ID are missing from the request");
		}

		$template = $viewService->renderTemplate('embeddedassets/_preview', [
			'embeddedAsset' => $embeddedAsset,
			'callback' => $callback,
			'showContent' => $showContent,
		]);

		$response = $this->asRaw($template);
		$headers = $response->getHeaders();
		$headers->set('content-type', 'text/html; charset=utf-8');

		return $response;
	}
public function actionPreview ( ) : Response { $ assetsService = Craft :: $ app -> getAssets ( ) ; $ requestService = Craft :: $ app -> getRequest ( ) ; $ viewService = Craft :: $ app -> getView ( ) ; $ viewService -> registerAssetBundle ( PreviewAsset :: class ) ; $ url = $ requestService -> getParam ( 'url' ) ; $ assetId = $ requestService -> getParam ( 'assetId' ) ; $ callback = $ requestService -> getParam ( 'callback' ) ; $ showContent = ( bool ) $ requestService -> getParam ( 'showContent' , true ) ; if ( $ url ) { $ embeddedAsset = EmbeddedAssets :: $ plugin -> methods -> requestUrl ( $ url ) ; } else if ( $ assetId ) { $ asset = $ assetsService -> getAssetById ( $ assetId ) ; if ( ! $ asset ) { throw new BadRequestHttpException ( "Could not find asset with ID: $assetId" ) ; } $ embeddedAsset = EmbeddedAssets :: $ plugin -> methods -> getEmbeddedAsset ( $ asset ) ; if ( ! $ embeddedAsset ) { throw new BadRequestHttpException ( "Could not find embedded asset from asset $assetId ($asset->filename)" ) ; } } else { throw new BadRequestHttpException ( "URL or asset ID are missing from the request" ) ; } $ template = $ viewService -> renderTemplate ( 'embeddedassets/_preview' , [ 'embeddedAsset' => $ embeddedAsset , 'callback' => $ callback , 'showContent' => $ showContent , ] ) ; $ response = $ this -> asRaw ( $ template ) ; $ headers = $ response -> getHeaders ( ) ; $ headers -> set ( 'content-type' , 'text/html; charset=utf-8' ) ; return $ response ; }
Renders a preview of the embedded asset.

@query string url The URL to create an embedded asset from.
@query string assetId The asset ID to load the embedded asset from.
@query string callback The name of a global Javascript function to be called when the preview is loaded.
@response HTML

@return Response
@throws BadRequestHttpException
@throws \Twig_Error_Loader
@throws \yii\base\Exception
@throws \yii\base\InvalidConfigException
Renders a preview of the embedded asset .