views_handler_relationship_translation.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_relationship_translation.
  5. */
  6. /**
  7. * Handles relationships for content translation sets and provides multiple
  8. * options.
  9. *
  10. * @ingroup views_relationship_handlers
  11. */
  12. class views_handler_relationship_translation extends views_handler_relationship {
  13. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['language'] = array('default' => 'current');
  16. return $options;
  17. }
  18. /**
  19. * Add a translation selector.
  20. */
  21. function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $options = array(
  24. 'all' => t('All'),
  25. 'current' => t('Current language'),
  26. 'default' => t('Default language'),
  27. );
  28. $options = array_merge($options, locale_language_list());
  29. $form['language'] = array(
  30. '#type' => 'select',
  31. '#options' => $options,
  32. '#default_value' => $this->options['language'],
  33. '#title' => t('Translation option'),
  34. '#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.'),
  35. );
  36. }
  37. /**
  38. * Called to implement a relationship in a query.
  39. */
  40. function query() {
  41. // Figure out what base table this relationship brings to the party.
  42. $table_data = views_fetch_data($this->definition['base']);
  43. $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
  44. $this->ensure_my_table();
  45. $def = $this->definition;
  46. $def['table'] = $this->definition['base'];
  47. $def['field'] = $base_field;
  48. $def['left_table'] = $this->table_alias;
  49. $def['left_field'] = $this->field;
  50. if (!empty($this->options['required'])) {
  51. $def['type'] = 'INNER';
  52. }
  53. $def['extra'] = array();
  54. if ($this->options['language'] != 'all') {
  55. switch ($this->options['language']) {
  56. case 'current':
  57. $def['extra'][] = array(
  58. 'field' => 'language',
  59. 'value' => '***CURRENT_LANGUAGE***',
  60. );
  61. break;
  62. case 'default':
  63. $def['extra'][] = array(
  64. 'field' => 'language',
  65. 'value' => '***DEFAULT_LANGUAGE***',
  66. );
  67. break;
  68. // Other values will be the language codes.
  69. default:
  70. $def['extra'][] = array(
  71. 'field' => 'language',
  72. 'value' => $this->options['language'],
  73. );
  74. break;
  75. }
  76. }
  77. if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
  78. $join = new $def['join_handler'];
  79. }
  80. else {
  81. $join = new views_join();
  82. }
  83. $join->definition = $def;
  84. $join->construct();
  85. $join->adjusted = TRUE;
  86. // use a short alias for this:
  87. $alias = $def['table'] . '_' . $this->table;
  88. $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
  89. }
  90. }