1

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)
  • Something should be getting logged... can you reproduce and search for [error] or [warning] and find it? – Brad Bell Dec 6 '14 at 5:35
  • 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 Dec 8 '14 at 18:14
  • There should be a full stack-trace underneath that. Can you add it to the original question? – Brad Bell Dec 8 '14 at 18:16
  • updated original – Curtis Dec 8 '14 at 18:28
2

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 '<a href="'.$cat->getCpEditUrl().'">'.$cat->title.'</a>';
    }
}

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).

| improve this answer | |
  • 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. – migswd Mar 19 '16 at 19:25
  • 1
    @migswd Yeah things changed in 2.5. Just updated my answer. Thanks! – Brandon Kelly Mar 21 '16 at 20:46

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.