/libraries/joomla/form/fields/language.php

https://github.com/pjwiseman/joomla-cms · PHP · 95 lines · 55 code · 9 blank · 31 comment · 7 complexity · d4aa6bf7ab7817f2492c89b095344287 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. JFormHelper::loadFieldClass('list');
  11. /**
  12. * Form Field class for the Joomla Platform.
  13. * Supports a list of installed application languages
  14. *
  15. * @see JFormFieldContentLanguage for a select list of content languages.
  16. * @since 11.1
  17. */
  18. class JFormFieldLanguage extends JFormFieldList
  19. {
  20. /**
  21. * The form field type.
  22. *
  23. * @var string
  24. * @since 11.1
  25. */
  26. protected $type = 'Language';
  27. /**
  28. * Method to get the field options.
  29. *
  30. * @return array The field option objects.
  31. *
  32. * @since 11.1
  33. */
  34. protected function getOptions()
  35. {
  36. // Initialize some field attributes.
  37. $client = (string) $this->element['client'];
  38. if ($client != 'site' && $client != 'administrator')
  39. {
  40. $client = 'site';
  41. }
  42. // Make sure the languages are sorted base on locale instead of random sorting
  43. $languages = JLanguageHelper::createLanguageList($this->value, constant('JPATH_' . strtoupper($client)), true, true);
  44. if (count($languages) > 1)
  45. {
  46. usort(
  47. $languages,
  48. function ($a, $b)
  49. {
  50. return strcmp($a["value"], $b["value"]);
  51. }
  52. );
  53. }
  54. // Merge any additional options in the XML definition.
  55. $options = array_merge(
  56. parent::getOptions(),
  57. $languages
  58. );
  59. // Set the default value active language
  60. if ($langParams = JComponentHelper::getParams('com_languages'))
  61. {
  62. switch ((string) $this->value)
  63. {
  64. case 'site':
  65. case 'frontend':
  66. case '0':
  67. $this->value = $langParams->get('site', 'en-GB');
  68. break;
  69. case 'admin':
  70. case 'administrator':
  71. case 'backend':
  72. case '1':
  73. $this->value = $langParams->get('administrator', 'en-GB');
  74. break;
  75. case 'active':
  76. case 'auto':
  77. $lang = JFactory::getLanguage();
  78. $this->value = $lang->getTag();
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. return $options;
  85. }
  86. }