Skip to content

Commit

Permalink
Make all relation fields selectable as thumbnail providers
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Sep 4, 2024
1 parent 5e23c85 commit 7fced54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Release Notes for Craft CMS 5.5 (WIP)

### Administration
- All relation fields can now be selected as field layouts’ thumbnail providers. ([#15651](https://github.com/craftcms/cms/discussions/15651))
17 changes: 1 addition & 16 deletions src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Craft;
use craft\base\ElementInterface;
use craft\base\ThumbableFieldInterface;
use craft\elements\Asset;
use craft\elements\conditions\ElementCondition;
use craft\elements\db\AssetQuery;
Expand Down Expand Up @@ -47,7 +46,7 @@
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Assets extends BaseRelationField implements ThumbableFieldInterface
class Assets extends BaseRelationField
{
/**
* @since 3.5.11
Expand Down Expand Up @@ -487,20 +486,6 @@ protected function previewHtml(ElementCollection $elements): string
);
}

/**
* @inheritdoc
*/
public function getThumbHtml(mixed $value, ElementInterface $element, int $size): ?string
{
/** @var AssetQuery|ElementCollection $value */
if ($value instanceof AssetQuery) {
$handle = sprintf('%s-%s-%s', preg_replace('/:+/', '-', __METHOD__), $this->id, $size);
$value = (clone $value)->eagerly($handle);
}

return $value->one()?->getThumbHtml($size);
}

// Events
// -------------------------------------------------------------------------

Expand Down
16 changes: 16 additions & 0 deletions src/fields/BaseRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use craft\base\MergeableFieldInterface;
use craft\base\NestedElementInterface;
use craft\base\RelationalFieldInterface;
use craft\base\ThumbableFieldInterface;
use craft\behaviors\EventBehavior;
use craft\db\FixedOrderExpression;
use craft\db\Query;
Expand Down Expand Up @@ -61,6 +62,7 @@ abstract class BaseRelationField extends Field implements
InlineEditableFieldInterface,
EagerLoadingFieldInterface,
RelationalFieldInterface,
ThumbableFieldInterface,
MergeableFieldInterface
{
/**
Expand Down Expand Up @@ -949,6 +951,20 @@ protected function previewHtml(ElementCollection $elements): string
return Cp::elementPreviewHtml($elements->all());
}

/**
* @inheritdoc
*/
public function getThumbHtml(mixed $value, ElementInterface $element, int $size): ?string
{
/** @var ElementQueryInterface|ElementCollection $value */
if ($value instanceof ElementQueryInterface) {
$handle = sprintf('%s-%s-%s', preg_replace('/:+/', '-', __METHOD__), $this->id, $size);
$value = (clone $value)->eagerly($handle);
}

return $value->one()?->getThumbHtml($size);
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 7fced54

Please sign in to comment.