Last active
October 12, 2015 07:38
-
-
Save cam8001/3992969 to your computer and use it in GitHub Desktop.
How to implement a custom field formatter
This file contains 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
<?php | |
/** | |
* @file | |
* Sets up a field formatter, which can run custom theming or whatever else for a field. | |
* | |
* Once you have implemented the formatter, choose it on the 'display options' for a node. | |
*/ | |
/** | |
* Implements hook_field_formatter_info(). | |
* | |
* Provides a custom formatter for node_reference fields. | |
*/ | |
function superpoll_field_formatter_info() { | |
return array( | |
'superpoll_option' => array( | |
'label' => t('Option with title, thumbnail, description and vote button.'), | |
'field types' => array('node_reference'), | |
) | |
); | |
} | |
/** | |
* Implements hook_field_formatter_prepare_view(). | |
*/ | |
function superpoll_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) { | |
node_reference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays); | |
} | |
/** | |
* Implements hook_field_formatter_view(). | |
*/ | |
function superpoll_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { | |
// Type is always superpoll_option - we don't expose any other formatters, so | |
// we don't need to check $display['type']. | |
$result = array(); | |
foreach ($items as $delta => $item) { | |
if ($item['access']) { | |
$result[$delta] = array( | |
'#theme' => 'superpoll_option', | |
'#node' => $item['node'], | |
'#parent_nid' => $entity->hostEntityId(), | |
// Doesn't seem like this uses any extra memory really. | |
'#parent_node' => node_load($entity->hostEntityId()), | |
); | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment