Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, array &$form_state)
 {
     $active = \Drupal::linkGenerator()->getActive();
     $language_url = \Drupal::languageManager()->getLanguage(Language::TYPE_URL);
     $unique_id = uniqid();
     $module_path = drupal_get_path('module', 'lang_dropdown');
     $options = $js_settings = $hidden_elements = array();
     $selected_option_language_icon = $language_selected = $language_session_selected = '';
     $form['lang_dropdown_type'] = array('#type' => 'value', '#default_value' => $this->type);
     $form['lang_dropdown_tohome'] = array('#type' => 'value', '#default_value' => $this->settings['tohome']);
     $language_objects = language_list();
     // Support Domain access
     if ($domain_locale_exists = module_exists('domain_locale')) {
         global $_domain;
         $domain_languages = domain_locale_lookup($_domain['domain_id']);
     }
     // Now we iterate on $languages to build the needed options for the select element.
     foreach ($this->languages as $lang_code => $lang_options) {
         // The language is not enabled on this domain
         if ($domain_locale_exists && !array_key_exists($lang_code, $domain_languages)) {
             continue;
         }
         // There is no translation for this language and not all languages are shown
         if (in_array('locale-untranslated', $lang_options['attributes']['class']) && !$this->settings['showall']) {
             continue;
         }
         // Build the options in an assosiative array, so it will be ready for #options in select form element.
         switch ($this->settings['display']) {
             case LANGDROPDOWN_DISPLAY_TRANSLATED:
                 $options += array($lang_code => t($language_objects[$lang_code]->name));
                 break;
             case LANGDROPDOWN_DISPLAY_NATIVE:
                 $options += array($lang_code => t($language_objects[$lang_code]->name, array(), array('langcode' => $lang_code)));
                 break;
             case LANGDROPDOWN_DISPLAY_LANGCODE:
                 $options += array($lang_code => $lang_code);
                 break;
             default:
                 $options += array($lang_code => t($language_objects[$lang_code]->name));
                 break;
         }
         // Identify selected language
         if (isset($lang_options['route_name'])) {
             $variables = array('options' => array());
             if (!empty($link['language'])) {
                 $variables['options']['language'] = $link['language'];
             }
             if ($lang_options['route_name'] == $active['route_name'] && (empty($variables['options']['language']) || $variables['options']['language']->id == $active['language']) && $lang_options['route_parameters'] == $active['parameters']) {
                 $language_selected = $lang_code;
             }
         } elseif (isset($lang_options['href'])) {
             $is_current_path = $lang_options['href'] == current_path() || $lang_options['href'] == '<front>' && drupal_is_front_page();
             $is_current_language = empty($lang_options['language']) || $lang_options['language']->id == $language_url->id;
             if ($is_current_path && $is_current_language) {
                 $language_selected = $lang_code;
             }
         }
         // Identify if session negotiation had set session-active class
         // the trim is needed because of a bug in locale.inc, drupal version <7.24 at least
         if (in_array('session-active', array_map('trim', $lang_options['attributes']['class']))) {
             $language_session_selected = $lang_code;
         }
         // Now we build our hidden form inputs to handle the redirections.
         $href = isset($lang_options['href']) && $this->settings['tohome'] == 0 ? $lang_options['href'] : '<front>';
         if (!isset($lang_options['query'])) {
             $lang_options['query'] = drupal_get_query_parameters();
         }
         $hidden_elements[$lang_code] = array('#type' => 'hidden', '#default_value' => url($href, $lang_options));
         // Handle flags with Language icons module using JS widget.
         if (module_exists('languageicons') && $this->settings['widget']) {
             $languageicons_path = variable_get('languageicons_path', drupal_get_path('module', 'languageicons') . '/flags/*.png');
             $js_settings['languageicons'][$lang_code] = file_create_url(str_replace('*', $lang_code, $languageicons_path));
         }
     }
     // If session-active is set that's the selected language otherwise rely on $language_selected
     $selected_option = $language_session_selected == '' ? $language_selected : $language_session_selected;
     // Icon for the selected language
     if (module_exists('languageicons') && !$this->settings['widget']) {
         $selected_option_language_icon = theme('languageicons_icon', array('language' => (object) array('language' => $selected_option), 'title' => $language_names[$selected_option]));
     }
     // Add required files and settings for JS widget.
     if ($this->settings['widget'] == LANGDROPDOWN_MSDROPDOWN) {
         drupal_add_library('lang_dropdown', 'msdropdown');
         $js_settings += array('widget' => 'msdropdown', 'visibleRows' => $this->settings['msdropdown']['visible_rows'], 'roundedCorner' => $this->settings['msdropdown']['rounded'], 'animStyle' => $this->settings['msdropdown']['animation'], 'event' => $this->settings['msdropdown']['event']);
         $selected_skin = $this->settings['msdropdown']['skin'];
         if ($selected_skin == 'custom') {
             $custom_skin = check_plain($this->settings['msdropdown']['custom_skin']);
             drupal_add_css(_lang_dropdown_get_msdropdown_path() . '/css/msdropdown/' . $custom_skin . '.css');
             $js_settings += array('mainCSS' => $custom_skin);
         } else {
             $skins = _lang_dropdown_get_msdropdown_skins();
             $skin_data = $skins[$selected_skin];
             drupal_add_css($skin_data['file']);
             $js_settings += array('mainCSS' => $skin_data['mainCSS']);
         }
         drupal_add_js(array('lang_dropdown' => array($unique_id => array('jsWidget' => $js_settings))), 'setting');
     } else {
         if ($this->settings['widget'] == LANGDROPDOWN_CHOSEN) {
             drupal_add_library('lang_dropdown', 'chosen');
             $js_settings += array('widget' => 'chosen', 'disable_search' => $this->settings['chosen']['disable_search'], 'no_results_text' => $this->settings['chosen']['no_results_text']);
             drupal_add_js(array('lang_dropdown' => array($unique_id => array('jsWidget' => $js_settings))), 'setting');
         } else {
             if ($this->settings['widget'] == LANGDROPDOWN_DDSLICK) {
                 drupal_add_library('lang_dropdown', 'ddslick');
                 $selected_skin = $this->settings['ddslick']['skin'];
                 if ($selected_skin == 'custom') {
                     $custom_skin = check_plain($this->settings['ddslick']['custom_skin']);
                     drupal_add_css(_lang_dropdown_get_ddslick_path() . '/' . $custom_skin . '.css');
                     $ddsSkin = $custom_skin;
                 } else {
                     $skins = _lang_dropdown_get_ddslick_skins();
                     $skin_data = $skins[$selected_skin];
                     drupal_add_css($skin_data['file']);
                     $ddsSkin = $selected_skin;
                 }
                 $js_settings += array('widget' => 'ddslick', 'width' => $this->settings['width'], 'height' => $this->settings['ddslick']['ddslick_height'], 'showSelectedHTML' => $this->settings['ddslick']['showSelectedHTML'], 'imagePosition' => $this->settings['ddslick']['imagePosition']);
                 drupal_add_js(array('lang_dropdown' => array($unique_id => array('jsWidget' => $js_settings))), 'setting');
             }
         }
     }
     $this->settings['languageicons']['flag_position'] ? $flag_position = '#suffix' : ($flag_position = '#prefix');
     // Now we build the $form array.
     $form['lang_dropdown_select'] = array('#type' => 'select', '#default_value' => isset($selected_option) ? $selected_option : key($options), '#options' => $options, '#attributes' => array('style' => 'width:' . $this->settings['width'] . 'px', 'class' => array('lang-dropdown-select-element'), 'id' => 'lang-dropdown-select-' . $unique_id), '#attached' => array('js' => array($module_path . '/js/lang_dropdown.js'), 'css' => $this->settings['widget'] ? array() : array($module_path . '/css/lang_dropdown.css')));
     if (empty($hidden_elements)) {
         return array();
     }
     $form += $hidden_elements;
     if (module_exists('languageicons')) {
         $form['lang_dropdown_select'][$flag_position] = $selected_option_language_icon;
     }
     $form['#attributes']['class'] = array('lang_dropdown_form', $this->type);
     $form['#attributes']['id'] = 'lang_dropdown_form_' . $unique_id;
     if ($this->settings['widget'] == LANGDROPDOWN_DDSLICK) {
         $form['#attributes']['class'][] = $ddsSkin;
     }
     $form['submit'] = array('#type' => 'submit', '#value' => t('Go'), '#prefix' => "<noscript><div>\n", '#suffix' => "\n</div></noscript>");
     return $form;
 }
