2

I am trying to return an array of selected entries in the list view, but so far I can only get first() and last() to export the names, since it's only grabbing one entry. I would like to get a comma separated list of entries tied the that specific entry.

I have tried using find() and find()->title but it gives me an error.

Here's what I have right now.

    public function getEntryTableAttributeHtml(EntryModel $entry, $attribute){
     if ($attribute == 'sponsorEntries'){
      return '<span>'.$entry->sponsorEntries->first().'</span>';
     }
    }

and then I call it:

        // Artists
     public function modifyEntryTableAttributes(&$attributes, $source){
      if ($source == 'section:9'){
        $attributes['sponsorEntries'] = Craft::t('Sponsor');
      }
     }

but I would like to get return all the entries, not just the first() one like the screenshot below illustrates.

enter image description here

3

You'll need to use the find method rather than first and last, in order to pull all the entries.

Then, you'll have to loop through the entries, appending their titles to a variable which will be used for the attribute's HTML value in the list view.

Here, I'm pushing the entry titles to an array called $tempArray. After looping, the array is joined and returned as a comma separated list:

public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
    if ($attribute == 'sponsorEntries')
    {
        if ($sponsorEntries = @$entry->$attribute->find())
        {
            $tempArray = [];
            foreach ($sponsorEntries as $sponsorEntry)
            {
                $tempArray[] = $sponsorEntry->title;
            }
            // Will return "Awesome Entry, Another Awesome Entry"
            return implode(', ', $tempArray);
        }
    }
}

If you want to, you can also make the entry titles in the list view clickable – i.e., to make it easy for your content editor to edit those entries – by wrapping the titles in anchor tags and pulling the entries' cpEditUrl property:

if ($sponsorEntries = @$entry->$attribute->find())
{
    $tempArray = [];
    foreach ($sponsorEntries as $sponsorEntry)
    {
        $tempArray[] = '<a href="' . $sponsorEntry->cpEditUrl . '">' . $sponsorEntry->title . '</a>';
    }
    return implode(', ', $tempArray);
 }

...or if you prefer, you can make the links point to the entries on the frontend, by using $sponsorEntry->url instead:

$tempArray[] = '<a href="' . $sponsorEntry->url . '" target="_blank">' . $sponsorEntry->title . '</a>';
  • Thanks for this, when adding this to getEntryTableAttributeHtml and reloading the entries, I get a blank list (nothing renders), do I have to update modifyEntryTableAttributes too? Right now I just have $attributes['sponsorEntries'] = Craft::t('Sponsor(s)'); – Kenyon Kowal Sep 30 '15 at 21:19
  • Nope, looks good :) – Mats Mikkel Rummelhoff Sep 30 '15 at 21:19
  • hm, I can't seem to get it working. The list page goes blank when adding either of the code samples. – Kenyon Kowal Sep 30 '15 at 21:25
  • I tested it and the code runs fine on my end. If the page goes blank its a PHP error; turn on dev mode and you'll likely see an error message. Also check logs under /craft/storage/runtime/logs – Mats Mikkel Rummelhoff Sep 30 '15 at 21:28
  • Well actually I just spotted a typo in my example – had $element in place of $entry in a couple of spots. Edited the answer; should be correct now. Sorry about that – test again and see if the error goes away. – Mats Mikkel Rummelhoff Sep 30 '15 at 21:31

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.