Q:

How can I extend admincp to show all related category not only the first one on the AdminCP list view?

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:

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 '';
}

