handler_field_saved_search_interval.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiSavedSearchesViewsHandlerFieldInterval class.
  5. */
  6. /**
  7. * Views field handler for displaying a saved search's notification interval.
  8. */
  9. class SearchApiSavedSearchesViewsHandlerFieldInterval extends views_handler_field_time_interval {
  10. /**
  11. * Overrides views_handler_field_time_interval::option_definition().
  12. *
  13. * Adds the custom_labels option.
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['custom_labels'] = array('default' => FALSE);
  18. return $options;
  19. }
  20. /**
  21. * Overrides views_handler_field_time_interval::options_form().
  22. *
  23. * Adds the custom_labels option.
  24. */
  25. public function options_form(&$form, &$form_state) {
  26. $form['custom_labels'] = array(
  27. '#type' => 'checkbox',
  28. '#title' => t('Use custom labels defined in the saved search settings'),
  29. '#default_value' => $this->options['custom_labels'],
  30. );
  31. parent::options_form($form, $form_state);
  32. $form['granularity']['#dependency'] = array(
  33. 'edit-options-custom-labels' => array(0),
  34. );
  35. }
  36. /**
  37. * Overrides views_handler_field::query().
  38. *
  39. * Adds the settings_id field if custom labels should be used.
  40. */
  41. public function query() {
  42. parent::query();
  43. if ($this->options['custom_labels']) {
  44. $this->add_additional_fields(array('settings_id'));
  45. }
  46. }
  47. /**
  48. * Overrides views_handler_field_time_interval::render().
  49. *
  50. * Takes custom labels from the associated settings, if the corresponding
  51. * handler option is enabled.
  52. */
  53. public function render($values) {
  54. if ($this->options['custom_labels']) {
  55. $field = $this->aliases['settings_id'];
  56. if ($values->$field && ($settings = search_api_saved_searches_settings_load($values->$field))) {
  57. $intervals = $settings->getTranslatedOption('interval_options');
  58. if (isset($intervals[$values->{$this->field_alias}])) {
  59. return $intervals[$values->{$this->field_alias}];
  60. }
  61. }
  62. }
  63. if ($values->{$this->field_alias} < 0) {
  64. return t('Never');
  65. }
  66. return parent::render($values);
  67. }
  68. }