Define Age formatter for date fields

Public

</> CopyGet raw version
php
  1. <?php
  2. /**
  3.  * Define Age formatter for date fields.
  4.  */
  5.  
  6. /**
  7.  * Implements hook_field_formatter_info().
  8.  */
  9. function kalabro_field_formatter_info() {
  10. $formatters = array(
  11. 'date_age' => array(
  12. 'label' => t('Age'),
  13. 'field types' => array('date', 'datestamp', 'datetime'),
  14. 'settings' => array(
  15. 'show_raw_date' => 0,
  16. 'raw_date_format' => 'short',
  17. ),
  18. ),
  19. );
  20.  
  21. return $formatters;
  22. }
  23.  
  24. /**
  25.  * Implements hook_field_formatter_settings_form().
  26.  */
  27. function kalabro_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  28. $display = $instance['display'][$view_mode];
  29. $settings = $display['settings'];
  30. $element = array();
  31.  
  32. if ($display['type'] == 'date_age') {
  33. $id = drupal_html_id('show_raw_date');
  34. $element['show_raw_date'] = array(
  35. '#type' => 'checkbox',
  36. '#title' => t('Show full date after age'),
  37. '#default_value' => $settings['show_raw_date'],
  38. '#attributes' => array('id' => $id),
  39. );
  40. $element['raw_date_format'] = array(
  41. '#type' => 'select',
  42. '#title' => t('Date format'),
  43. '#default_value' => $settings['raw_date_format'],
  44. '#options' => date_format_type_options(),
  45. '#states' => array(
  46. 'visible' => array(
  47. '#' . $id => array('checked' => TRUE),
  48. ),
  49. ),
  50. );
  51. }
  52. return $element;
  53. }
  54.  
  55. /**
  56.  * Implements hook_field_formatter_settings_summary().
  57.  */
  58. function kalabro_field_formatter_settings_summary($field, $instance, $view_mode) {
  59. $display = $instance['display'][$view_mode];
  60. $settings = $display['settings'];
  61.  
  62. $value = $settings['show_raw_date'] ? t('yes') : t('no');
  63. $summary = t('Show full date after age') . ': <em>' . $value . '</em>';
  64.  
  65. return $summary;
  66. }
  67.  
  68. /**
  69.  * Implements hook_field_formatter_view().
  70.  */
  71. function kalabro_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  72. $element = array();
  73. $settings = $display['settings'];
  74.  
  75. if ($display['type'] == 'date_age') {
  76. foreach ($items as $delta => $item) {
  77. if (!empty($item['value'])) {
  78. $age = _ij_main_count_age($item['value']);
  79. $element[$delta] = array('#markup' => $age);
  80. if ($settings['show_raw_date']) {
  81. $element[$delta]['#suffix'] = ' (' . format_date(strtotime($item['value']), $settings['raw_date_format']) . ')';
  82. }
  83. }
  84. }
  85. }
  86.  
  87. return $element;
  88. }
  89.  
  90. /**
  91.  * Helper for age count.
  92.  */
  93. function _kalabro_count_age($date_str) {
  94. $diff = date_diff(new DateTime($date_str), new DateTime());
  95. return $diff->y;
  96. }

Comments

ooops's picture

Pretty cool ! thanks for sharing:)