[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/installation/src/Form/Field/Installation/ -> LanguageField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package    Joomla.Installation
   5   *
   6   * @copyright  (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Installation\Form\Field\Installation;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Form\Field\ListField;
  14  use Joomla\CMS\Installation\Model\SetupModel;
  15  use Joomla\CMS\Language\LanguageHelper;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Installation Language field.
  23   *
  24   * @since  1.6
  25   */
  26  class LanguageField extends ListField
  27  {
  28      /**
  29       * The form field type.
  30       *
  31       * @var    string
  32       * @since  1.6
  33       */
  34      protected $type = 'Language';
  35  
  36      /**
  37       * Method to attach a Form object to the field.
  38       *
  39       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  40       * @param   mixed              $value    The form field value to validate.
  41       * @param   string             $group    The field name group control value. This acts as as an array container for the field.
  42       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  43       *                                       full field name would end up being "bar[foo]".
  44       *
  45       * @return  boolean  True on success.
  46       *
  47       * @since   4.2.0
  48       */
  49      public function setup(\SimpleXMLElement $element, $value, $group = null)
  50      {
  51          $value = $this->getNativeLanguage();
  52  
  53          return parent::setup($element, $value, $group);
  54      }
  55  
  56      /**
  57       * Method to get the field options.
  58       *
  59       * @return  array  The field option objects.
  60       *
  61       * @since   1.6
  62       */
  63      protected function getOptions()
  64      {
  65          $native = $this->getNativeLanguage();
  66  
  67          // Get the list of available languages.
  68          $options = LanguageHelper::createLanguageList($native);
  69  
  70          // Fix wrongly set parentheses in RTL languages
  71          if (Factory::getLanguage()->isRtl()) {
  72              foreach ($options as &$option) {
  73                  $option['text'] .= '&#x200E;';
  74              }
  75          }
  76  
  77          if (!$options || $options instanceof \Exception) {
  78              $options = array();
  79          } else {
  80              // Sort languages by name
  81              usort($options, array($this, '_sortLanguages'));
  82          }
  83  
  84          // Merge any additional options in the XML definition.
  85          $options = array_merge(parent::getOptions(), $options);
  86  
  87          return $options;
  88      }
  89  
  90      /**
  91       * Method to sort languages by name.
  92       *
  93       * @param   array  $a  The first value to determine sort
  94       * @param   array  $b  The second value to determine sort
  95       *
  96       * @return  integer
  97       *
  98       * @since   3.1
  99       */
 100      protected function _sortLanguages($a, $b)
 101      {
 102          return strcmp($a['text'], $b['text']);
 103      }
 104  
 105      /**
 106       * Determinate the native language to select
 107       *
 108       * @return  string  The native language to use
 109       *
 110       * @since   4.2.0
 111       */
 112      protected function getNativeLanguage()
 113      {
 114          static $native;
 115  
 116          if (isset($native)) {
 117              return $native;
 118          }
 119  
 120          $app = Factory::getApplication();
 121  
 122          // Detect the native language.
 123          $native = LanguageHelper::detectLanguage();
 124  
 125          if (empty($native)) {
 126              $native = 'en-GB';
 127          }
 128  
 129          // Get a forced language if it exists.
 130          $forced = $app->getLocalise();
 131  
 132          if (!empty($forced['language'])) {
 133              $native = $forced['language'];
 134          }
 135  
 136          // If a language is already set in the session, use this instead
 137          $model   = new SetupModel();
 138          $options = $model->getOptions();
 139  
 140          if (isset($options['language'])) {
 141              $native = $options['language'];
 142          }
 143  
 144          return $native;
 145      }
 146  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer