entity_views_handler_field_duration.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Contains the entity_views_handler_field_duration class.
  5. */
  6. /**
  7. * A handler to provide proper displays for duration properties retrieved via data selection.
  8. *
  9. * This handler may only be used in conjunction with data selection based Views
  10. * tables or other base tables using a query plugin that supports data
  11. * selection.
  12. *
  13. * @see entity_views_field_definition()
  14. * @ingroup views_field_handlers
  15. */
  16. class entity_views_handler_field_duration extends views_handler_field {
  17. /**
  18. * Stores the entity type of the result entities.
  19. */
  20. public $entity_type;
  21. /**
  22. * Stores the result entities' metadata wrappers.
  23. */
  24. public $wrappers = array();
  25. /**
  26. * Stores the current value when rendering list fields.
  27. */
  28. public $current_value;
  29. /**
  30. * Overridden to add the field for the entity ID (if necessary).
  31. */
  32. public function query() {
  33. EntityFieldHandlerHelper::query($this);
  34. }
  35. /**
  36. * Adds a click-sort to the query.
  37. */
  38. public function click_sort($order) {
  39. EntityFieldHandlerHelper::click_sort($this, $order);
  40. }
  41. /**
  42. * Load the entities for all rows that are about to be displayed.
  43. */
  44. public function pre_render(&$values) {
  45. parent::pre_render($values);
  46. EntityFieldHandlerHelper::pre_render($this, $values);
  47. }
  48. /**
  49. * Overridden to use a metadata wrapper.
  50. */
  51. public function get_value($values, $field = NULL) {
  52. return EntityFieldHandlerHelper::get_value($this, $values, $field);
  53. }
  54. public function option_definition() {
  55. $options = parent::option_definition();
  56. $options += EntityFieldHandlerHelper::option_definition($this);
  57. $options['format_interval'] = array('default' => TRUE);
  58. $options['granularity'] = array('default' => 2);
  59. $options['prefix'] = array('default' => '', 'translatable' => TRUE);
  60. $options['suffix'] = array('default' => '', 'translatable' => TRUE);
  61. return $options;
  62. }
  63. public function options_form(&$form, &$form_state) {
  64. parent::options_form($form, $form_state);
  65. EntityFieldHandlerHelper::options_form($this, $form, $form_state);
  66. $form['format_interval'] = array(
  67. '#type' => 'checkbox',
  68. '#title' => t('Format interval'),
  69. '#description' => t('If checked, the value will be formatted as a time interval. Otherwise, just the number of seconds will be displayed.'),
  70. '#default_value' => $this->options['format_interval'],
  71. );
  72. $form['granularity'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Granularity'),
  75. '#default_value' => $this->options['granularity'],
  76. '#description' => t('Specify how many different units to display.'),
  77. '#dependency' => array('edit-options-format-interval' => array(TRUE)),
  78. '#size' => 2,
  79. );
  80. $form['prefix'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Prefix'),
  83. '#default_value' => $this->options['prefix'],
  84. '#description' => t('Text to put before the duration text.'),
  85. );
  86. $form['suffix'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Suffix'),
  89. '#default_value' => $this->options['suffix'],
  90. '#description' => t('Text to put after the duration text.'),
  91. );
  92. }
  93. /**
  94. * Render the field.
  95. *
  96. * @param $values
  97. * The values retrieved from the database.
  98. */
  99. public function render($values) {
  100. return EntityFieldHandlerHelper::render($this, $values);
  101. }
  102. /**
  103. * Render a single field value.
  104. */
  105. public function render_single_value($value, $values) {
  106. if ($this->options['format_interval']) {
  107. $value = format_interval($value, (int) $this->options['granularity']);
  108. }
  109. // Value sanitization is handled by the wrapper, see
  110. // EntityFieldHandlerHelper::get_value().
  111. return $this->sanitize_value($this->options['prefix'], 'xss') .
  112. $value .
  113. $this->sanitize_value($this->options['suffix'], 'xss');
  114. }
  115. }