Esempio n. 2
0
 /**
  * Overrides \Drupal\block\BlockBase::blockForm().
  */
 public function blockForm($form, &$form_state)
 {
     $form['lang_dropdown'] = array('#type' => 'fieldset', '#title' => t('Language switcher dropdown settings'), '#weight' => 1, '#tree' => TRUE);
     $form['lang_dropdown']['showall'] = array('#type' => 'checkbox', '#title' => t('Show all enabled languages'), '#description' => t('Show all languages in the switcher no matter if there is a translation for the node or not. For languages without translation the switcher will redirect to homepage.'), '#default_value' => $this->configuration['showall']);
     $form['lang_dropdown']['tohome'] = array('#type' => 'checkbox', '#title' => t('Redirect to home on switch'), '#description' => t('When you change language the switcher will redirect to homepage.'), '#default_value' => $this->configuration['tohome']);
     $form['lang_dropdown']['width'] = array('#type' => 'number', '#title' => t('Width of dropdown element'), '#size' => 8, '#maxlength' => 3, '#required' => TRUE, '#field_suffix' => 'px', '#default_value' => $this->configuration['width']);
     $form['lang_dropdown']['display'] = array('#type' => 'select', '#title' => t('Display format'), '#options' => array(LANGDROPDOWN_DISPLAY_TRANSLATED => t('Translated into Current Language'), LANGDROPDOWN_DISPLAY_NATIVE => t('Language Native Name'), LANGDROPDOWN_DISPLAY_LANGCODE => t('Language Code')), '#default_value' => $this->configuration['display']);
     $form['lang_dropdown']['widget'] = array('#type' => 'select', '#title' => t('Output type'), '#options' => array(LANGDROPDOWN_SIMPLE_SELECT => t('Simple HTML select'), LANGDROPDOWN_MSDROPDOWN => t('Marghoob Suleman Dropdown jquery library'), LANGDROPDOWN_CHOSEN => t('Chosen jquery library'), LANGDROPDOWN_DDSLICK => t('ddSlick library')), '#default_value' => $this->configuration['widget']);
     $form['lang_dropdown']['msdropdown'] = array('#type' => 'fieldset', '#title' => t('Marghoob Suleman Dropdown Settings'), '#weight' => 1, '#states' => array('visible' => array(':input[name="settings[lang_dropdown][widget]"]' => array('value' => LANGDROPDOWN_MSDROPDOWN))));
     if (!module_exists('languageicons')) {
         $form['lang_dropdown']['msdropdown']['#description'] = t('This looks better with !languageicons module.', array('!languageicons' => l(t('language icons'), LANGDROPDOWN_LANGUAGEICONS_MOD_URL)));
     }
     if (_lang_dropdown_get_msdropdown_path()) {
         $form['lang_dropdown']['msdropdown']['visible_rows'] = array('#type' => 'select', '#title' => t('Maximum number of visible rows'), '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)), '#default_value' => $this->configuration['msdropdown']['visible_rows']);
         $form['lang_dropdown']['msdropdown']['rounded'] = array('#type' => 'checkbox', '#title' => t('Rounded corners.'), '#default_value' => $this->configuration['msdropdown']['rounded']);
         $form['lang_dropdown']['msdropdown']['animation'] = array('#type' => 'select', '#title' => t('Animation style for dropdown'), '#options' => array('slideDown' => t('Slide down'), 'fadeIn' => t('Fade in'), 'show' => t('Show')), '#default_value' => $this->configuration['msdropdown']['animation']);
         $form['lang_dropdown']['msdropdown']['event'] = array('#type' => 'select', '#title' => t('Event that opens the menu'), '#options' => array('click' => t('Click'), 'mouseover' => t('Mouse Over')), '#default_value' => $this->configuration['msdropdown']['event']);
         $msdSkinOptions = array();
         foreach (_lang_dropdown_get_msdropdown_skins() as $key => $value) {
             $msdSkinOptions[$key] = $value['text'];
         }
         $form['lang_dropdown']['msdropdown']['skin'] = array('#type' => 'select', '#title' => t('Skin'), '#options' => $msdSkinOptions, '#default_value' => $this->configuration['msdropdown']['skin']);
         $form['lang_dropdown']['msdropdown']['custom_skin'] = array('#type' => 'textfield', '#title' => t('Custom skin name'), '#size' => 80, '#maxlength' => 55, '#default_value' => $this->configuration['msdropdown']['custom_skin'], '#states' => array('visible' => array(':input[name="settings[lang_dropdown][msdropdown][skin]"]' => array('value' => 'custom'))));
     } else {
         $form['lang_dropdown']['msdropdown']['#description'] = t('You need to download the !msdropdown and extract the entire contents of the archive into the %path directory on your server.', array('!msdropdown' => l(t('Marghoob Suleman Dropdown JavaScript library'), LANGDROPDOWN_MSDROPDOWN_URL), '%path' => 'sites/all/libraries'));
         $form['lang_dropdown']['msdropdown']['visible_rows'] = array('#type' => 'hidden', '#value' => $this->configuration['msdropdown']['visible_rows']);
         $form['lang_dropdown']['msdropdown']['rounded'] = array('#type' => 'hidden', '#value' => $this->configuration['msdropdown']['rounded']);
         $form['lang_dropdown']['msdropdown']['animation'] = array('#type' => 'hidden', '#value' => $this->configuration['msdropdown']['animation']);
         $form['lang_dropdown']['msdropdown']['event'] = array('#type' => 'hidden', '#value' => $this->configuration['msdropdown']['event']);
         $form['lang_dropdown']['msdropdown']['skin'] = array('#type' => 'hidden', '#value' => $this->configuration['msdropdown']['skin']);
         $form['lang_dropdown']['msdropdown']['custom_skin'] = array('#type' => 'hidden', '#value' => $this->configuration['msdropdown']['custom_skin']);
     }
     $form['lang_dropdown']['languageicons'] = array('#type' => 'fieldset', '#title' => t('Language icons settings'), '#weight' => 1, '#states' => array('visible' => array(':input[name="settings[lang_dropdown][widget]"]' => array('value' => LANGDROPDOWN_SIMPLE_SELECT))));
     if (module_exists('languageicons')) {
         $form['lang_dropdown']['languageicons']['flag_position'] = array('#type' => 'select', '#title' => t('Position of the flag when the dropdown is show just as a select'), '#options' => array(LANGDROPDOWN_FLAG_POSITION_BEFORE => t('Before'), LANGDROPDOWN_FLAG_POSITION_AFTER => t('After')), '#default_value' => $this->configuration['languageicons']['flag_position']);
     } else {
         $form['lang_dropdown']['languageicons']['#description'] = t('Enable !languageicons module to show a flag of the selected language before or after the select box.', array('!languageicons' => l(t('language icons'), LANGDROPDOWN_LANGUAGEICONS_MOD_URL)));
         $form['lang_dropdown']['languageicons']['flag_position'] = array('#type' => 'hidden', '#value' => $this->configuration['languageicons']['flag_position']);
     }
     $form['lang_dropdown']['chosen'] = array('#type' => 'fieldset', '#title' => t('Chosen settings'), '#weight' => 2, '#states' => array('visible' => array(':input[name="settings[lang_dropdown][widget]"]' => array('value' => LANGDROPDOWN_CHOSEN))));
     if (!module_exists('chosen') && _lang_dropdown_get_chosen_path()) {
         $form['lang_dropdown']['chosen']['disable_search'] = array('#type' => 'checkbox', '#title' => t('Disable search box'), '#default_value' => $this->configuration['chosen']['disable_search']);
         $form['lang_dropdown']['chosen']['no_results_text'] = array('#type' => 'textfield', '#title' => t('No Result Text'), '#description' => t('Text to show when no result is found on search.'), '#default_value' => $this->configuration['chosen']['no_results_text'], '#states' => array('visible' => array(':input[name="settings[lang_dropdown][chosen][disable_search]"]' => array('checked' => FALSE))));
     } else {
         $form['lang_dropdown']['chosen']['disable_search'] = array('#type' => 'hidden', '#value' => $this->configuration['chosen']['disable_search']);
         $form['lang_dropdown']['chosen']['no_results_text'] = array('#type' => 'hidden', '#value' => $this->configuration['chosen']['no_results_text']);
         if (module_exists('chosen')) {
             $form['lang_dropdown']['chosen']['#description'] = t('If you are already using the !chosenmod you must just choose to output language dropdown as a simple HTML select and allow !chosenmod to turn it into a chosen style select.', array('!chosenmod' => l(t('Chosen module'), LANGDROPDOWN_CHOSEN_MOD_URL)));
         } else {
             $form['lang_dropdown']['chosen']['#description'] = t('You need to download the !chosen and extract the entire contents of the archive into the %path directory on your server.', array('!chosen' => l(t('Chosen library'), LANGDROPDOWN_CHOSEN_WEB_URL), '%path' => 'sites/all/libraries'));
         }
     }
     $form['lang_dropdown']['ddslick'] = array('#type' => 'fieldset', '#title' => t('ddSlick settings'), '#weight' => 3, '#states' => array('visible' => array(':input[name="settings[lang_dropdown][widget]"]' => array('value' => LANGDROPDOWN_DDSLICK))));
     if (_lang_dropdown_get_ddslick_path()) {
         $form['lang_dropdown']['ddslick']['ddslick_height'] = array('#type' => 'number', '#title' => t('Height'), '#description' => t('Height in px for the drop down options i.e. 300. The scroller will automatically be added if options overflows the height. Use 0 for full height.'), '#size' => 8, '#maxlength' => 3, '#field_suffix' => 'px', '#default_value' => $this->configuration['ddslick']['ddslick_height']);
         if (module_exists('languageicons')) {
             $form['lang_dropdown']['ddslick']['showSelectedHTML'] = array('#type' => 'checkbox', '#title' => t('Show Flag'), '#default_value' => $this->configuration['ddslick']['showSelectedHTML']);
             $form['lang_dropdown']['ddslick']['imagePosition'] = array('#type' => 'select', '#title' => t('Flag Position'), '#options' => array(LANGDROPDOWN_DDSLICK_LEFT => t('left'), LANGDROPDOWN_DDSLICK_RIGHT => t('right')), '#default_value' => $this->configuration['ddslick']['imagePosition'], '#states' => array('visible' => array(':input[name="settings[lang_dropdown][ddslick][showSelectedHTML]"]' => array('checked' => TRUE))));
         } else {
             $form['lang_dropdown']['ddslick']['#description'] = t('This looks better with !languageicons module.', array('!languageicons' => l(t('language icons'), LANGDROPDOWN_LANGUAGEICONS_MOD_URL)));
             $form['lang_dropdown']['ddslick']['showSelectedHTML'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['showSelectedHTML']);
             $form['lang_dropdown']['ddslick']['imagePosition'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['imagePosition']);
         }
         $ddsSkinOptions = array();
         foreach (_lang_dropdown_get_ddslick_skins() as $key => $value) {
             $ddsSkinOptions[$key] = $value['text'];
         }
         $form['lang_dropdown']['ddslick']['skin'] = array('#type' => 'select', '#title' => t('Skin'), '#options' => $ddsSkinOptions, '#default_value' => $this->configuration['ddslick']['skin']);
         $form['lang_dropdown']['ddslick']['custom_skin'] = array('#type' => 'textfield', '#title' => t('Custom skin name'), '#size' => 80, '#maxlength' => 55, '#default_value' => $this->configuration['ddslick']['custom_skin'], '#states' => array('visible' => array(':input[name="settings[lang_dropdown][ddslick][skin]"]' => array('value' => 'custom'))));
     } else {
         $form['lang_dropdown']['ddslick']['#description'] = t('You need to download the !ddslick and extract the entire contents of the archive into the %path directory on your server.', array('!ddslick' => l(t('ddSlick library'), LANGDROPDOWN_DDSLICK_WEB_URL), '%path' => 'sites/all/libraries/ddslick'));
         $form['lang_dropdown']['ddslick']['ddslick_height'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['ddslick_height']);
         $form['lang_dropdown']['ddslick']['showSelectedHTML'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['showSelectedHTML']);
         $form['lang_dropdown']['ddslick']['imagePosition'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['imagePosition']);
         $form['lang_dropdown']['ddslick']['skin'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['skin']);
         $form['lang_dropdown']['ddslick']['custom_skin'] = array("#type" => 'hidden', "#value" => $this->configuration['ddslick']['custom_skin']);
     }
     // configuration options that allow to hide a specific language to specific roles
     $form['lang_dropdown']['hideout'] = array('#type' => 'fieldset', '#title' => t('Hide language settings'), '#description' => t('Select which languages you want to hide to specific roles.'), '#weight' => 4);
     $languages = language_list();
     $roles = user_roles();
     $role_names = array();
     $role_languages = array();
     foreach ($roles as $rid => $role) {
         // Retrieve role names for columns.
         $role_names[$rid] = String::checkPlain($role->label());
         // Fetch languages for the roles.
         $role_languages[$rid] = isset($this->configuration['hidden_languages'][$rid]) ? $this->configuration['hidden_languages'][$rid] : array();
     }
     // Store $role_names for use when saving the data.
     $form['lang_dropdown']['hideout']['role_names'] = array('#type' => 'value', '#value' => $role_names);
     $form['lang_dropdown']['hideout']['languages'] = array('#type' => 'table', '#header' => array($this->t('Languages')), '#id' => 'hidden_languages_table', '#sticky' => TRUE);
     foreach ($role_names as $name) {
         $form['lang_dropdown']['hideout']['languages']['#header'][] = array('data' => $name, 'class' => array('checkbox'));
     }
     foreach ($languages as $code => $language) {
         $options[$code] = '';
         $form['lang_dropdown']['hideout']['languages'][$code]['language'] = array('#type' => 'item', '#markup' => $language->name);
         foreach ($role_names as $rid => $role) {
             $form['lang_dropdown']['hideout']['languages'][$code][$rid] = array('#title' => $rid . ': ' . $language->name, '#title_display' => 'invisible', '#wrapper_attributes' => array('class' => array('checkbox')), '#type' => 'checkbox', '#default_value' => in_array($code, $role_languages[$rid]) ? 1 : 0, '#attributes' => array('class' => array('rid-' . $rid)));
         }
     }
     return $form;
 }