[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2010 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\Form\FormField;
  13  use Joomla\CMS\HTML\HTMLHelper;
  14  use Joomla\CMS\Language\Text;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Form Field class for the Joomla Platform.
  22   * Provides a grouped list select field.
  23   *
  24   * @since  1.7.0
  25   */
  26  class GroupedlistField extends FormField
  27  {
  28      /**
  29       * The form field type.
  30       *
  31       * @var    string
  32       * @since  1.7.0
  33       */
  34      protected $type = 'Groupedlist';
  35  
  36      /**
  37       * Name of the layout being used to render the field
  38       *
  39       * @var    string
  40       * @since  4.0.0
  41       */
  42      protected $layout = 'joomla.form.field.groupedlist';
  43  
  44      /**
  45       * Method to get the field option groups.
  46       *
  47       * @return  array  The field option objects as a nested array in groups.
  48       *
  49       * @since   1.7.0
  50       * @throws  \UnexpectedValueException
  51       */
  52      protected function getGroups()
  53      {
  54          $groups = array();
  55          $label = 0;
  56  
  57          foreach ($this->element->children() as $element) {
  58              switch ($element->getName()) {
  59                  // The element is an <option />
  60                  case 'option':
  61                      // Initialize the group if necessary.
  62                      if (!isset($groups[$label])) {
  63                          $groups[$label] = array();
  64                      }
  65  
  66                      $disabled = (string) $element['disabled'];
  67                      $disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1');
  68  
  69                      // Create a new option object based on the <option /> element.
  70                      $tmp = HTMLHelper::_(
  71                          'select.option',
  72                          ($element['value']) ? (string) $element['value'] : trim((string) $element),
  73                          Text::alt(trim((string) $element), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)),
  74                          'value',
  75                          'text',
  76                          $disabled
  77                      );
  78  
  79                      // Set some option attributes.
  80                      $tmp->class = (string) $element['class'];
  81  
  82                      // Set some JavaScript option attributes.
  83                      $tmp->onclick = (string) $element['onclick'];
  84  
  85                      // Add the option.
  86                      $groups[$label][] = $tmp;
  87                      break;
  88  
  89                  // The element is a <group />
  90                  case 'group':
  91                      // Get the group label.
  92                      if ($groupLabel = (string) $element['label']) {
  93                          $label = Text::_($groupLabel);
  94                      }
  95  
  96                      // Initialize the group if necessary.
  97                      if (!isset($groups[$label])) {
  98                          $groups[$label] = array();
  99                      }
 100  
 101                      // Iterate through the children and build an array of options.
 102                      foreach ($element->children() as $option) {
 103                          // Only add <option /> elements.
 104                          if ($option->getName() !== 'option') {
 105                              continue;
 106                          }
 107  
 108                          $disabled = (string) $option['disabled'];
 109                          $disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1');
 110  
 111                          // Create a new option object based on the <option /> element.
 112                          $tmp = HTMLHelper::_(
 113                              'select.option',
 114                              ($option['value']) ? (string) $option['value'] : Text::_(trim((string) $option)),
 115                              Text::_(trim((string) $option)),
 116                              'value',
 117                              'text',
 118                              $disabled
 119                          );
 120  
 121                          // Set some option attributes.
 122                          $tmp->class = (string) $option['class'];
 123  
 124                          // Set some JavaScript option attributes.
 125                          $tmp->onclick = (string) $option['onclick'];
 126  
 127                          // Add the option.
 128                          $groups[$label][] = $tmp;
 129                      }
 130  
 131                      if ($groupLabel) {
 132                          $label = \count($groups);
 133                      }
 134                      break;
 135  
 136                  // Unknown element type.
 137                  default:
 138                      throw new \UnexpectedValueException(sprintf('Unsupported element %s in GroupedlistField', $element->getName()), 500);
 139              }
 140          }
 141  
 142          reset($groups);
 143  
 144          return $groups;
 145      }
 146  
 147      /**
 148       * Method to get the field input markup fora grouped list.
 149       * Multiselect is enabled by using the multiple attribute.
 150       *
 151       * @return  string  The field input markup.
 152       *
 153       * @since   1.7.0
 154       */
 155      protected function getInput()
 156      {
 157          $data = $this->getLayoutData();
 158  
 159          // Get the field groups.
 160          $data['groups'] = (array) $this->getGroups();
 161  
 162          return $this->getRenderer($this->layout)->render($data);
 163      }
 164  }


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