Skip to content

Commit

Permalink
#7 Added support for asset elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Hintum committed Jul 5, 2021
1 parent 2134dbc commit 80c027d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
25 changes: 11 additions & 14 deletions src/fields/ImageHotspots.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 */
Expand All @@ -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,
]);
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 80c027d

Please sign in to comment.