[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   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\Form\Field;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  use Joomla\CMS\Form\FormHelper;
  16  use Joomla\CMS\Helper\ModuleHelper;
  17  use Joomla\CMS\Language\Associations;
  18  use Joomla\CMS\Language\Multilanguage;
  19  use Joomla\CMS\Language\Text;
  20  use Joomla\CMS\Plugin\PluginHelper;
  21  use Joomla\CMS\Uri\Uri;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('JPATH_PLATFORM') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Form Field class for the Joomla Platform.
  29   * Supports a generic list of options.
  30   *
  31   * @since  1.7.0
  32   */
  33  class ListField extends FormField
  34  {
  35      /**
  36       * The form field type.
  37       *
  38       * @var    string
  39       * @since  1.7.0
  40       */
  41      protected $type = 'List';
  42  
  43      /**
  44       * Name of the layout being used to render the field
  45       *
  46       * @var    string
  47       * @since  4.0.0
  48       */
  49      protected $layout = 'joomla.form.field.list';
  50  
  51      /**
  52       * Method to get the field input markup for a generic list.
  53       * Use the multiple attribute to enable multiselect.
  54       *
  55       * @return  string  The field input markup.
  56       *
  57       * @since   3.7.0
  58       */
  59      protected function getInput()
  60      {
  61          $data = $this->getLayoutData();
  62  
  63          $data['options'] = (array) $this->getOptions();
  64  
  65          return $this->getRenderer($this->layout)->render($data);
  66      }
  67  
  68      /**
  69       * Method to get the field options.
  70       *
  71       * @return  array  The field option objects.
  72       *
  73       * @since   3.7.0
  74       */
  75      protected function getOptions()
  76      {
  77          $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);
  78          $options   = array();
  79  
  80          foreach ($this->element->xpath('option') as $option) {
  81              // Filter requirements
  82              if ($requires = explode(',', (string) $option['requires'])) {
  83                  // Requires multilanguage
  84                  if (\in_array('multilanguage', $requires) && !Multilanguage::isEnabled()) {
  85                      continue;
  86                  }
  87  
  88                  // Requires associations
  89                  if (\in_array('associations', $requires) && !Associations::isEnabled()) {
  90                      continue;
  91                  }
  92  
  93                  // Requires adminlanguage
  94                  if (\in_array('adminlanguage', $requires) && !ModuleHelper::isAdminMultilang()) {
  95                      continue;
  96                  }
  97  
  98                  // Requires vote plugin
  99                  if (\in_array('vote', $requires) && !PluginHelper::isEnabled('content', 'vote')) {
 100                      continue;
 101                  }
 102  
 103                  // Requires record hits
 104                  if (\in_array('hits', $requires) && !ComponentHelper::getParams('com_content')->get('record_hits', 1)) {
 105                      continue;
 106                  }
 107              }
 108  
 109              $value = (string) $option['value'];
 110              $text  = trim((string) $option) != '' ? trim((string) $option) : $value;
 111  
 112              $disabled = (string) $option['disabled'];
 113              $disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1');
 114              $disabled = $disabled || ($this->readonly && $value != $this->value);
 115  
 116              $checked = (string) $option['checked'];
 117              $checked = ($checked === 'true' || $checked === 'checked' || $checked === '1');
 118  
 119              $selected = (string) $option['selected'];
 120              $selected = ($selected === 'true' || $selected === 'selected' || $selected === '1');
 121  
 122              $tmp = array(
 123                      'value'    => $value,
 124                      'text'     => Text::alt($text, $fieldname),
 125                      'disable'  => $disabled,
 126                      'class'    => (string) $option['class'],
 127                      'selected' => ($checked || $selected),
 128                      'checked'  => ($checked || $selected),
 129              );
 130  
 131              // Set some event handler attributes. But really, should be using unobtrusive js.
 132              $tmp['onclick']  = (string) $option['onclick'];
 133              $tmp['onchange'] = (string) $option['onchange'];
 134  
 135              if ((string) $option['showon']) {
 136                  $encodedConditions = json_encode(
 137                      FormHelper::parseShowOnConditions((string) $option['showon'], $this->formControl, $this->group)
 138                  );
 139  
 140                  $tmp['optionattr'] = " data-showon='" . $encodedConditions . "'";
 141              }
 142  
 143              // Add the option object to the result set.
 144              $options[] = (object) $tmp;
 145          }
 146  
 147          if ($this->element['useglobal']) {
 148              $tmp        = new \stdClass();
 149              $tmp->value = '';
 150              $tmp->text  = Text::_('JGLOBAL_USE_GLOBAL');
 151              $component  = Factory::getApplication()->input->getCmd('option');
 152  
 153              // Get correct component for menu items
 154              if ($component === 'com_menus') {
 155                  $link      = $this->form->getData()->get('link');
 156                  $uri       = new Uri($link);
 157                  $component = $uri->getVar('option', 'com_menus');
 158              }
 159  
 160              $params = ComponentHelper::getParams($component);
 161              $value  = $params->get($this->fieldname);
 162  
 163              // Try with global configuration
 164              if (\is_null($value)) {
 165                  $value = Factory::getApplication()->get($this->fieldname);
 166              }
 167  
 168              // Try with menu configuration
 169              if (\is_null($value) && Factory::getApplication()->input->getCmd('option') === 'com_menus') {
 170                  $value = ComponentHelper::getParams('com_menus')->get($this->fieldname);
 171              }
 172  
 173              if (!\is_null($value)) {
 174                  $value = (string) $value;
 175  
 176                  foreach ($options as $option) {
 177                      if ($option->value === $value) {
 178                          $value = $option->text;
 179  
 180                          break;
 181                      }
 182                  }
 183  
 184                  $tmp->text = Text::sprintf('JGLOBAL_USE_GLOBAL_VALUE', $value);
 185              }
 186  
 187              array_unshift($options, $tmp);
 188          }
 189  
 190          reset($options);
 191  
 192          return $options;
 193      }
 194  
 195      /**
 196       * Method to add an option to the list field.
 197       *
 198       * @param   string  $text        Text/Language variable of the option.
 199       * @param   array   $attributes  Array of attributes ('name' => 'value' format)
 200       *
 201       * @return  ListField  For chaining.
 202       *
 203       * @since   3.7.0
 204       */
 205      public function addOption($text, $attributes = array())
 206      {
 207          if ($text && $this->element instanceof \SimpleXMLElement) {
 208              $child = $this->element->addChild('option', $text);
 209  
 210              foreach ($attributes as $name => $value) {
 211                  $child->addAttribute($name, $value);
 212              }
 213          }
 214  
 215          return $this;
 216      }
 217  
 218      /**
 219       * Method to get certain otherwise inaccessible properties from the form field object.
 220       *
 221       * @param   string  $name  The property name for which to get the value.
 222       *
 223       * @return  mixed  The property value or null.
 224       *
 225       * @since   3.7.0
 226       */
 227      public function __get($name)
 228      {
 229          if ($name === 'options') {
 230              return $this->getOptions();
 231          }
 232  
 233          return parent::__get($name);
 234      }
 235  }


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