Projet

Général

Profil

Paste
Télécharger (2,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / search / views_handler_field_search_score.inc @ 5d12d676

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_search_score.
6
 */
7

    
8
/**
9
 * Field handler to provide simple renderer that allows linking to a node.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_search_score extends views_handler_field_numeric {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    $options['alternate_sort'] = array('default' => '');
22
    $options['alternate_order'] = array('default' => 'asc');
23

    
24
    return $options;
25
  }
26

    
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public function options_form(&$form, &$form_state) {
31
    $style_options = $this->view->display_handler->get_option('style_options');
32
    if (isset($style_options['default']) && $style_options['default'] == $this->options['id']) {
33
      $handlers = $this->view->display_handler->get_handlers('field');
34
      $options = array('' => t('No alternate'));
35
      foreach ($handlers as $id => $handler) {
36
        $options[$id] = $handler->ui_name();
37
      }
38

    
39
      $form['alternate_sort'] = array(
40
        '#type' => 'select',
41
        '#title' => t('Alternative sort'),
42
        '#description' => t('Pick an alternative default table sort field to use when the search score field is unavailable.'),
43
        '#options' => $options,
44
        '#default_value' => $this->options['alternate_sort'],
45
      );
46

    
47
      $form['alternate_order'] = array(
48
        '#type' => 'select',
49
        '#title' => t('Alternate sort order'),
50
        '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
51
        '#default_value' => $this->options['alternate_order'],
52
      );
53
    }
54

    
55
    parent::options_form($form, $form_state);
56
  }
57

    
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function query() {
62
    // Check to see if the search filter added 'score' to the table.
63
    // Our filter stores it as $handler->search_score -- and we also
64
    // need to check its relationship to make sure that we're using the same
65
    // one or obviously this won't work.
66
    foreach ($this->view->filter as $handler) {
67
      if (isset($handler->search_score) && $handler->relationship == $this->relationship) {
68
        $this->field_alias = $handler->search_score;
69
        $this->table_alias = $handler->table_alias;
70
        return;
71
      }
72
    }
73

    
74
    // Hide this field if no search filter is in place.
75
    $this->options['exclude'] = TRUE;
76
    if (!empty($this->options['alternate_sort'])) {
77
      if (isset($this->view->style_plugin->options['default']) && $this->view->style_plugin->options['default'] == $this->options['id']) {
78
        // Since the style handler initiates fields, we plug these values right
79
        // into the active handler.
80
        $this->view->style_plugin->options['default'] = $this->options['alternate_sort'];
81
        $this->view->style_plugin->options['order'] = $this->options['alternate_order'];
82
      }
83
    }
84
  }
85

    
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function render($values) {
90
    // Only render if we exist.
91
    if (isset($this->table_alias)) {
92
      return parent::render($values);
93
    }
94
  }
95

    
96
}