[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> PredefinedlistField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 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\Form\Field;
  11  
  12  use Joomla\CMS\Language\Text;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Form Field to load a list of predefined values
  20   *
  21   * @since  3.2
  22   */
  23  abstract class PredefinedlistField extends ListField
  24  {
  25      /**
  26       * The form field type.
  27       *
  28       * @var    string
  29       * @since  3.2
  30       */
  31      protected $type = 'Predefinedlist';
  32  
  33      /**
  34       * Cached array of the category items.
  35       *
  36       * @var    array
  37       * @since  3.2
  38       */
  39      protected static $options = array();
  40  
  41      /**
  42       * Available predefined options
  43       *
  44       * @var  array
  45       * @since  3.2
  46       */
  47      protected $predefinedOptions = array();
  48  
  49      /**
  50       * Translate options labels ?
  51       *
  52       * @var  boolean
  53       * @since  3.2
  54       */
  55      protected $translate = true;
  56  
  57      /**
  58       * Allows to use only specific values of the predefined list
  59       *
  60       * @var  array
  61       * @since  4.0.0
  62       */
  63      protected $optionsFilter = [];
  64  
  65      /**
  66       * Method to attach a Form object to the field.
  67       *
  68       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  69       * @param   mixed              $value    The form field value to validate.
  70       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  71       *                                      For example if the field has name="foo" and the group value is set to "bar" then the
  72       *                                      full field name would end up being "bar[foo]".
  73       *
  74       * @return  boolean  True on success.
  75       *
  76       * @see     \Joomla\CMS\Form\FormField::setup()
  77       * @since   4.0.0
  78       */
  79      public function setup(\SimpleXMLElement $element, $value, $group = null)
  80      {
  81          $return = parent::setup($element, $value, $group);
  82  
  83          if ($return) {
  84              // Note: $this->element['optionsFilter'] is not cast to string here to allow empty string value.
  85              $this->optionsFilter = $this->element['optionsFilter'] ? explode(',', (string) $this->element['optionsFilter']) : [];
  86          }
  87  
  88          return $return;
  89      }
  90  
  91      /**
  92       * Method to get the options to populate list
  93       *
  94       * @return  array  The field option objects.
  95       *
  96       * @since   3.2
  97       */
  98      protected function getOptions()
  99      {
 100          // Hash for caching
 101          $hash = md5($this->element);
 102          $type = strtolower($this->type);
 103  
 104          if (!isset(static::$options[$type][$hash]) && !empty($this->predefinedOptions)) {
 105              static::$options[$type][$hash] = parent::getOptions();
 106  
 107              $options = array();
 108  
 109              foreach ($this->predefinedOptions as $value => $text) {
 110                  $val = (string) $value;
 111  
 112                  if (empty($this->optionsFilter) || in_array($val, $this->optionsFilter, true)) {
 113                      $text = $this->translate ? Text::_($text) : $text;
 114  
 115                      $options[] = (object) array(
 116                          'value' => $value,
 117                          'text'  => $text,
 118                      );
 119                  }
 120              }
 121  
 122              static::$options[$type][$hash] = array_merge(static::$options[$type][$hash], $options);
 123          }
 124  
 125          return static::$options[$type][$hash];
 126      }
 127  }


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