Created
August 21, 2015 08:52
-
-
Save mmikkel/450306a143310d935e76 to your computer and use it in GitHub Desktop.
Craft: Add entry author's category field to entry listing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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