Skip to content

Instantly share code, notes, and snippets.

@mmikkel
Created August 21, 2015 08:52
Show Gist options
  • Save mmikkel/450306a143310d935e76 to your computer and use it in GitHub Desktop.
Save mmikkel/450306a143310d935e76 to your computer and use it in GitHub Desktop.
Craft: Add entry author's category field to entry listing
public function modifyEntryTableAttributes( &$attributes, $source ) {
// Note: the number "3" is the relevant section's ID
if ($source == 'section:3')
{
// Add a dummy field key – this could be called anything but avoid conflicts
$attributes['myEntryAuthorCategory'] = Craft::t('Author category');
}
}
public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
if ($attribute == 'myEntryAuthorCategory')
{
// Get entry author
$author = craft()->users->getUserById($entry->authorId);
// Get category field for author – "categoryFieldHandle" represents the handle for your category field
if ($categories = $author->categoryFieldHandle->find())
{
$temp = array();
// Build HTML output (category titles/links)
foreach ($categories as $category)
{
$attributeHtml = $category->title;
if ( $category->cpEditUrl ) {
$attributeHtml = '<a href="' . $category->cpEditUrl . '">' . $attributeHtml . '</a>';
}
$temp[] = $attributeHtml;
}
return ! empty( $temp ) ? implode( ', ', $temp ) : false;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment