[FR] Add Owner to 'Default Table Columns' in Custom Sources #15230
-
I'm setting up a custom source for Matrix entries and looking for an option to add the Owner to the Default Table Columns to give the Matrix entries some context. I see Parent but that seems to be different (Structures, I guess). Is it possible to add an Owner column? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
That’s a little tricky, because the owners could span across multiple element types, so we don’t currently have a way to eager-load them. If you don’t mind the performance hit, you could add the column yourself by adding this code to a module’s use craft\base\Element;
use craft\elements\Entry;
use craft\events\DefineAttributeHtmlEvent;
use craft\events\RegisterElementTableAttributesEvent;
use craft\helpers\Cp;
use yii\base\Event;
Event::on(
Entry::class,
Element::EVENT_REGISTER_TABLE_ATTRIBUTES,
function(RegisterElementTableAttributesEvent $event) {
$event->tableAttributes['owner'] = ['label' => 'Owner'];
},
);
Event::on(
Entry::class,
Element::EVENT_DEFINE_ATTRIBUTE_HTML,
function(DefineAttributeHtmlEvent $event) {
if ($event->attribute === 'owner') {
/** @var Entry $entry */
$entry = $event->sender;
$owner = $entry->getOwner();
$event->html = $owner ? Cp::elementChipHtml($owner) : '';
}
},
); |
Beta Was this translation helpful? Give feedback.
-
Thanks Brandon! I'll set that up and see how it performs for my site. Appreciate it. |
Beta Was this translation helpful? Give feedback.
That’s a little tricky, because the owners could span across multiple element types, so we don’t currently have a way to eager-load them.
If you don’t mind the performance hit, you could add the column yourself by adding this code to a module’s
init()
method: