[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> TextareaField.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   * Supports a multi line area for entry of plain text
  21   *
  22   * @link   https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element
  23   * @since  1.7.0
  24   */
  25  class TextareaField extends FormField
  26  {
  27      /**
  28       * The form field type.
  29       *
  30       * @var    string
  31       * @since  1.7.0
  32       */
  33      protected $type = 'Textarea';
  34  
  35      /**
  36       * The number of rows in textarea.
  37       *
  38       * @var    mixed
  39       * @since  3.2
  40       */
  41      protected $rows;
  42  
  43      /**
  44       * The number of columns in textarea.
  45       *
  46       * @var    mixed
  47       * @since  3.2
  48       */
  49      protected $columns;
  50  
  51      /**
  52       * The maximum number of characters in textarea.
  53       *
  54       * @var    mixed
  55       * @since  3.4
  56       */
  57      protected $maxlength;
  58  
  59      /**
  60       * Does this field support a character counter?
  61       *
  62       * @var    boolean
  63       * @since  4.0.0
  64       */
  65      protected $charcounter = false;
  66  
  67      /**
  68       * Name of the layout being used to render the field
  69       *
  70       * @var    string
  71       * @since  3.7
  72       */
  73      protected $layout = 'joomla.form.field.textarea';
  74  
  75      /**
  76       * Method to get certain otherwise inaccessible properties from the form field object.
  77       *
  78       * @param   string  $name  The property name for which to get the value.
  79       *
  80       * @return  mixed  The property value or null.
  81       *
  82       * @since   3.2
  83       */
  84      public function __get($name)
  85      {
  86          switch ($name) {
  87              case 'rows':
  88              case 'columns':
  89              case 'maxlength':
  90              case 'charcounter':
  91                  return $this->$name;
  92          }
  93  
  94          return parent::__get($name);
  95      }
  96  
  97      /**
  98       * Method to set certain otherwise inaccessible properties of the form field object.
  99       *
 100       * @param   string  $name   The property name for which to set the value.
 101       * @param   mixed   $value  The value of the property.
 102       *
 103       * @return  void
 104       *
 105       * @since   3.2
 106       */
 107      public function __set($name, $value)
 108      {
 109          switch ($name) {
 110              case 'rows':
 111              case 'columns':
 112              case 'maxlength':
 113                  $this->$name = (int) $value;
 114                  break;
 115  
 116              case 'charcounter':
 117                  $this->charcounter = strtolower($value) === 'true';
 118                  break;
 119  
 120              default:
 121                  parent::__set($name, $value);
 122          }
 123      }
 124  
 125      /**
 126       * Method to attach a Form object to the field.
 127       *
 128       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 129       * @param   mixed              $value    The form field value to validate.
 130       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 131       *                                      For example if the field has name="foo" and the group value is set to "bar" then the
 132       *                                      full field name would end up being "bar[foo]".
 133       *
 134       * @return  boolean  True on success.
 135       *
 136       * @see     FormField::setup()
 137       * @since   3.2
 138       */
 139      public function setup(\SimpleXMLElement $element, $value, $group = null)
 140      {
 141          $return = parent::setup($element, $value, $group);
 142  
 143          if ($return) {
 144              $this->rows        = isset($this->element['rows']) ? (int) $this->element['rows'] : false;
 145              $this->columns     = isset($this->element['cols']) ? (int) $this->element['cols'] : false;
 146              $this->maxlength   = isset($this->element['maxlength']) ? (int) $this->element['maxlength'] : false;
 147              $this->charcounter = isset($this->element['charcounter']) ? strtolower($this->element['charcounter']) === 'true' : false;
 148          }
 149  
 150          return $return;
 151      }
 152  
 153      /**
 154       * Method to get the textarea field input markup.
 155       * Use the rows and columns attributes to specify the dimensions of the area.
 156       *
 157       * @return  string  The field input markup.
 158       *
 159       * @since   1.7.0
 160       */
 161      protected function getInput()
 162      {
 163          // Trim the trailing line in the layout file
 164          return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
 165      }
 166  
 167      /**
 168       * Method to get the data to be passed to the layout for rendering.
 169       *
 170       * @return  array
 171       *
 172       * @since 3.7
 173       */
 174      protected function getLayoutData()
 175      {
 176          $data = parent::getLayoutData();
 177  
 178          // Initialize some field attributes.
 179          $columns      = $this->columns ? ' cols="' . $this->columns . '"' : '';
 180          $rows         = $this->rows ? ' rows="' . $this->rows . '"' : '';
 181          $maxlength    = $this->maxlength ? ' maxlength="' . $this->maxlength . '"' : '';
 182  
 183          $extraData = array(
 184              'maxlength'    => $maxlength,
 185              'rows'         => $rows,
 186              'columns'      => $columns,
 187              'charcounter'  => $this->charcounter
 188          );
 189  
 190          return array_merge($data, $extraData);
 191      }
 192  }


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