Created
August 9, 2012 20:54
-
-
Save rjpeter2/3307956 to your computer and use it in GitHub Desktop.
Field Formatter for replacing empty field with another.
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 | |
/** | |
* Implements hook_field_formatter_info(). | |
*/ | |
function custom_field_formatter_info() { | |
$formatters = array(); | |
$formatters['custom_event_time'] = array( | |
'label' => t('CUSTOM: Event Time'), | |
'description' => t('Displays this field normally if it has a value, if empty, it displays the time from field_event_date.'), | |
'field types' => array('text'), | |
); | |
return $formatters; | |
} | |
/** | |
* Implements hook_field_formatter_view(). | |
*/ | |
function custom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { | |
$element = array(); | |
$settings = $display['settings']; | |
switch ($display['type']) { | |
case 'custom_event_time': | |
if (!empty($items)) { | |
// Field has data, print normally. | |
$display['type'] = 'text_default'; | |
$element = text_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display); | |
} | |
else { | |
// If the this field is empty, use the time values from the date field instead. | |
$field = field_info_field('field_event_date'); | |
$instance = field_info_instance($entity_type, 'field_event_date', 'event'); | |
$items = field_get_items($entity_type, $entity, 'field_event_date', $langcode); | |
$display = field_get_display($instance, 'teaser', $entity); | |
// Manually set date formatter. | |
$display['settings']['format_type'] = 'time_only'; | |
$element = date_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display); | |
// Drupal needs the following line, or your field will NOT print! | |
$element['#items'] = $items; | |
} | |
break; | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment