<p>In this case, the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#modifyEntryTableAttributes" rel="nofollow">modifyEntryTableAttributes</a> hook isn’t quite enough - you also need to use the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#getEntryTableAttributeHtml" rel="nofollow">getEntryTableAttributeHtml</a> hook to customize what actually gets output, since <code>$entry-&gt;entryCategory</code> returns an <a href="http://buildwithcraft.com/classreference/models/ElementCriteriaModel" rel="nofollow">ElementCriteriaModel</a> object, which can’t be output as a string directly.</p>

<pre class="lang-php prettyprint-override"><code>public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
    if ($attribute == 'entryCategory')
    {
        $cat = $entry-&gt;entryCategory-&gt;first();
        return '&lt;a href="'.$cat-&gt;getCpEditUrl().'"&gt;'.$cat-&gt;title.'&lt;/a&gt;';
    }
}
</code></pre>

<hr>

<p><strong>UPDATE</strong></p>

<p>In Craft 2.5+, it's possible to add category fields to element indexes without a plugin, although only the first-selected category will be shown.</p>

<p>If you want to use a plugin to provide a customized cell value, you can use the new <a href="https://craftcms.com/docs/plugins/hooks-reference#defineAdditionalEntryTableAttributes" rel="nofollow">defineAdditionalEntryTableAttributes</a> hook to register a custom table attribute (this replaces the old modifyEntryTableAttributes hook). You’ll still use the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#getEntryTableAttributeHtml" rel="nofollow">getEntryTableAttributeHtml</a> the same as pre-2.5 (see above example).</p>

<p>The other key change is that, even with a plugin implementing these two hooks, it is still up to the admin to go to the element index, click the Settings cog icon, and check the box next to the custom table attribute (as admins have full control over which table columns show up now).</p>

CC BY-SA 3.0
2016-03-19T19:25:49.663Z
Hi Brandon is it still working in Craft 2.5 ? Because I just updated from 2.4 to 2.5 and it is brealing my extras fields added with modifyEntryTableAttributes.
migmaker
CC BY-SA 3.0
2016-03-21T20:46:17.610Z
@migswd Yeah things changed in 2.5. Just updated my answer. Thanks!
Brandon Kelly
CC BY-SA 3.0
2014-12-09T17:30:29.593Z
2016-03-21T20:46:02.263Z
2016-03-21T20:46:02.263Z
Brandon Kelly
<p>I'm playing around with the modifyEntryTableAttributes() and tried adding a category field. I get an unknown error has occurred.</p>

<p>Guessing there's a problem with it being a category field and not being a sortable attribute? Anyone know if this could work?</p>

<p>I tried a few things but here's a simple example:</p>

<pre><code>public function modifyEntryTableAttributes(&amp;$attributes, $source)
{
  if ($source == 'section:3')
  {
    $attributes['entryCategory'] = Craft::t('Category');
  }
}
</code></pre>

<p>Full stack trace for the error I get:</p>

