Page 1 of 1

Howto only getlanguages for which user has permissions?

Posted: Fri Feb 14, 2020 2:07 pm
by geejay
Hi

I am building a module where I need a list of available languages. getLanguages gets all active languages. The problem is however that I am restricting permissions in some languages to specific user groups.

How can I get all active languages for which the user has permissions?

Thanks for any hints.

Re: Howto only getlanguages for which user has permissions?

Posted: Sat Feb 15, 2020 7:21 am
by infograf768
Are you using Joomla native multilingual feature?

Re: Howto only getlanguages for which user has permissions?

Posted: Sat Feb 15, 2020 6:08 pm
by geejay
Hi,

The user permissions for the languages are defined in the Joomla Content languages, so natively. I want to query that.

BTW: I am using Falang. The native and the Falang language switcher use getLanguages to determine the available languages, which is an insufficient check the user permissions.

Thanks for your ideas on this.

Re: Howto only getlanguages for which user has permissions?

Posted: Sun Feb 16, 2020 7:55 am
by infograf768
Native languagefilter plugin is checking in its __construct for

Code: Select all

$levels = JFactory::getUser()->getAuthorisedViewLevels();
if the access is not authorised, the language is unset.

Code: Select all

	$this->app = JFactory::getApplication();

		// Setup language data.
		$this->mode_sef     = $this->app->get('sef', 0);
		$this->sefs         = JLanguageHelper::getLanguages('sef');
		$this->lang_codes   = JLanguageHelper::getLanguages('lang_code');
		$this->default_lang = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');

		// If language filter plugin is executed in a site page.
		if ($this->app->isClient('site'))
		{
			$levels = JFactory::getUser()->getAuthorisedViewLevels();

			foreach ($this->sefs as $sef => $language)
			{
				// @todo: In Joomla 2.5.4 and earlier access wasn't set. Non modified Content Languages got 0 as access value
				// we also check if frontend language exists and is enabled
				if (($language->access && !in_array($language->access, $levels))
					|| (!array_key_exists($language->lang_code, JLanguageHelper::getInstalledLanguages(0))))
				{
					unset($this->lang_codes[$language->lang_code], $this->sefs[$language->sef]);
				}
			}
		}

Re: Howto only getlanguages for which user has permissions?

Posted: Mon Feb 17, 2020 9:46 am
by geejay
Great! I was looking for such code.
Thanks for your help.

Is there any reason why such check was not included in the language switcher - to avoid showing languages which are unavailable to the user?

Re: Howto only getlanguages for which user has permissions?

Posted: Tue Feb 18, 2020 11:34 am
by infograf768
If I remember well, once this is defined in the languagefilter, it should reflect in the native module switcher.