Search code examples
drupaldrupal-7view

Views exposed filter on node language, used on 2 different sites, with a different language list


my problem :

I've a drupal website used for 2 sites (drupal multi-site). These 2 sites are using 2 different databases, and the most important, has different languages. One with "french, english, german, italian, ...", one with "english" only.

I've created a views to manage a content type, and exposed a filter forms containing a node language filter.

enter image description here

The problem is when you export the views and use it in another website using a different language list, the default value is get from the view definition (it's done in views_handler_filter_in_operator, in the value_form(&$form, &$form_state) function, with an array_shift on the values) The problem is the values are hardcoded, it's not something which says "list all the available languages". When I use this view on the second website (with the english language by default), the default value "french", is used (cause the views definition) and the value is refused, causing an error.

enter image description here

How could I create another views filter handler to not have a hardcoded list of values, but an automatic list getting their values from the current available languages.

Here is an extract of the view export :

$handler->display->display_options['filters']['language']['value'] = array(
'fr' => 'fr',
'en' => 'en',
'de' => 'de',
'it' => 'it',
'es' => 'es',
'nl' => 'nl',

);

Many thanks for your help, it's a bit long and complex to explain :D


Solution

  • The solution

    The solution is to set the filter as not required, so you can avoid to select values for the filter. In this case, the list will be filled with the current languages, as expected. The exported view doesn't contain any hard coded values.

    enter image description here

    To go a bit further

    if you want only the current languages, but not these values :

    • 'CURRENT_LANGUAGE' => t("Current user's language" ,
    • 'DEFAULT_LANGUAGE' => t("Default site language"),
    • LANGUAGE_NONE => t('No language')

    you can create a custom filter handler, very simple, and only override the get_value_options function :

    Add this line in your module .info file :

    files[] = views/views_handler_filter_node_enabled_language.inc
    

    Implements a hook_views_data_alter

    /**
     * Implements hook_views_data_alter().
     *
     * Add translation information to the node table.
     */
    function YOURMODULE_views_data_alter(&$data) {
      $data['node']['enabled_languages'] = array(
        'title' => t('Enabled languages'),
        'help' => t('The language the content is in.
                     The filter values are only the enabled languages.'),
        // The real field the query filter on
        'real field' => 'language',
        'filter' => array(
          'handler' => 'views_handler_filter_node_enabled_language',
        ),
      );    
    }
    

    Create you handler class :

    /**
     * Filter by language.
     *
     * @ingroup views_filter_handlers
     */
    class views_handler_filter_node_enabled_language extends views_handler_filter_in_operator     {
      function get_value_options() {
        if (!isset($this->value_options)) {
          $this->value_title = t('Language');
          $this->value_options = views_language_list();
        }
      }
    }