From 80c027d944a7445cbd53fdd3fbd5b864d729beed Mon Sep 17 00:00:00 2001 From: Roel van Hintum Date: Mon, 5 Jul 2021 09:10:05 +0200 Subject: [PATCH] #7 Added support for asset elements --- README.md | 4 ++-- src/fields/ImageHotspots.php | 25 +++++++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cbde7e6..ba389c3 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Image Hotspots -Image Hotspots fieldtype for adding multiple hotspots to images from a matrix or super-table in Craft CMS. +Image Hotspots fieldtype for adding multiple hotspots to images from an asset element, matrix field, neo field or super-table field in Craft CMS. ## Setup & usage 1. Create an asset field for use with images. 2. Create a "Image Hotspot" field, pointing to the asset field's handle. _When used inside a super-table or matrix, multiple hotspots can be placed on one image._ -3. Make sure the hotspot field is on the same or a higher level inside an entry. _The hotspot field goes down the entry's element tree untill it finds the asset field matching the handle._ +3. Make sure the hotspot field is on the same or a higher level inside an entry. No asset field is needed when using the hotspot field directly on an asset element. _The hotspot field goes down the entry's element tree untill it finds the asset field matching the handle._ 4. Fill the asset field on the entry and save. 5. Pick the hotspots on the asset using the "Hotspot" button. diff --git a/src/fields/ImageHotspots.php b/src/fields/ImageHotspots.php index 2728fa5..1760ab4 100644 --- a/src/fields/ImageHotspots.php +++ b/src/fields/ImageHotspots.php @@ -9,6 +9,7 @@ use Craft; use craft\base\ElementInterface; use craft\base\Field; +use craft\elements\Asset; use craft\helpers\Json; use yii\db\Schema; @@ -125,19 +126,15 @@ public function getInputHtml($value, ElementInterface $element = null): string // Register our asset bundle Craft::$app->getView()->registerAssetBundle(ImageHotspotsFieldAsset::class); - if (strlen($this->relatedAssetHandle)) { - if ($element instanceof \verbb\supertable\elements\SuperTableBlockElement) { - $rootElement = $element->getOwner(); - } else { - $rootElement = $element; - } - - $rootElement = $this->determineFieldOwner($this->relatedAssetHandle, $element); + $rootElement = $this->determineFieldOwner($this->relatedAssetHandle, $element); - if (isset($rootElement)) { - $relatedAssetHandle = $this->relatedAssetHandle; - $assetField = $rootElement->$relatedAssetHandle; - } + $asset = null; + if ($rootElement instanceof Asset) { + $asset = $rootElement; + } elseif (isset($rootElement)) { + $relatedAssetHandle = $this->relatedAssetHandle; + $assetField = $rootElement->$relatedAssetHandle; + $asset = isset($assetField) ? $assetField->one() : null; } /** @var Hotspot|null $value */ @@ -146,7 +143,7 @@ public function getInputHtml($value, ElementInterface $element = null): string 'name' => $this->handle, 'value' => $value, 'relatedAssetHandle' => $this->relatedAssetHandle, - 'asset' => isset($assetField) ? $assetField->one() : null, + 'asset' => $asset, ]); } @@ -166,7 +163,7 @@ public function getContentGqlType() { */ private function determineFieldOwner(string $fieldHandle, ElementInterface $element = null) { - if (isset($element->$fieldHandle)) { + if ($element instanceof Asset || (strlen($fieldHandle) && isset($element->$fieldHandle))) { return $element; }