| Commit | Line | Data |
|---|---|---|
| ddda5392 | 1 | <?php |
| ddda5392 JR |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Views support for Internationalization (i18n) package | |
| 6 | * | |
| 7 | * This module uses i18n module namespace to add some views hooks | |
| 8 | * | |
| 9 | * @author Jose A. Reyero, 2007 | |
| 10 | */ | |
| 11 | ||
| 12 | /** | |
| 13 | * Views integration | |
| 14 | */ | |
| 15 | function i18n_views_tables() { | |
| 16 | $tables['i18n'] = array( | |
| 17 | 'name' => 'i18n_node', | |
| 18 | 'join' => array( | |
| 19 | 'left' => array( | |
| 20 | 'table' => 'node', | |
| 21 | 'field' => 'nid' | |
| 22 | ), | |
| 23 | 'right' => array( | |
| 24 | 'field' => 'nid' | |
| 25 | ) | |
| 26 | ) | |
| 27 | ); | |
| 28 | $tables['i18n']['fields']['language'] = array( | |
| 29 | 'name' => t('Internationalization: Language'), | |
| 30 | 'sortable' => true, | |
| 31 | 'handler' => 'i18n_views_handler_language', | |
| 32 | ); | |
| 33 | $tables['i18n']['filters']['language'] = array( | |
| 34 | 'name' => t('Internationalization: Language'), | |
| 35 | 'list-type' => 'list', | |
| 36 | 'value-type' => 'array', | |
| 37 | 'list' => 'i18n_views_language_list', | |
| 38 | 'operator' => 'views_handler_operator_or', | |
| 39 | 'help' => t('Enabled languages for content.'), | |
| 40 | ); | |
| 41 | $tables['i18n']['filters']['extlanguage'] = array( | |
| 42 | 'field' => 'language', | |
| 43 | 'name' => t('Internationalization: Language (extended)'), | |
| 44 | 'list-type' => 'list', | |
| 45 | 'value-type' => 'array', | |
| 46 | 'list' => 'i18n_views_language_list_all', | |
| 47 | 'operator' => 'views_handler_operator_or', | |
| 48 | 'help' => t('All defined languages for content.'), | |
| 49 | ); | |
| 50 | $tables['i18n']['filters']['selection'] = array( | |
| 51 | 'name' => t('Internationalization: Selection'), | |
| 52 | 'handler' => 'i18n_views_filter_handler', | |
| 53 | 'list-type' => 'select', | |
| 54 | 'operator' => array('=' => t('Is')), | |
| 55 | 'list' => '_i18n_selection_mode', | |
| 56 | 'help' => t('Content language.'), | |
| 57 | 'cacheable' => 'no' // This query condition cannot be cached | |
| 58 | ); | |
| 59 | return $tables; | |
| 60 | } | |
| 61 | ||
| 62 | function i18n_views_language_list($a, $b){ | |
| 818652bd | 63 | return locale_language_list(); |
| ddda5392 JR |
64 | } |
| 65 | function i18n_views_language_list_all($a, $b){ | |
| 818652bd | 66 | return locale_language_list('name', TRUE); |
| ddda5392 JR |
67 | } |
| 68 | function i18n_views_handler_language($fieldinfo, $fielddata, $value, $data) { | |
| 818652bd | 69 | $languages = locale_language_list('name', TRUE); |
| ddda5392 JR |
70 | return $languages[$value]; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Filter handler callback. Manages language selection mode | |
| 75 | */ | |
| 76 | function i18n_views_filter_handler($filterdata, $filterinfo, &$query) { | |
| 77 | if ($filterdata == 'handler' && $filterinfo['field'] == 'i18n.selection' && $mode = $filterinfo['value']) { | |
| 78 | // If this filter is used, rollback value set in pre_query and set the new one. | |
| 79 | i18n_selection_mode('reset'); | |
| 80 | i18n_selection_mode($mode); | |
| 81 | } | |
| 82 | } | |
| 83 | /** | |
| 84 | * Implementation of hook_views_pre_query(). | |
| 85 | * | |
| 86 | * Disable language conditions for views. This is called before filter handlers | |
| 87 | */ | |
| 88 | function i18n_views_pre_query(&$view) { | |
| 89 | // If any language filter, the language selection mode will be 'off' | |
| 90 | foreach($view->exposed_filter as $filter) { | |
| 91 | if($filter['field'] == 'i18n.language') { | |
| 92 | i18n_selection_mode('off'); | |
| 93 | return; | |
| 94 | } | |
| 95 | } | |
| 96 | // If no filter, sets the one from settings for this view | |
| 97 | i18n_selection_mode(variable_get('i18n_selection_mode', 'simple')); | |
| 98 | } | |
| 99 | /** | |
| 100 | * Implementation of hook_views_pre_view(). | |
| 101 | * | |
| 102 | * If used language selection filter, reset query rewriting again | |
| 103 | */ | |
| 104 | function i18n_views_pre_view(&$view, &$items) { | |
| 105 | i18n_selection_mode('reset'); | |
| 9ecdf8dc JR |
106 | } |
| 107 | ||
| 108 | /** | |
| 109 | * Implementation of hook_form_alter() | |
| 110 | * | |
| 111 | * @ TODO Copied from i18ntaxonomy, update all this | |
| 112 | */ | |
| 113 | function i18nviews_form_alter(&$form, $form_state, $form_id) { | |
| 114 | ||
| 115 | if ($form_id == 'views_filters' && $translate = variable_get('i18ntaxonomy_vocabularies', array())) { | |
| 116 | // We only translate exposed filters here | |
| 117 | $view = $form['view']['#value']; | |
| 118 | if($view->exposed_filter) { | |
| 119 | foreach($view->exposed_filter as $index => $filter) { | |
| 120 | $matches = array(); | |
| 121 | if($filter['field'] == 'term_node.tid') { | |
| 122 | // That's a full taxonomy box. Translate options: arary(tid => "Vocabulary: Term") | |
| 123 | // First, we get a translated list. Then we replace on the options array | |
| 124 | $replace = _i18ntaxonomy_vocabulary_terms(array_keys($translate)); | |
| 125 | foreach($replace as $tid => $name) { | |
| 126 | if(isset($form["filter$index"]['#options'][$tid])) { | |
| 127 | $form["filter$index"]['#options'][$tid] = $name; | |
| 128 | } | |
| 129 | } | |
| 130 | } elseif(preg_match("/term_node_(\d+)\.tid/", $filter['field'], $matches)) { | |
| 131 | $vid = $matches[1]; | |
| 132 | if ($translate[$vid]) { | |
| 133 | // Translate this vocabulary terms, field name is filter$index vid = $matches[1] | |
| 134 | foreach ($form["filter$index"]['#options'] as $value => $option) { | |
| 135 | if ($value != '**ALL**') { // ALL option should be already localized | |
| 136 | // This may be an object with an option property being an array (tid => name) | |
| 137 | if (is_object($option) && is_array($option->option)) { | |
| 138 | foreach (array_keys($option->option) as $tid) { | |
| 139 | $option->option[$tid] = t($option->option[$tid]); | |
| 140 | } | |
| 141 | $form["filter$index"]['#options'][$value] = $option; | |
| 142 | // But it used to be a plain string, so let's keep this just in case... | |
| 143 | } elseif(is_string($option)) { | |
| 144 | $form["filter$index"]['#options'][$value] = t($option); | |
| 145 | } | |
| 146 | } | |
| 147 | } | |
| 148 | } | |
| 149 | } | |
| 150 | } | |
| 151 | } | |
| 152 | } | |
| ddda5392 | 153 | } |