1

Is it possible to display in the asset overview on which entry / entries an asset is used? So that when I go into an asset, I have an overview of the entries where that asset is inserted.enter image description here

1 Answer 1

1

There's nothing native that would add a column like that, so you'd need to create a plugin or custom module that registers a custom column by listening to the Asset::EVENT_REGISTER_TABLE_ATTRIBUTES event, and rendering the content for that column (i.e. the list of related entries) via the Asset::EVENT_SET_TABLE_ATTRIBUTE_HTML event.

For that latter part, you'd need to create a query that queries for all entries related to each asset (the asset will be available via the sender attribute for the SetElementTableAttributeHtmlEvent instance passed to your EVENT_SET_TABLE_ATTRIBUTE_HTML event listener); rendering a list of titles/links to each entry (as per your mockup).

The query could probably be an asset query using the relatedTo param, i.e. something like

/** @var Asset $asset */
$asset = $event->sender;

$relatedEntries = \craft\elements\Entry::find()
    ->relatedTo(['targetElement' => $asset->id])
    ->all();

...

...although for performance reasons, you might want to avoid that approach and look into doing a completely custom query.

Here's a complete, albeit simplified (and untested) example of how the solution described above could look (this code would go into your module or plugin's init() method):

// Add a custom "Entries" column to asset indexes
Event::on(
    Asset::class,
    Element::EVENT_REGISTER_TABLE_ATTRIBUTES,
    static function (RegisterElementTableAttributesEvent $event) {
        $event->tableAttributes['_relatedEntries'] = [
            'label' => 'Entries',
        ];
    }
); 

// Render a list of edit page links for all related entries, for the custom column
Event::on(
    Asset::class, 
    Asset::EVENT_SET_TABLE_ATTRIBUTE_HTML, 
    static function (SetElementTableAttributeHtmlEvent $event) {
        if ($event->attribute === '_relatedEntries') {
            /** @var Asset $asset */
            $asset = $event->sender;

            // Get all entries related to this asset
            $relatedEntries = Entry::find()
                ->relatedTo(['targetElement' => $asset->id])
                ->all();

            // Build an array of links to the related entries' edit pages
            $entryLinks = [];
            foreach ($entries as $entry) {
                $entryLinks[] = '<a href="' . $entry->getCpEditUrl() . '"'>' . $entry->title . '</a>';
            }

            $event->html = implode(', ', $entryLinks);
        }
    }
);

As a good place to start, I'd also recommend looking at the Asset Usage plugin, which does almost exactly what you want.

2
  • Thank you. But the problem is, that the asset usage plugin doesn't show, on which entry / entries the asset is inserted. I only see how often it is used.
    – ChuckLean
    Commented Dec 6, 2022 at 19:50
  • I'm aware, which is why I wrote that the Asset Usage plugin does almost exactly what you want, and that you'll need to implement this as a custom module or plugin. I've added a more complete example to the answer now though – hopefully it'll be useful to you :) Commented Dec 6, 2022 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.