[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> TextField.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\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Uri\Uri;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Form Field class for the Joomla Platform.
  25   * Supports a one line text field.
  26   *
  27   * @link   https://html.spec.whatwg.org/multipage/input.html#text-(type=text)-state-and-search-state-(type=search)
  28   * @since  1.7.0
  29   */
  30  class TextField extends FormField
  31  {
  32      /**
  33       * The form field type.
  34       *
  35       * @var    string
  36       * @since  1.7.0
  37       */
  38      protected $type = 'Text';
  39  
  40      /**
  41       * The allowable maxlength of the field.
  42       *
  43       * @var    integer
  44       * @since  3.2
  45       */
  46      protected $maxLength;
  47  
  48      /**
  49       * The mode of input associated with the field.
  50       *
  51       * @var    mixed
  52       * @since  3.2
  53       */
  54      protected $inputmode;
  55  
  56      /**
  57       * The name of the form field direction (ltr or rtl).
  58       *
  59       * @var    string
  60       * @since  3.2
  61       */
  62      protected $dirname;
  63  
  64      /**
  65       * Input addon before
  66       *
  67       * @var    string
  68       * @since  4.0.0
  69       */
  70      protected $addonBefore;
  71  
  72      /**
  73       * Input addon after
  74       *
  75       * @var    string
  76       * @since  4.0.0
  77       */
  78      protected $addonAfter;
  79  
  80      /**
  81       * Name of the layout being used to render the field
  82       *
  83       * @var    string
  84       * @since  3.7
  85       */
  86      protected $layout = 'joomla.form.field.text';
  87  
  88      /**
  89       * Method to get certain otherwise inaccessible properties from the form field object.
  90       *
  91       * @param   string  $name  The property name for which to get the value.
  92       *
  93       * @return  mixed  The property value or null.
  94       *
  95       * @since   3.2
  96       */
  97      public function __get($name)
  98      {
  99          switch ($name) {
 100              case 'maxLength':
 101              case 'dirname':
 102              case 'addonBefore':
 103              case 'addonAfter':
 104              case 'inputmode':
 105                  return $this->$name;
 106          }
 107  
 108          return parent::__get($name);
 109      }
 110  
 111      /**
 112       * Method to set certain otherwise inaccessible properties of the form field object.
 113       *
 114       * @param   string  $name   The property name for which to set the value.
 115       * @param   mixed   $value  The value of the property.
 116       *
 117       * @return  void
 118       *
 119       * @since   3.2
 120       */
 121      public function __set($name, $value)
 122      {
 123          switch ($name) {
 124              case 'maxLength':
 125                  $this->maxLength = (int) $value;
 126                  break;
 127  
 128              case 'dirname':
 129                  $value = (string) $value;
 130                  $this->dirname = ($value == $name || $value === 'true' || $value === '1');
 131                  break;
 132  
 133              case 'inputmode':
 134                  $this->inputmode = (string) $value;
 135                  break;
 136  
 137              case 'addonBefore':
 138                  $this->addonBefore = (string) $value;
 139                  break;
 140  
 141              case 'addonAfter':
 142                  $this->addonAfter = (string) $value;
 143                  break;
 144  
 145              default:
 146                  parent::__set($name, $value);
 147          }
 148      }
 149  
 150      /**
 151       * Method to attach a Form object to the field.
 152       *
 153       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 154       * @param   mixed              $value    The form field value to validate.
 155       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 156       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 157       *                                       full field name would end up being "bar[foo]".
 158       *
 159       * @return  boolean  True on success.
 160       *
 161       * @see     FormField::setup()
 162       * @since   3.2
 163       */
 164      public function setup(\SimpleXMLElement $element, $value, $group = null)
 165      {
 166          $result = parent::setup($element, $value, $group);
 167  
 168          if ($result == true) {
 169              $inputmode = (string) $this->element['inputmode'];
 170              $dirname = (string) $this->element['dirname'];
 171  
 172              $this->inputmode = '';
 173              $inputmode = preg_replace('/\s+/', ' ', trim($inputmode));
 174              $inputmode = explode(' ', $inputmode);
 175  
 176              if (!empty($inputmode)) {
 177                  $defaultInputmode = \in_array('default', $inputmode) ? Text::_('JLIB_FORM_INPUTMODE') . ' ' : '';
 178  
 179                  foreach (array_keys($inputmode, 'default') as $key) {
 180                      unset($inputmode[$key]);
 181                  }
 182  
 183                  $this->inputmode = $defaultInputmode . implode(' ', $inputmode);
 184              }
 185  
 186              // Set the dirname.
 187              $dirname = ($dirname === 'dirname' || $dirname === 'true' || $dirname === '1');
 188              $this->dirname = $dirname ? $this->getName($this->fieldname . '_dir') : false;
 189  
 190              $this->maxLength = (int) $this->element['maxlength'];
 191  
 192              $this->addonBefore = (string) $this->element['addonBefore'];
 193              $this->addonAfter  = (string) $this->element['addonAfter'];
 194          }
 195  
 196          return $result;
 197      }
 198  
 199      /**
 200       * Method to get the field input markup.
 201       *
 202       * @return  string  The field input markup.
 203       *
 204       * @since   1.7.0
 205       */
 206      protected function getInput()
 207      {
 208          if ($this->element['useglobal']) {
 209              $component = Factory::getApplication()->input->getCmd('option');
 210  
 211              // Get correct component for menu items
 212              if ($component === 'com_menus') {
 213                  $link      = $this->form->getData()->get('link');
 214                  $uri       = new Uri($link);
 215                  $component = $uri->getVar('option', 'com_menus');
 216              }
 217  
 218              $params = ComponentHelper::getParams($component);
 219              $value  = $params->get($this->fieldname);
 220  
 221              // Try with global configuration
 222              if (\is_null($value)) {
 223                  $value = Factory::getApplication()->get($this->fieldname);
 224              }
 225  
 226              // Try with menu configuration
 227              if (\is_null($value) && Factory::getApplication()->input->getCmd('option') === 'com_menus') {
 228                  $value = ComponentHelper::getParams('com_menus')->get($this->fieldname);
 229              }
 230  
 231              if (!\is_null($value)) {
 232                  $value = (string) $value;
 233  
 234                  $this->hint = Text::sprintf('JGLOBAL_USE_GLOBAL_VALUE', $value);
 235              }
 236          }
 237  
 238          return $this->getRenderer($this->layout)->render($this->getLayoutData());
 239      }
 240  
 241      /**
 242       * Method to get the field options.
 243       *
 244       * @return  array  The field option objects.
 245       *
 246       * @since   3.4
 247       */
 248      protected function getOptions()
 249      {
 250          $options = array();
 251  
 252          foreach ($this->element->children() as $option) {
 253              // Only add <option /> elements.
 254              if ($option->getName() !== 'option') {
 255                  continue;
 256              }
 257  
 258              // Create a new option object based on the <option /> element.
 259              $options[] = HTMLHelper::_(
 260                  'select.option',
 261                  (string) $option['value'],
 262                  Text::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)),
 263                  'value',
 264                  'text'
 265              );
 266          }
 267  
 268          return $options;
 269      }
 270  
 271      /**
 272       * Method to get the data to be passed to the layout for rendering.
 273       *
 274       * @return  array
 275       *
 276       * @since 3.7
 277       */
 278      protected function getLayoutData()
 279      {
 280          $data = parent::getLayoutData();
 281  
 282          // Initialize some field attributes.
 283          $maxLength    = !empty($this->maxLength) ? ' maxlength="' . $this->maxLength . '"' : '';
 284          $inputmode    = !empty($this->inputmode) ? ' inputmode="' . $this->inputmode . '"' : '';
 285          $dirname      = !empty($this->dirname) ? ' dirname="' . $this->dirname . '"' : '';
 286  
 287          // Get the field options for the datalist.
 288          $options  = (array) $this->getOptions();
 289  
 290          $extraData = array(
 291              'maxLength'   => $maxLength,
 292              'pattern'     => $this->pattern,
 293              'inputmode'   => $inputmode,
 294              'dirname'     => $dirname,
 295              'addonBefore' => $this->addonBefore,
 296              'addonAfter'  => $this->addonAfter,
 297              'options'     => $options,
 298          );
 299  
 300          return array_merge($data, $extraData);
 301      }
 302  }


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