4

Using the example code from the docs, I'm attempting to add a value from my custom field to the Entries listing.

public function modifyEntryTableAttributes(&$attributes, $source)
{
    if ($source == 'index')
    {
        unset($attributes['expiryDate']);
        $attributes['id'] = Craft::t('ID');
    }
}

How do I go about accessing additional attributes on the Entries? The custom field data is stored in MyPluginName's table and doesn't appear to be available in the attributes of the EntryElement.

Thanks for any pointers you may have!

1 Answer 1

5

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

6
  • Thanks for the response Jørgen. Can you clarify something about the modifyEntryTableAttributes hook? If I use a standard field from craft in the $attributes['MyStandardCraftField'] then it works. However, if I use my custom field, which is made up of a few of the Craft field types, like date/time & text input, and is stored in it's own plugin table, I don't appear to have access to it in the $attributes['MyCustomField'] variable. Any ideas? Commented May 20, 2015 at 11:44
  • One more possible factor: $attributes['MyCustomField'] does appear to return data from my custom field, however it is an array since there are multiple fields within this single custom field. Commented May 20, 2015 at 11:54
  • @JesseSchutt do you mean $entry['MyCustomField'] inside getEntryTableAttributeHtml? Would be great to see your code or error messages.
    – fuling
    Commented May 20, 2015 at 12:02
  • Here is a snippet of the two main functions: gist.github.com/jschutt/adea2793456b25616f6b Commented May 20, 2015 at 12:10
  • I can get into the custom field when accessing it through $entry['MyCustomField'] in the getEntryTableAttributeHtml. It errors out when I try to access $attributes['MyCustomField']. I'm assuming that has to do with my custom field returning an array of data instead of a string. Does that sound right? Commented May 20, 2015 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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