diff --git a/README.md b/README.md index 9632397..bad7015 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,12 @@ After setting up the S3 volume follow these instructions to install the plugin. 4. Add `Toggle aws image processor` to your s3 volume field layout. +## Plugin Settings + +` Serverless Distribution URL ` (optional) + +This field is for the Serverless Image Handler CloudFront Distribution URL, which frees up your asset volume distribution URL to be a regular CloudFront distribution URL. This means that any asset in the volume that does not pass through this plugin (rich text fields, certain SEO plugins, etc) will be served normally. + ## Usage In your twig template you can use: diff --git a/composer.json b/composer.json index d65e3ad..ae37cd6 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "dutchheight/aws-serverless-image-handler", "description": "Craft CMS plugin to generate resource URL", "type": "craft-plugin", - "version": "1.1.3", + "version": "1.1.4", "keywords": [ "craft", "cms", @@ -32,7 +32,7 @@ "extra": { "name": "aws-serverless-image-handler", "handle": "aws-serverless-image-handler", - "hasCpSettings": false, + "hasCpSettings": true, "hasCpSection": false, "changelogUrl": "???", "class": "dutchheight\\awsserverlessimagehandler\\Awsserverlessimagehandler" diff --git a/src/Awsserverlessimagehandler.php b/src/Awsserverlessimagehandler.php index e3b7bb4..5db8e49 100644 --- a/src/Awsserverlessimagehandler.php +++ b/src/Awsserverlessimagehandler.php @@ -23,7 +23,7 @@ use craft\web\twig\variables\CraftVariable; - +use dutchheight\awsserverlessimagehandler\models\Settings; use dutchheight\awsserverlessimagehandler\fields\ImageProperties; use dutchheight\awsserverlessimagehandler\variables\Variable; use dutchheight\awsserverlessimagehandler\services\HelperService; @@ -54,6 +54,11 @@ class Awsserverlessimagehandler extends Plugin */ public $schemaVersion = '1.0.0'; + /** + * @var bool + */ + public $hasCpSettings = true; + // Public Methods // ========================================================================= @@ -82,7 +87,7 @@ public function getHelpers(): HelperService // Protected Methods // ========================================================================= - + protected function registerComponents() { $this->setComponents([ 'helpers' => HelperService::class @@ -114,4 +119,19 @@ function (Event $event) { } ); } + + protected function createSettingsModel() + { + return new Settings(); + } + + protected function settingsHtml() + { + return Craft::$app->view->renderTemplate( + 'aws-serverless-image-handler/settings', + [ + 'settings' => $this->getSettings() + ] + ); + } } diff --git a/src/models/Settings.php b/src/models/Settings.php new file mode 100644 index 0000000..213dda9 --- /dev/null +++ b/src/models/Settings.php @@ -0,0 +1,25 @@ +serverlessDistributionURL); + } +} \ No newline at end of file diff --git a/src/templates/settings.twig b/src/templates/settings.twig new file mode 100644 index 0000000..03ae19f --- /dev/null +++ b/src/templates/settings.twig @@ -0,0 +1,12 @@ +{% import "_includes/forms" as forms %} + +{{ forms.autosuggestField({ + label: 'Serverless Distribution URL', + instructions: 'Enter your AWS Serverless Image Handler CloudFront Distribution URL here.', + name: 'serverlessDistributionURL', + id: 'serverlessDistributionURL', + required: false, + value: settings.serverlessDistributionURL, + suggestEnvVars: true, + suggestAliases: true, +}) }} \ No newline at end of file diff --git a/src/variables/Variable.php b/src/variables/Variable.php index de03267..b158bec 100644 --- a/src/variables/Variable.php +++ b/src/variables/Variable.php @@ -54,11 +54,14 @@ public function getImgUrl(Asset $image, array $edits = null, bool $allowWebP = t return $image->url; } - $volumeSubfolder = (Craft::parseEnv($image->getVolume()->subfolder) ?: $image->getVolume()->subfolder); + $distributionSettingUrl = Craft::parseEnv(Awsserverlessimagehandler::$plugin->settings->serverlessDistributionURL); + $volumeUrl = Craft::parseEnv($image->volume->url); + $distributionUrl = $distributionSettingUrl ?: $volumeUrl; + $volumeSubfolder = Craft::parseEnv($image->getVolume()->subfolder); $json = [ "bucket" => $image->getVolume()->bucket, - "key" => $volumeSubfolder ? $volumeSubfolder . "/" . $image['filename'] : $image['filename'], + "key" => $volumeSubfolder ? $volumeSubfolder . "/" . $image->getPath() : $image->getPath(), "edits" => [ "resize" => [ "fit" => (isset($edits['fit']) ? $edits['fit'] : "cover"), @@ -97,6 +100,6 @@ public function getImgUrl(Asset $image, array $edits = null, bool $allowWebP = t $json["edits"]["webp"] = []; } - echo (Craft::parseEnv($image->volume->url) ?: $image->volume->url) . base64_encode(json_encode($json)); + echo $distributionUrl . base64_encode(json_encode($json)); } }