<p>You can use the <a href="https://docs.craftcms.com/v3/updating-plugins.html#gettableattributesforsource" rel="nofollow noreferrer">getTableAttributesForSource</a> event</p>

<pre><code>Event::on(
    Entry::class, 
    Element::EVENT_SET_TABLE_ATTRIBUTE_HTML, 
    function(SetElementTableAttributeHtmlEvent $event) {
    // field: + your field id, you can as well grab all Fields of type 
    // BaseRelationField and collect those ids in an array to get all
    if ($event-&gt;attribute === 'field:2') { 
        /** @var Entry $entry */
        $entry = $event-&gt;sender;

        // just a quick example to fetch the titles for all related fields
        $event-&gt;html = implode(', ', $entry-&gt;getFieldValue('relation')-&gt;select('title')-&gt;column());

        // Prevent other event listeners from getting invoked
        $event-&gt;handled = true;
    }
});
</code></pre>

<p>This is the function that renders the usual table html</p>

<pre><code>public function getTableAttributeHtml($value, ElementInterface $element): string
{
    if ($value instanceof ElementQueryInterface) {
        $element = $this-&gt;_all($value)-&gt;one();
    } else {
        $element = $value[0] ?? null;
    }

    if ($element) {
        return Craft::$app-&gt;getView()-&gt;renderTemplate('_elements/element', [
            'element' =&gt; $element
        ]);
    }

    return '';
}
</code></pre>

CC BY-SA 4.0
2018-06-06T20:15:26.967Z
Is it getting eagerloaded, or the related item is getting eagerloaded anyway I guess... So I do not have to force anything... I will check this aout thx Robin
Visky Máté
CC BY-SA 4.0
2018-06-06T20:19:19.500Z
They won't get eager loaded usually... unless you have another event for it
Robin Schambach
CC BY-SA 4.0
2018-06-06T20:25:29.323Z
Hm, and what would be the best event to force eagerload... because that should come from the section view I guess...
Visky Máté
CC BY-SA 4.0
2018-06-06T20:27:55.853Z
There is another event before the query is executed but I wouldn't suggest you to use it cause it may do more damage then good. Why do you even want to eager load them. It won't make it noticeable faster
Robin Schambach
CC BY-SA 4.0
2018-06-06T20:30:53.297Z
do not need. Just doublechecked it right now. It is good enough, thank you for the help, working like charm
Visky Máté
CC BY-SA 4.0
2018-06-06T20:13:28.063Z
2018-06-06T20:13:28.063Z
Robin Schambach
<p>I would like to extend the adminCP via plugin to not only show the first related item, but every related item in the list (like categories, or tags etc...) Right now the adminCP is showing only 1 item in the table view
<a href="https://i.sstatic.net/LvBKT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LvBKT.png" alt="enter image description here"></a></p>

<plugin-development>
How can I extend admincp to show all related category not only the first one on the AdminCP list view?
# How can I extend admincp to show all related category not only the first one on the AdminCP list view?

(Asked by: username_0 on 2018-06-06)

I would like to extend the adminCP via plugin to not only show the first related item, but every related item in the list (like categories, or tags etc...) Right now the adminCP is showing only 1 item in the table view
[![enter image description here](https://i.sstatic.net/LvBKT.png)](https://i.sstatic.net/LvBKT.png)

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2018-06-06. Score: 2 | Accepted answer

You can use the getTableAttributesForSource event
Event::on(
    Entry::class, 
    Element::EVENT_SET_TABLE_ATTRIBUTE_HTML, 
    function(SetElementTableAttributeHtmlEvent $event) {
    // field: + your field id, you can as well grab all Fields of type 
    // BaseRelationField and collect those ids in an array to get all
    if ($event->attribute === 'field:2') { 
        /** @var Entry $entry */
        $entry = $event->sender;

        // just a quick example to fetch the titles for all related fields
        $event->html = implode(', ', $entry->getFieldValue('relation')->select('title')->column());

        // Prevent other event listeners from getting invoked
        $event->handled = true;
    }
});

This is the function that renders the usual table html
public function getTableAttributeHtml($value, ElementInterface $element): string
{
    if ($value instanceof ElementQueryInterface) {
        $element = $this->_all($value)->one();
    } else {
        $element = $value[0] ?? null;
    }

    if ($element) {
        return Craft::$app->getView()->renderTemplate('_elements/element', [
            'element' => $element
        ]);
    }

    return '';
}

> **Comments:**
> **username_0**: Is it getting eagerloaded, or the related item is getting eagerloaded anyway I guess... So I do not have to force anything... I will check this aout thx Robin  
> **username_1**: They won't get eager loaded usually... unless you have another event for it  
> **username_0**: Hm, and what would be the best event to force eagerload... because that should come from the section view I guess...  
> **username_1**: There is another event before the query is executed but I wouldn't suggest you to use it cause it may do more damage then good. Why do you even want to eager load them. It won't make it noticeable faster  
> **username_0**: do not need. Just doublechecked it right now. It is good enough, thank you for the help, working like charm  

---