<pre><code>2014/12/08 18:14:07 [error] [php] htmlspecialchars() expects parameter 1 to be string, object given (craft/app/framework/web/helpers/CHtml.php:103)
Stack trace:
#0 craft/app/elementtypes/EntryElementType.php(405): Craft\EntryElementType-&gt;getTableAttributeHtml()
#1 craft/app/variables/ElementTypeVariable.php(91): Craft\EntryElementType-&gt;getTableAttributeHtml()
#2 unknown(0): Craft\ElementTypeVariable-&gt;getTableAttributeHtml()
#3 craft/app/vendor/twig/twig/lib/Twig/Template.php(478): call_user_func_array()
#4 craft/app/etc/templating/BaseTemplate.php(41): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b-&gt;getAttribute()
#5 craft/storage/runtime/compiled_templates/e6/df/1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b.php(124): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b-&gt;getAttribute()
#6 craft/app/vendor/twig/twig/lib/Twig/Template.php(279): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b-&gt;doDisplay()
#7 craft/app/vendor/twig/twig/lib/Twig/Template.php(253): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b-&gt;displayWithErrorHandling()
#8 craft/storage/runtime/compiled_templates/7b/09/b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12.php(59): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b-&gt;display()
#9 craft/app/vendor/twig/twig/lib/Twig/Template.php(279): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12-&gt;doDisplay()
#10 craft/app/vendor/twig/twig/lib/Twig/Template.php(253): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12-&gt;displayWithErrorHandling()
#11 craft/app/vendor/twig/twig/lib/Twig/Template.php(264): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12-&gt;display()
#12 craft/app/vendor/twig/twig/lib/Twig/Environment.php(292): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12-&gt;render()
#13 craft/app/services/TemplatesService.php(220): Twig_Environment-&gt;render()
#14 craft/app/elementtypes/BaseElementType.php(233): Craft\TemplatesService-&gt;render()
#15 craft/app/controllers/ElementIndexController.php(300): Craft\EntryElementType-&gt;getIndexHtml()
#16 craft/app/controllers/ElementIndexController.php(112): Craft\ElementIndexController-&gt;_getElementHtml()
#17 craft/app/framework/web/actions/CInlineAction.php(49): Craft\ElementIndexController-&gt;actionGetElements()
#18 craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams()
#19 craft/app/framework/web/CController.php(286): Craft\ElementIndexController-&gt;runAction()
#20 craft/app/framework/web/CController.php(265): Craft\ElementIndexController-&gt;runActionWithFilters()
#21 craft/app/framework/web/CWebApplication.php(282): Craft\ElementIndexController-&gt;run()
#22 craft/app/etc/web/WebApp.php(754): Craft\WebApp-&gt;runController()
#23 craft/app/etc/web/WebApp.php(261): Craft\WebApp-&gt;_processActionRequest()
#24 craft/app/framework/base/CApplication.php(180): Craft\WebApp-&gt;processRequest()
#25 craft/app/index.php(62): Craft\WebApp-&gt;run()
#26 public/index.php(14): require_once()
REQUEST_URI=/index.php/admin/actions/elementIndex/getElements
in craft/app/elementtypes/BaseElementType.php (322)
in craft/app/elementtypes/EntryElementType.php (405)
in craft/app/variables/ElementTypeVariable.php (91)
2014/12/08 18:14:07 [trace] [system.CModule] Loading "errorHandler" application component
in craft/app/etc/web/WebApp.php (569)
in craft/app/elementtypes/BaseElementType.php (322)
in craft/app/elementtypes/EntryElementType.php (405)
</code></pre>

CC BY-SA 3.0
2014-12-06T05:35:28.460Z
Something should be getting logged... can you reproduce and search for `[error]` or `[warning]` and find it?
Brad Bell
CC BY-SA 3.0
2014-12-08T18:14:56.927Z
Oh yup, there it is. 

[error] [php] htmlspecialchars() expects parameter 1 to be string, object given (craft/app/framework/web/helpers/CHtml.php:103)
Curtis
CC BY-SA 3.0
2014-12-08T18:16:56.930Z
There should be a full stack-trace underneath that. Can you add it to the original question?
Brad Bell
CC BY-SA 3.0
2014-12-08T18:28:11.637Z
updated original
Curtis
<plugin-development><control-panel>
Possible to add category field to modifyEntryTableAttributes?
# Possible to add category field to modifyEntryTableAttributes?

(Asked by: username_0 on 2014-12-06)

I'm playing around with the modifyEntryTableAttributes() and tried adding a category field. I get an unknown error has occurred.
Guessing there's a problem with it being a category field and not being a sortable attribute? Anyone know if this could work?
I tried a few things but here's a simple example:
public function modifyEntryTableAttributes(&$attributes, $source)
{
  if ($source == 'section:3')
  {
    $attributes['entryCategory'] = Craft::t('Category');
  }
}

