Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / locale / views_handler_sort_node_language.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains .
6
 */
7

    
8
/**
9
 * Sort handler that allows sorting on a specific language.
10
 *
11
 * @ingroup views_sort_handlers
12
 */
13
class views_handler_sort_node_language extends views_handler_sort {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function can_expose() {
19
    return FALSE;
20
  }
21

    
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public function query() {
26
    if (isset($this->options['language'])) {
27
      $langcode = $this->get_system_langcode($this->options['language']);
28
      // Validate the langcode.
29
      if (preg_match('/^[a-z0-9\-]+$/i', $langcode)) {
30
        $this->ensure_my_table();
31
        // See https://stackoverflow.com/questions/14104055/ordering-by-specific-field-value-first
32
        $formula = "{$this->table_alias}_language = '{$langcode}'";
33
        $this->query->add_orderby($this->table_alias, NULL, $this->options['order'], $formula);
34
      }
35
    }
36
  }
37

    
38
  /**
39
   * Converts a views language code into a Drupal language code.
40
   */
41
  public function get_system_langcode($langcode) {
42
    global $language_content;
43
    $default_language = language_default('language');
44
    $system_langcode = str_replace(array(
45
      '***CURRENT_LANGUAGE***',
46
      '***DEFAULT_LANGUAGE***',
47
      ),
48
      array(
49
        $language_content->language,
50
        $default_language,
51
      ),
52
      $langcode);
53
    return $system_langcode;
54
  }
55

    
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function option_definition() {
60
    $options = parent::option_definition();
61
    $options['language'] = array('default' => '***CURRENT_LANGUAGE***');
62
    return $options;
63
  }
64

    
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function admin_summary() {
69
    $summary = parent::admin_summary();
70
    if ($this->options['language']) {
71
      $language_options = $this->get_language_options();
72
      $summary = t('@order, @language', array(
73
        '@order' => $summary,
74
        '@language' => $language_options[$this->options['language']],
75
      ));
76
    }
77
    return $summary;
78
  }
79

    
80
  /**
81
   * Returns languages to sort by.
82
   *
83
   * @return array
84
   *   All the languages.
85
   */
86
  public function get_language_options() {
87
    $languages = array(
88
      '***CURRENT_LANGUAGE***' => t("Current user's language"),
89
      '***DEFAULT_LANGUAGE***' => t("Default site language"),
90
      LANGUAGE_NONE => t('No language'),
91
    );
92
    $languages = array_merge($languages, views_language_list());
93
    return $languages;
94
  }
95

    
96
  /**
97
   * {@inheritdoc}
98
   */
99
  public function show_sort_form(&$form, &$form_state) {
100
    parent::show_sort_form($form, $form_state);
101

    
102
    $form['language'] = array(
103
      '#type' => 'radios',
104
      '#title' => t("Specific language"),
105
      '#description' => t("Choose which specific language to sort by. Not to be confused with the 'Language' sort handler, which sorts by language."),
106
      '#options' => $this->get_language_options(),
107
      '#default_value' => $this->options['language'],
108
    );
109
  }
110

    
111
}