diff --git a/composer.json b/composer.json index 4b7c58f..39b754b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "milkshakestudio/craft-brandfetch", "description": "Craft CMS Plugin that allows you quickly get the logo of a website via the Brandfetch API.", "type": "craft-plugin", - "version": "1.0.1", + "version": "1.0.2", "keywords": [ "craft", "cms", diff --git a/src/Brandfetch.php b/src/Brandfetch.php index 2a5fd88..28bd099 100644 --- a/src/Brandfetch.php +++ b/src/Brandfetch.php @@ -184,7 +184,9 @@ protected function createSettingsModel() */ protected function settingsHtml(): string { - // Render the settings template + // TODO: add erroro if no volumes + // Render the settings template + $destinationOptions = []; $volumes = Craft::$app->getVolumes(); foreach ($volumes->getAllVolumes() as $source) { $destinationOptions[] = array('label' => $source->name, 'value' => $source->id); diff --git a/src/assetbundles/brandfetcherfield/dist/css/Brandfetcher.css b/src/assetbundles/brandfetcherfield/dist/css/Brandfetcher.css index 633b750..c4031ab 100644 --- a/src/assetbundles/brandfetcherfield/dist/css/Brandfetcher.css +++ b/src/assetbundles/brandfetcherfield/dist/css/Brandfetcher.css @@ -10,11 +10,10 @@ * @since 0.0.1 */ #fields-brandfetch-logo .flex .btn { - margin-top: 19px; + /* margin-top: 19px; */ } #fields-brandfetch-logo img{ - margin: 10px 0px; - height: 40px; - display: block; + /* width: 100%; */ + /* height: auto; */ } \ No newline at end of file diff --git a/src/assetbundles/brandfetcherfield/dist/js/Brandfetcher.js b/src/assetbundles/brandfetcherfield/dist/js/Brandfetcher.js index 2ad4f4e..e69b6ef 100644 --- a/src/assetbundles/brandfetcherfield/dist/js/Brandfetcher.js +++ b/src/assetbundles/brandfetcherfield/dist/js/Brandfetcher.js @@ -42,9 +42,6 @@ $(this.options.removeLogoBtn).on("click", this.removeLogo); $(this.options.getLogoBtn).on("click", this.getLogo); - // console.log('HIT', self.options.url, Craft.getActionUrl('brandfetch/bfcontroller/fetch-logo')) - - /* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */ }, removeLogo: function() { $(self.options.imageIdField).val(""); @@ -78,18 +75,27 @@ if (response.success) { //reset value $("#fields-brandfetch-url").val(""); - // set image ID Value - $(self.options.imageIdField).val( - response.brandfetch.result.id - ); - // create image - var $html = $(""); - $html.attr("src", response.brandfetch.result.url); - // show files - $(self.options.filledView) - .show() - .prepend($html); - $(self.options.emptyView).hide(); + + + $(self.options.imageIdField).html(response.brandfetch.html) + + // var thumbLoader = new Craft.ElementThumbLoader(); + // var $existing = $(self.options.imageIdField) + // thumbLoader.load($existing); + + var tests = new Craft.BaseElementSelectInput({ + id: 'brandfetcher-img-id', + fieldId:'brandfetcher-img-upload', + name: 'fields[logo]', + elementType: self.options.elementType, + viewMode: 'large', + limit: 1, + fieldId: self.options.id, + elements:([response.brandfetch.result.id]), + // defaultFieldLayoutId: 'brandfetcher-img-id', + modalSettings: {hideSidebar: true} + }); + Craft.cp.displayNotice(response.brandfetch.message); } else { @@ -112,6 +118,7 @@ $("#fields-brandfetch-url").removeClass("error"); $("#fields-brandfetch-url-label").removeClass("error"); }, + }; // A really lightweight plugin wrapper around the constructor, diff --git a/src/fields/Brandfetcher.php b/src/fields/Brandfetcher.php index 383b6ed..9d9751b 100644 --- a/src/fields/Brandfetcher.php +++ b/src/fields/Brandfetcher.php @@ -25,7 +25,10 @@ use craft\helpers\Json; -use craft\services\Assets; +use craft\elements\Asset; + +use function PHPSTORM_META\elementType; + /** * Brandfetcher Field * @@ -97,10 +100,10 @@ public function getContentColumnType(): string public function normalizeValue($value, ElementInterface $element = null) { // get the logo if available - $imgObj=[]; if($value){ - $imgObj = Craft::$app->getAssets()->getAssetById($value); - return parent::normalizeValue($imgObj, $element); + + $asset = Asset::find()->id($value)->one(); + return parent::normalizeValue($asset, $element); }else{ return parent::normalizeValue($value, $element); } @@ -169,13 +172,14 @@ public function getInputHtml($value, ElementInterface $element = null): string $logoURL = Craft::$app->getAssets()->getThumbUrl($value, 300, 200, false, false); } - + // Variables to pass down to our field JavaScript to let it namespace properly $jsonVars = [ 'id' => $id, 'name' => $this->handle, 'namespace' => $namespacedId, 'prefix' => Craft::$app->getView()->namespaceInputId(''), + 'elementType' => 'craft\\elements\\Asset', ]; $jsonVars = Json::encode($jsonVars); Craft::$app->getView()->registerJs("$('#{$namespacedId}-field').BrandfetchBrandfetcher(" . $jsonVars . ");"); @@ -187,12 +191,14 @@ public function getInputHtml($value, ElementInterface $element = null): string [ 'name' => $this->handle, 'value' => $this->serializeValue($value), + 'test'=> $value, 'field' => $this, 'id' => $id, 'logo' => $logoURL ?? null, 'url' => $this->url, 'apiSet' => $apiSet, - 'namespacedId' => $namespacedId + 'namespacedId' => $namespacedId, + 'elementType' => 'craft\\elements\\Asset', ] ); } diff --git a/src/services/BrandfetchService.php b/src/services/BrandfetchService.php index f05da1d..67f4360 100644 --- a/src/services/BrandfetchService.php +++ b/src/services/BrandfetchService.php @@ -71,6 +71,7 @@ public function callBFApi($url) */ public function saveLogo($res, $name ) { + $view = Craft::$app->getView(); $imgUrl = $res['response']['logo']['image']; @@ -122,12 +123,38 @@ public function saveLogo($res, $name ) $asset->setScenario(Asset::SCENARIO_CREATE); $result = Craft::$app->elements->saveElement($asset); + // $html = $asset->getEditorHtml(); + $html = $view->renderTemplateMacro('_includes/forms', 'elementSelectField', [ + [ + "id" => 'brandfetcher-img-id', + "name" => "fields[logo]", + "elementType" => 'craft\\elements\\Asset', + "selectionLabel" => "Add Logo", + "limit" => 1, + "viewMode" =>'large', + "elements" =>[$asset], + "modalSettings" => [ + "hideSidebar" => true + ] + // 'label' => Craft::t('app', 'Filename'), + // 'id' => 'newFilename', + // 'name' => 'newFilename', + // 'value' => $this->filename, + // 'errors' => $this->getErrors('newLocation'), + // 'first' => true, + // 'required' => true, + // 'class' => 'renameHelper text' + ] + ]); + // getPreviewThumbImg if ($result) { // $asset->getEditorHtml(); $returnData['result'] = $asset; $returnData['thumbnail'] = $assetsService->getThumbUrl($asset, 300, 200, false, false); $returnData['message'] = 'Image saved!'; + $returnData['html'] = $html; + } else { // $returnData['message'] = Craft::t('brandfetch', 'Oops, something went wrong...'); $returnData['result'] = $result; diff --git a/src/templates/_components/fields/Brandfetcher_input.twig b/src/templates/_components/fields/Brandfetcher_input.twig index 2b96e62..eb3cb4b 100644 --- a/src/templates/_components/fields/Brandfetcher_input.twig +++ b/src/templates/_components/fields/Brandfetcher_input.twig @@ -29,10 +29,25 @@ {% else %}