See this question:
Possible to add category field to modifyEntryTableAttributes?
Basically, you need to use the getEntryTableAttributeHtml hook to get the actual content to the table cells in addition to the modifyEntryTableAttributes
. You can do queries here if you need to get the data if you can not access it from the entry.
public function modifyEntryTableAttributes(&$attributes, $source)
{
if ($source == 'index')
{
// This will be the header name of the column, the <th>
$attributes['myCustomField'] = Craft::t('My Label');
}
}
public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
if ($attribute == 'myCustomField')
{
// Fetch the related data from your plugin. This will be placed in the <td>'s
$fieldData = craft()->myPlugin->getFieldData($entry);
// OR $entry->myCustomField->first(); if myCustomField is an ElementCriteriaModel.
return $fieldData;
}
}
EDIT: Think of it this way: when craft is loading up the entries (or any of the element types) to display on the index page, it has to know what fields are going to show. These fields are defined in the EntryElementType::defineTableAttributes
. This method returns an array of attributes, where the key is whatever you want, and the value is what is displayed in the tables <th>
tags. In this function, the entry type is defining some standard fields to show (title, uri, postdate etc), but Craft is nice enough to let plugins modify this array before it is returned.
Where does the array of attributes go after? The function for populating the <td>
tags of the table, the EntryElementType::getTableAttributeHtml($entry, $attribute)
. The first thing this function does it to let plugins decide what to do, so it calls the getEntryTableAttributeHtml
which is the function you should implement. The $attribute parameter is the key you defined. Here you have a chance to tell Craft how the data is displayed. If you in the first function defined $attributes['id']
and did nothing in the second function, the default way Craft is getting this data is doing $entry->$attribute
(or $entry->id in this case), which would display the entry's id in the column. If $entry->$attribute is an array, or an object without a __toString
, this will not work, so you will have to tell Craft explicitly how to represent this field, e.g.
if ($attribute = 'myArrayOfSpecialFields'){
return $entry->myArrayOfSpecialFields['indexOfStringToDisplay'];
}
There are tons of useful information on this in the EntryElementType, you can just follow the flow in the code (perhaps start in the ElementIndexController
)
Hope that makes things more clear