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


You can use the getTableAttributesForSource event

    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 '';
  • 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 Jun 6 '18 at 20:15
  • They won't get eager loaded usually... unless you have another event for it Jun 6 '18 at 20:19
  • Hm, and what would be the best event to force eagerload... because that should come from the section view I guess... Jun 6 '18 at 20:25
  • 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 Jun 6 '18 at 20:27
  • do not need. Just doublechecked it right now. It is good enough, thank you for the help, working like charm Jun 6 '18 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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