Skip to content

Commit

Permalink
[4.2] Fix wrong installation language select default value (#37252)
Browse files Browse the repository at this point in the history
* Fix language select on installation

* Add method version

* Updating Joomla Browser through composer dependencies (#129)

* Revert API version
  • Loading branch information
bembelimen authored May 2, 2022
1 parent 79bef58 commit c00ddcc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 67 additions & 29 deletions installation/src/Form/Field/Installation/LanguageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ class LanguageField extends ListField
*/
protected $type = 'Language';

/**
* Method to attach a Form object to the field.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
*
* @return boolean True on success.
*
* @since __DEPLOY_VERSION__
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
$value = $this->getNativeLanguage();

return parent::setup($element, $value, $group);
}

/**
* Method to get the field options.
*
Expand All @@ -39,32 +59,7 @@ class LanguageField extends ListField
*/
protected function getOptions()
{
$app = Factory::getApplication();

// Detect the native language.
$native = LanguageHelper::detectLanguage();

if (empty($native))
{
$native = 'en-GB';
}

// Get a forced language if it exists.
$forced = $app->getLocalise();

if (!empty($forced['language']))
{
$native = $forced['language'];
}

// If a language is already set in the session, use this instead
$model = new SetupModel;
$options = $model->getOptions();

if (isset($options['language']))
{
$native = $options['language'];
}
$native = $this->getNativeLanguage();

// Get the list of available languages.
$options = LanguageHelper::createLanguageList($native);
Expand All @@ -88,9 +83,6 @@ protected function getOptions()
usort($options, array($this, '_sortLanguages'));
}

// Set the default value from the native language.
$this->value = $native;

// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);

Expand All @@ -111,4 +103,50 @@ protected function _sortLanguages($a, $b)
{
return strcmp($a['text'], $b['text']);
}

/**
* Determinate the native language to select
*
* @return string The native language to use
*
* @since __DEPLOY_VERSION__
*/
protected function getNativeLanguage()
{
static $native;

if (isset($native))
{
return $native;
}

$app = Factory::getApplication();

// Detect the native language.
$native = LanguageHelper::detectLanguage();

if (empty($native))
{
$native = 'en-GB';
}

// Get a forced language if it exists.
$forced = $app->getLocalise();

if (!empty($forced['language']))
{
$native = $forced['language'];
}

// If a language is already set in the session, use this instead
$model = new SetupModel;
$options = $model->getOptions();

if (isset($options['language']))
{
$native = $options['language'];
}

return $native;
}
}

0 comments on commit c00ddcc

Please sign in to comment.