[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> CheckboxesField.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  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Form Field class for the Joomla Platform.
  18   * Displays options as a list of checkboxes.
  19   * Multiselect may be forced to be true.
  20   *
  21   * @see    CheckboxesField
  22   * @since  1.7.0
  23   */
  24  class CheckboxesField extends ListField
  25  {
  26      /**
  27       * The form field type.
  28       *
  29       * @var    string
  30       * @since  1.7.0
  31       */
  32      protected $type = 'Checkboxes';
  33  
  34      /**
  35       * Name of the layout being used to render the field
  36       *
  37       * @var    string
  38       * @since  3.5
  39       */
  40      protected $layout = 'joomla.form.field.checkboxes';
  41  
  42      /**
  43       * Flag to tell the field to always be in multiple values mode.
  44       *
  45       * @var    boolean
  46       * @since  1.7.0
  47       */
  48      protected $forceMultiple = true;
  49  
  50      /**
  51       * The comma separated list of checked checkboxes value.
  52       *
  53       * @var    mixed
  54       * @since  3.2
  55       */
  56      public $checkedOptions;
  57  
  58      /**
  59       * Method to get certain otherwise inaccessible properties from the form field object.
  60       *
  61       * @param   string  $name  The property name for which to get the value.
  62       *
  63       * @return  mixed  The property value or null.
  64       *
  65       * @since   3.2
  66       */
  67      public function __get($name)
  68      {
  69          switch ($name) {
  70              case 'forceMultiple':
  71              case 'checkedOptions':
  72                  return $this->$name;
  73          }
  74  
  75          return parent::__get($name);
  76      }
  77  
  78      /**
  79       * Method to set certain otherwise inaccessible properties of the form field object.
  80       *
  81       * @param   string  $name   The property name for which to set the value.
  82       * @param   mixed   $value  The value of the property.
  83       *
  84       * @return  void
  85       *
  86       * @since   3.2
  87       */
  88      public function __set($name, $value)
  89      {
  90          switch ($name) {
  91              case 'checkedOptions':
  92                  $this->checkedOptions = (string) $value;
  93                  break;
  94  
  95              default:
  96                  parent::__set($name, $value);
  97          }
  98      }
  99  
 100      /**
 101       * Method to get the radio button field input markup.
 102       *
 103       * @return  string  The field input markup.
 104       *
 105       * @since   1.7.0
 106       */
 107      protected function getInput()
 108      {
 109          if (empty($this->layout)) {
 110              throw new \UnexpectedValueException(sprintf('%s has no layout assigned.', $this->name));
 111          }
 112  
 113          return $this->getRenderer($this->layout)->render($this->getLayoutData());
 114      }
 115  
 116      /**
 117       * Method to attach a Form object to the field.
 118       *
 119       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 120       * @param   mixed              $value    The form field value to validate.
 121       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 122       *                                      For example if the field has name="foo" and the group value is set to "bar" then the
 123       *                                      full field name would end up being "bar[foo]".
 124       *
 125       * @return  boolean  True on success.
 126       *
 127       * @see     FormField::setup()
 128       * @since   3.2
 129       */
 130      public function setup(\SimpleXMLElement $element, $value, $group = null)
 131      {
 132          $return = parent::setup($element, $value, $group);
 133  
 134          if ($return) {
 135              $this->checkedOptions = (string) $this->element['checked'];
 136          }
 137  
 138          return $return;
 139      }
 140  
 141      /**
 142       * Method to get the data to be passed to the layout for rendering.
 143       *
 144       * @return  array
 145       *
 146       * @since   3.5
 147       */
 148      protected function getLayoutData()
 149      {
 150          $data = parent::getLayoutData();
 151  
 152          // True if the field has 'value' set. In other words, it has been stored, don't use the default values.
 153          $hasValue = (isset($this->value) && !empty($this->value));
 154  
 155          // If a value has been stored, use it. Otherwise, use the defaults.
 156          $checkedOptions = $hasValue ? $this->value : $this->checkedOptions;
 157  
 158          $extraData = array(
 159              'checkedOptions' => \is_array($checkedOptions) ? $checkedOptions : explode(',', (string) $checkedOptions),
 160              'hasValue'       => $hasValue,
 161              'options'        => $this->getOptions(),
 162          );
 163  
 164          return array_merge($data, $extraData);
 165      }
 166  }


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