[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> CheckboxField.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\Form\FormField;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Form Field class for the Joomla Platform.
  20   * Single checkbox field.
  21   * This is a boolean field with null for false and the specified option for true
  22   *
  23   * @link   https://html.spec.whatwg.org/multipage/input.html#checkbox-state-(type=checkbox)
  24   * @see    CheckboxField
  25   * @since  1.7.0
  26   */
  27  class CheckboxField extends FormField
  28  {
  29      /**
  30       * The form field type.
  31       *
  32       * @var    string
  33       * @since  1.7.0
  34       */
  35      protected $type = 'Checkbox';
  36  
  37      /**
  38       * Name of the layout being used to render the field
  39       *
  40       * @var    string
  41       * @since  4.0.0
  42       */
  43      protected $layout = 'joomla.form.field.checkbox';
  44  
  45      /**
  46       * The checked state of checkbox field.
  47       *
  48       * @var    boolean
  49       * @since  3.2
  50       */
  51      protected $checked = false;
  52  
  53      /**
  54       * Method to get certain otherwise inaccessible properties from the form field object.
  55       *
  56       * @param   string  $name  The property name for which to get the value.
  57       *
  58       * @return  mixed  The property value or null.
  59       *
  60       * @since   3.2
  61       */
  62      public function __get($name)
  63      {
  64          if ($name === 'checked') {
  65              return $this->checked;
  66          }
  67  
  68          return parent::__get($name);
  69      }
  70  
  71      /**
  72       * Method to set certain otherwise inaccessible properties of the form field object.
  73       *
  74       * @param   string  $name   The property name for which to set the value.
  75       * @param   mixed   $value  The value of the property.
  76       *
  77       * @return  void
  78       *
  79       * @since   3.2
  80       */
  81      public function __set($name, $value)
  82      {
  83          if ($name === 'checked') {
  84              $value = (string) $value;
  85              $this->checked = ($value === 'true' || $value == $name || $value === '1');
  86  
  87              return;
  88          }
  89  
  90          parent::__set($name, $value);
  91      }
  92  
  93      /**
  94       * Method to attach a Form object to the field.
  95       *
  96       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  97       * @param   mixed              $value    The form field value to validate.
  98       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  99       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 100       *                                       full field name would end up being "bar[foo]".
 101       *
 102       * @return  boolean  True on success.
 103       *
 104       * @see     FormField::setup()
 105       * @since   3.2
 106       */
 107      public function setup(\SimpleXMLElement $element, $value, $group = null)
 108      {
 109          // Handle the default attribute
 110          $default = (string) $element['default'];
 111  
 112          if ($default) {
 113              $test = $this->form->getValue((string) $element['name'], $group);
 114  
 115              $value = ($test == $default) ? $default : null;
 116          }
 117  
 118          $return = parent::setup($element, $value, $group);
 119  
 120          if ($return) {
 121              $checked = (string) $this->element['checked'];
 122              $this->checked = ($checked === 'true' || $checked === 'checked' || $checked === '1');
 123  
 124              empty($this->value) || $this->checked ? null : $this->checked = true;
 125          }
 126  
 127          return $return;
 128      }
 129  
 130      /**
 131       * Method to get the data to be passed to the layout for rendering.
 132       *
 133       * @return  array
 134       *
 135       * @since   4.0.0
 136       */
 137      protected function getLayoutData()
 138      {
 139          $data            = parent::getLayoutData();
 140          $data['value']   = $this->default ?: '1';
 141          $data['checked'] = $this->checked || $this->value;
 142  
 143          return $data;
 144      }
 145  }


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