I followed a very helpful article by Antonio Benitez that gave some example code showing how to create a custom date formatter for date fields (How To Create a Custom Format for a Date Field). The code adds an additional format option in the manage display settings page. This works great for date fields I have created (such as an event date for an event content type) but it is not applying to the existing date fields such as the node posted date. How can I make this custom formatter apply to system date fields such as the posted date, so that it appears as an option on the manage display page as well?
I am reproducing the code Antonio posted in his article here:
/**
* Implements hook_field_formatter_info().
*/
function custom_module_field_formatter_info() {
return array(
'custom_date_format' => array(
'label' => t('Custom date format'),
'field types' => array('date', 'datestamp', 'datetime'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function custom_module_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$elements = array();
foreach ($items as $delta => $item) {
$element = array(
'element' => $item,
'field' => $instance,
'display' => $display
);
$elements[$delta] = array(
'#theme' => 'custom_module_formatter_' . $display['type'],
'#element' => $element,
'#field' => $field,
);
}
return $elements;
}
/**
* Implements hook_theme().
*/
function custom_module_theme() {
$themes = array(
'custom_module_formatter_custom_date_format' => array(
'variables' => array(
'element' => NULL,
'field' => NULL,
),
),
);
return $themes;
}
/**
* Implements theme function
*/
function theme_custom_module_formatter_custom_date_format($vars) {
$element = $vars['element'];
$field = $vars['field'];
// Configure custom date
$format_date = 'd M';
$timezone_bd = date_get_timezone_db($field['settings']['tz_handling']);
$publish_date = DateTime::createFromFormat('Y-m-d H:i:s', $element['element']['value'], new DateTimeZone($timezone_bd));
$date = format_date($publish_date->getTimestamp(), 'custom', $format_date);
$date = explode(' ', $date);
// Declare array for render content with custom format and tags
$render_array = array(
'date' => array(
'date-day' => array(
'#prefix' =>'<span class="date-day">',
'#markup' => $date[0],
'#suffix' => '</span>'
),
'date-month' => array(
'#prefix' =>'<span class="date-month">',
'#markup' => $date[1],
'#suffix' => '</span>'
),
),
'#theme_wrapper' => array('container'),
);
return render($render_array['date']);
}