handler_field_saved_search_name.inc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiSavedSearchesViewsHandlerFieldName class.
  5. */
  6. /**
  7. * Views field handler for displaying a saved search's name, optionally linked to the search page.
  8. */
  9. class SearchApiSavedSearchesViewsHandlerFieldName extends views_handler_field {
  10. /**
  11. * The ID of the currently rendered search.
  12. *
  13. * @var int|null
  14. */
  15. protected $currentSearchId;
  16. public function option_definition() {
  17. $options = parent::option_definition();
  18. $options['link_to_page'] = array('default' => TRUE);
  19. return $options;
  20. }
  21. public function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $form['link_to_page'] = array(
  24. '#type' => 'checkbox',
  25. '#title' => t('Link to search page'),
  26. '#default_value' => $this->options['link_to_page'],
  27. );
  28. }
  29. public function render($values) {
  30. $this->currentSearchId = isset($values->id) ? $values->id : NULL;
  31. return parent::render($values);
  32. }
  33. public function render_text($alter) {
  34. if ($this->options['link_to_page'] && $this->currentSearchId) {
  35. $search = search_api_saved_search_load($this->currentSearchId);
  36. if ($search && search_api_saved_search_edit_access(NULL, $search) && !empty($search->options['page'])) {
  37. $alter['make_link'] = TRUE;
  38. $page = $search->options['page'] + array(
  39. 'path' => NULL,
  40. 'query' => array(),
  41. );
  42. $alter['path'] = $page['path'];
  43. if ($page['query']) {
  44. $query = urldecode(drupal_http_build_query($page['query']));
  45. $alter['path'] .= '?' . $query;
  46. }
  47. }
  48. }
  49. return parent::render_text($alter);
  50. }
  51. }