[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> NumberField.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\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Uri\Uri;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Form Field class for the Joomla Platform.
  24   * Provides a one line text box with up-down handles to set a number in the field.
  25   *
  26   * @link   https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number)
  27   * @since  3.2
  28   */
  29  class NumberField extends FormField
  30  {
  31      /**
  32       * The form field type.
  33       *
  34       * @var    string
  35       * @since  3.2
  36       */
  37      protected $type = 'Number';
  38  
  39      /**
  40       * The allowable maximum value of the field.
  41       *
  42       * @var    float
  43       * @since  3.2
  44       */
  45      protected $max = null;
  46  
  47      /**
  48       * The allowable minimum value of the field.
  49       *
  50       * @var    float
  51       * @since  3.2
  52       */
  53      protected $min = null;
  54  
  55      /**
  56       * The step by which value of the field increased or decreased.
  57       *
  58       * @var    float
  59       * @since  3.2
  60       */
  61      protected $step = 0;
  62  
  63      /**
  64       * Name of the layout being used to render the field
  65       *
  66       * @var    string
  67       * @since  3.7
  68       */
  69      protected $layout = 'joomla.form.field.number';
  70  
  71      /**
  72       * The parent class of the field
  73       *
  74       * @var  string
  75       * @since 4.0.0
  76       */
  77      protected $parentclass;
  78  
  79      /**
  80       * Method to get certain otherwise inaccessible properties from the form field object.
  81       *
  82       * @param   string  $name  The property name for which to get the value.
  83       *
  84       * @return  mixed  The property value or null.
  85       *
  86       * @since   3.2
  87       */
  88      public function __get($name)
  89      {
  90          switch ($name) {
  91              case 'max':
  92              case 'min':
  93              case 'step':
  94                  return $this->$name;
  95          }
  96  
  97          return parent::__get($name);
  98      }
  99  
 100      /**
 101       * Method to set certain otherwise inaccessible properties of the form field object.
 102       *
 103       * @param   string  $name   The property name for which to set the value.
 104       * @param   mixed   $value  The value of the property.
 105       *
 106       * @return  void
 107       *
 108       * @since   3.2
 109       */
 110      public function __set($name, $value)
 111      {
 112          switch ($name) {
 113              case 'step':
 114              case 'min':
 115              case 'max':
 116                  $this->$name = (float) $value;
 117                  break;
 118  
 119              default:
 120                  parent::__set($name, $value);
 121          }
 122      }
 123  
 124      /**
 125       * Method to attach a Form object to the field.
 126       *
 127       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 128       * @param   mixed              $value    The form field value to validate.
 129       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 130       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 131       *                                       full field name would end up being "bar[foo]".
 132       *
 133       * @return  boolean  True on success.
 134       *
 135       * @see     FormField::setup()
 136       * @since   3.2
 137       */
 138      public function setup(\SimpleXMLElement $element, $value, $group = null)
 139      {
 140          $return = parent::setup($element, $value, $group);
 141  
 142          if ($return) {
 143              // It is better not to force any default limits if none is specified
 144              $this->max  = isset($this->element['max']) ? (float) $this->element['max'] : null;
 145              $this->min  = isset($this->element['min']) ? (float) $this->element['min'] : null;
 146              $this->step = isset($this->element['step']) ? (float) $this->element['step'] : 1;
 147          }
 148  
 149          return $return;
 150      }
 151  
 152      /**
 153       * Method to get the field input markup.
 154       *
 155       * @return  string  The field input markup.
 156       *
 157       * @since   3.2
 158       */
 159      protected function getInput()
 160      {
 161          if ($this->element['useglobal']) {
 162              $component = Factory::getApplication()->input->getCmd('option');
 163  
 164              // Get correct component for menu items
 165              if ($component === 'com_menus') {
 166                  $link      = $this->form->getData()->get('link');
 167                  $uri       = new Uri($link);
 168                  $component = $uri->getVar('option', 'com_menus');
 169              }
 170  
 171              $params = ComponentHelper::getParams($component);
 172              $value  = $params->get($this->fieldname);
 173  
 174              // Try with global configuration
 175              if (\is_null($value)) {
 176                  $value = Factory::getApplication()->get($this->fieldname);
 177              }
 178  
 179              // Try with menu configuration
 180              if (\is_null($value) && Factory::getApplication()->input->getCmd('option') === 'com_menus') {
 181                  $value = ComponentHelper::getParams('com_menus')->get($this->fieldname);
 182              }
 183  
 184              if (!\is_null($value)) {
 185                  $value = (string) $value;
 186  
 187                  $this->hint = Text::sprintf('JGLOBAL_USE_GLOBAL_VALUE', $value);
 188              }
 189          }
 190  
 191          // Trim the trailing line in the layout file
 192          return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
 193      }
 194  
 195      /**
 196       * Method to get the data to be passed to the layout for rendering.
 197       *
 198       * @return  array
 199       *
 200       * @since 3.7
 201       */
 202      protected function getLayoutData()
 203      {
 204          $data = parent::getLayoutData();
 205  
 206          // Initialize some field attributes.
 207          $extraData = array(
 208              'max'   => $this->max,
 209              'min'   => $this->min,
 210              'step'  => $this->step,
 211              'value' => $this->value,
 212          );
 213  
 214          return array_merge($data, $extraData);
 215      }
 216  }


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