Full stack trace for the error I get:
2014/12/08 18:14:07 [error] [php] htmlspecialchars() expects parameter 1 to be string, object given (craft/app/framework/web/helpers/CHtml.php:103)
Stack trace:
#0 craft/app/elementtypes/EntryElementType.php(405): Craft\EntryElementType->getTableAttributeHtml()
#1 craft/app/variables/ElementTypeVariable.php(91): Craft\EntryElementType->getTableAttributeHtml()
#2 unknown(0): Craft\ElementTypeVariable->getTableAttributeHtml()
#3 craft/app/vendor/twig/twig/lib/Twig/Template.php(478): call_user_func_array()
#4 craft/app/etc/templating/BaseTemplate.php(41): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b->getAttribute()
#5 craft/storage/runtime/compiled_templates/e6/df/1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b.php(124): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b->getAttribute()
#6 craft/app/vendor/twig/twig/lib/Twig/Template.php(279): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b->doDisplay()
#7 craft/app/vendor/twig/twig/lib/Twig/Template.php(253): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b->displayWithErrorHandling()
#8 craft/storage/runtime/compiled_templates/7b/09/b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12.php(59): __TwigTemplate_e6df1287b446e919fa0265d20996129e1a92548f58c2cf7a03f5f2a65590210b->display()
#9 craft/app/vendor/twig/twig/lib/Twig/Template.php(279): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12->doDisplay()
#10 craft/app/vendor/twig/twig/lib/Twig/Template.php(253): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12->displayWithErrorHandling()
#11 craft/app/vendor/twig/twig/lib/Twig/Template.php(264): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12->display()
#12 craft/app/vendor/twig/twig/lib/Twig/Environment.php(292): __TwigTemplate_7b09b21bca6b1cf8d1781aac559d66197da8067b06ecd6898c5fde93e6d14c12->render()
#13 craft/app/services/TemplatesService.php(220): Twig_Environment->render()
#14 craft/app/elementtypes/BaseElementType.php(233): Craft\TemplatesService->render()
#15 craft/app/controllers/ElementIndexController.php(300): Craft\EntryElementType->getIndexHtml()
#16 craft/app/controllers/ElementIndexController.php(112): Craft\ElementIndexController->_getElementHtml()
#17 craft/app/framework/web/actions/CInlineAction.php(49): Craft\ElementIndexController->actionGetElements()
#18 craft/app/framework/web/CController.php(308): CInlineAction->runWithParams()
#19 craft/app/framework/web/CController.php(286): Craft\ElementIndexController->runAction()
#20 craft/app/framework/web/CController.php(265): Craft\ElementIndexController->runActionWithFilters()
#21 craft/app/framework/web/CWebApplication.php(282): Craft\ElementIndexController->run()
#22 craft/app/etc/web/WebApp.php(754): Craft\WebApp->runController()
#23 craft/app/etc/web/WebApp.php(261): Craft\WebApp->_processActionRequest()
#24 craft/app/framework/base/CApplication.php(180): Craft\WebApp->processRequest()
#25 craft/app/index.php(62): Craft\WebApp->run()
#26 public/index.php(14): require_once()
REQUEST_URI=/index.php/admin/actions/elementIndex/getElements
in craft/app/elementtypes/BaseElementType.php (322)
in craft/app/elementtypes/EntryElementType.php (405)
in craft/app/variables/ElementTypeVariable.php (91)
2014/12/08 18:14:07 [trace] [system.CModule] Loading "errorHandler" application component
in craft/app/etc/web/WebApp.php (569)
in craft/app/elementtypes/BaseElementType.php (322)
in craft/app/elementtypes/EntryElementType.php (405)

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

> **Comments:**
> **username_1**: Something should be getting logged... can you reproduce and search for `[error]` or `[warning]` and find it?  
> **username_0**: Oh yup, there it is.   [error] [php] htmlspecialchars() expects parameter 1 to be string, object given (craft/app/framework/web/helpers/CHtml.php:103)  
> **username_1**: There should be a full stack-trace underneath that. Can you add it to the original question?  
> **username_0**: updated original  

---

## 1 answers

### Answer by username_2 on 2014-12-09. Score: 2 | Accepted answer

In this case, the modifyEntryTableAttributes hook isn’t quite enough - you also need to use the getEntryTableAttributeHtml hook to customize what actually gets output, since $entry->entryCategory returns an ElementCriteriaModel object, which can’t be output as a string directly.
public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
    if ($attribute == 'entryCategory')
    {
        $cat = $entry->entryCategory->first();
        return ''.$cat->title.'';
    }
}


UPDATE
In Craft 2.5+, it's possible to add category fields to element indexes without a plugin, although only the first-selected category will be shown.
If you want to use a plugin to provide a customized cell value, you can use the new defineAdditionalEntryTableAttributes hook to register a custom table attribute (this replaces the old modifyEntryTableAttributes hook). You’ll still use the getEntryTableAttributeHtml the same as pre-2.5 (see above example).
The other key change is that, even with a plugin implementing these two hooks, it is still up to the admin to go to the element index, click the Settings cog icon, and check the box next to the custom table attribute (as admins have full control over which table columns show up now).

> **Comments:**
> **username_3**: Hi Brandon is it still working in Craft 2.5 ? Because I just updated from 2.4 to 2.5 and it is brealing my extras fields added with modifyEntryTableAttributes.  
> **username_2**: @migswd Yeah things changed in 2.5. Just updated my answer. Thanks!  

---

