<p>See this question:</p>

<blockquote>
  <p><a href="https://craftcms.stackexchange.com/questions/5124/possible-to-add-category-field-to-modifyentrytableattributes">Possible to add category field to modifyEntryTableAttributes?</a></p>
</blockquote>

<p>Basically, you need to use the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#getEntryTableAttributeHtml" rel="nofollow noreferrer">getEntryTableAttributeHtml</a> hook to get the actual content to the table cells in addition to the <code>modifyEntryTableAttributes</code>. You can do queries here if you need to get the data if you can not access it from the entry.</p>

<pre><code>public function modifyEntryTableAttributes(&amp;$attributes, $source)
{
    if ($source == 'index')
    {
        // This will be the header name of the column, the &lt;th&gt;
        $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 &lt;td&gt;'s
        $fieldData = craft()-&gt;myPlugin-&gt;getFieldData($entry);
        // OR $entry-&gt;myCustomField-&gt;first(); if myCustomField is an ElementCriteriaModel.
        return $fieldData;
    }
}
</code></pre>

<p><strong>EDIT:</strong> 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 <em>defined</em> in the <code>EntryElementType::defineTableAttributes</code>. This method returns an array of attributes, where the key is whatever you want, and the value is what is displayed in the tables <code>&lt;th&gt;</code> 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 <em>modify</em> this array before it is returned.</p>

<p>Where does the array of attributes go after? The function for populating the <code>&lt;td&gt;</code> tags of the table, the <code>EntryElementType::getTableAttributeHtml($entry, $attribute)</code>. The first thing this function does it to let plugins decide what to do, so it calls the <code>getEntryTableAttributeHtml</code> 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 <code>$attributes['id']</code> and did nothing in the second function, the default way Craft is getting this data is doing <code>$entry-&gt;$attribute</code> (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 <code>__toString</code>, this will not work, so you will have to tell Craft explicitly how to represent this field, e.g.  </p>

<pre><code>if ($attribute = 'myArrayOfSpecialFields'){
  return $entry-&gt;myArrayOfSpecialFields['indexOfStringToDisplay'];
}
</code></pre>

<p>There are tons of useful information on this in the EntryElementType, you can just follow the flow in the code (perhaps start in the <code>ElementIndexController</code>)</p>

<p>Hope that makes things more clear</p>

CC BY-SA 3.0
2015-05-20T11:44:56.660Z
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?
Jesse Schutt
CC BY-SA 3.0
2015-05-20T11:54:51.447Z
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.
Jesse Schutt
CC BY-SA 3.0
2015-05-20T12:02:46.553Z
@JesseSchutt do you mean `$entry['MyCustomField']` inside `getEntryTableAttributeHtml`? Would be great to see your code or error messages.
fuling
CC BY-SA 3.0
2015-05-20T12:10:10.767Z
Here is a snippet of the two main functions: https://gist.github.com/jschutt/adea2793456b25616f6b
Jesse Schutt
CC BY-SA 3.0
2015-05-20T12:50:15.463Z
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?
Jesse Schutt
CC BY-SA 3.0
2015-05-21T07:37:34.240Z
@JesseSchutt I updated my answer to try to explain a bit better, hope you figure it out
fuling
CC BY-SA 3.0
2015-05-20T05:36:50.057Z
2015-05-21T07:36:21.210Z
2017-04-13T12:50:59.930Z
fuling
<p>Using the example code from <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#modifyEntryTableAttributes" rel="nofollow">the docs</a>, I'm attempting to add a value from my custom field to the Entries listing. </p>

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

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

<p>Thanks for any pointers you may have!</p>

<plugin-development><control-panel>
Adding additional attributes to modifyEntryTableAttributes()
# Adding additional attributes to modifyEntryTableAttributes()

(Asked by: username_0 on 2015-05-19)

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!

**Tags:** plugin-development, control-panel


---

## 1 answers

### Answer by username_1 on 2015-05-20. Score: 5 | Accepted answer

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 
        $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 '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  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  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

> **Comments:**
> **username_0**: 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?  
> **username_0**: 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.  
> **username_1**: @JesseSchutt do you mean `$entry['MyCustomField']` inside `getEntryTableAttributeHtml`? Would be great to see your code or error messages.  
> **username_0**: Here is a snippet of the two main functions: https://gist.github.com/jschutt/adea2793456b25616f6b  
> **username_0**: 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?  
> **username_1**: @JesseSchutt I updated my answer to try to explain a bit better, hope you figure it out  

---

