Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
updated to be a real asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Slgoetz committed Apr 15, 2020
1 parent 37b5b35 commit 7f90bbe
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/Brandfetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/assetbundles/brandfetcherfield/dist/css/Brandfetcher.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; */
}
37 changes: 22 additions & 15 deletions src/assetbundles/brandfetcherfield/dist/js/Brandfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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 = $("<img/>");
$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 {
Expand All @@ -112,6 +118,7 @@
$("#fields-brandfetch-url").removeClass("error");
$("#fields-brandfetch-url-label").removeClass("error");
},

};

// A really lightweight plugin wrapper around the constructor,
Expand Down
18 changes: 12 additions & 6 deletions src/fields/Brandfetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
use craft\helpers\Json;


use craft\services\Assets;
use craft\elements\Asset;

use function PHPSTORM_META\elementType;

/**
* Brandfetcher Field
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 . ");");
Expand All @@ -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',
]
);
}
Expand Down
27 changes: 27 additions & 0 deletions src/services/BrandfetchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function callBFApi($url)
*/
public function saveLogo($res, $name )
{
$view = Craft::$app->getView();

$imgUrl = $res['response']['logo']['image'];

Expand Down Expand Up @@ -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;
Expand Down
35 changes: 17 additions & 18 deletions src/templates/_components/fields/Brandfetcher_input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@
{% else %}

<div id="brandfetch-logo">
<div class="flex" id="brandfetch-empty" {% if logo %}style="display:none;"{% endif %}>

{{forms.elementSelectField({
id: 'brandfetcher-img-id',
fieldId:'brandfetcher-img-upload',
name: name,
elementType: elementType,
selectionLabel: "Add Logo",
limit: 1,
viewMode:'large',
elements:(test is defined and test ? [test]),
modalSettings: {
hideSidebar: true
}
})}}

<div class="flex" id="brandfetch-empty">
<div class="flex-grow">
{{ forms.textField({
label: 'URL',
label: 'Fetch URL',
type:'url',
id: 'brandfetch-url',
name: 'brandfetch-url',
Expand All @@ -43,22 +58,6 @@

<div class="btn add icon js-get-brandfetch ">Get Logo</div>
</div>


<div id="brandfetch-filled" {% if not logo %}style="display:none;"{% endif %}>
{% if logo %}
<img src="{{logo}}">
{% endif %}
<div class="btn small delete icon js-remove-brandfetch" >Remove</div>
</div>
</div>

{{ forms.textField({
type:'hidden',
id: 'brandfetcher-img-id',
name: name,
value: value
})
}}

{% endif %}

0 comments on commit 7f90bbe

Please sign in to comment.