Projet

Général

Profil

Paste
Télécharger (3,35 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / translation / views_handler_relationship_translation.inc @ 4003efde

1
<?php
2

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

    
8
/**
9
 * Handles relationships for content translation sets and provides multiple
10
 * options.
11
 *
12
 * @ingroup views_relationship_handlers
13
 */
14
class views_handler_relationship_translation extends views_handler_relationship {
15

    
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public function option_definition() {
20
    $options = parent::option_definition();
21
    $options['language'] = array('default' => 'current');
22

    
23
    return $options;
24
  }
25

    
26
  /**
27
   * Add a translation selector.
28
   */
29
  public function options_form(&$form, &$form_state) {
30
    parent::options_form($form, $form_state);
31

    
32
    $options = array(
33
      'all' => t('All'),
34
      'current' => t('Current language'),
35
      'default' => t('Default language'),
36
    );
37
    $options = array_merge($options, views_language_list());
38
    $form['language'] = array(
39
      '#type' => 'select',
40
      '#options' => $options,
41
      '#default_value' => $this->options['language'],
42
      '#title' => t('Translation option'),
43
      '#description' => t('The translation options allows you to select which translation or translations in a translation set join on. Select "Current language" or "Default language" to join on the translation in the current or default language respectively. Select a specific language to join on a translation in that language. If you select "All", each translation will create a new row, which may appear to cause duplicates.'),
44
    );
45
  }
46

    
47
  /**
48
   * Called to implement a relationship in a query.
49
   */
50
  public function query() {
51
    // Figure out what base table this relationship brings to the party.
52
    $table_data = views_fetch_data($this->definition['base']);
53
    $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
54

    
55
    $this->ensure_my_table();
56

    
57
    $def = $this->definition;
58
    $def['table'] = $this->definition['base'];
59
    $def['field'] = $base_field;
60
    $def['left_table'] = $this->table_alias;
61
    $def['left_field'] = $this->field;
62
    if (!empty($this->options['required'])) {
63
      $def['type'] = 'INNER';
64
    }
65

    
66
    $def['extra'] = array();
67
    if ($this->options['language'] != 'all') {
68
      switch ($this->options['language']) {
69
        case 'current':
70
          $def['extra'][] = array(
71
            'field' => 'language',
72
            'value' => '***CURRENT_LANGUAGE***',
73
          );
74
          break;
75

    
76
        case 'default':
77
          $def['extra'][] = array(
78
            'field' => 'language',
79
            'value' => '***DEFAULT_LANGUAGE***',
80
          );
81
          break;
82

    
83
        // Other values will be the language codes.
84
        default:
85
          $def['extra'][] = array(
86
            'field' => 'language',
87
            'value' => $this->options['language'],
88
          );
89
          break;
90
      }
91
    }
92

    
93
    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
94
      $join = new $def['join_handler'];
95
    }
96
    else {
97
      $join = new views_join();
98
    }
99

    
100
    $join->definition = $def;
101
    $join->extra = "(%alias.tnid != 0  OR ({$def['left_table']}.tnid = 0 AND %alias.nid = {$def['left_table']}.nid))";
102
    $join->construct();
103
    $join->adjusted = TRUE;
104

    
105
    // Use a short alias for this.
106
    $alias = $def['table'] . '_' . $this->table;
107

    
108
    $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
109
  }
110

    
111
}