Developing plugin for Craft 3, I want to add extra property to Assets (data being pulled from other table)
I am able to pull in the data by extending element query:
Event::on(
ElementQuery::class,
ElementQuery::EVENT_AFTER_PREPARE,
function(CancelableEvent $e) {
if ($e->sender->elementType == 'craft\\elements\\Asset')
{
$e->sender->query->addSelect('linkstats.downloads');
$subQuery = (new Query())
->select('assetId, SUM(downloads) AS downloads')
->from('{{%my_plugin_table}}')
->groupBy('assetId');
$e->sender->query->leftJoin(
['linkstats' => $subQuery], 'assets.id = linkstats.assetId'
);
}
}
);
However when trying to display the asset I get error:
Setting unknown property: craft\elements\Asset::downloads
Question, how do I register this new property/attribute for Assets? I cannot find any event to do that, is it possible at all?