[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> EditorField.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\Editor\Editor;
  13  use Joomla\CMS\Factory;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * A textarea field for content creation
  21   *
  22   * @see    JEditor
  23   * @since  1.6
  24   */
  25  class EditorField extends TextareaField
  26  {
  27      /**
  28       * The form field type.
  29       *
  30       * @var    string
  31       * @since  1.6
  32       */
  33      public $type = 'Editor';
  34  
  35      /**
  36       * The Editor object.
  37       *
  38       * @var    Editor
  39       * @since  1.6
  40       */
  41      protected $editor;
  42  
  43      /**
  44       * The height of the editor.
  45       *
  46       * @var    string
  47       * @since  3.2
  48       */
  49      protected $height;
  50  
  51      /**
  52       * The width of the editor.
  53       *
  54       * @var    string
  55       * @since  3.2
  56       */
  57      protected $width;
  58  
  59      /**
  60       * The assetField of the editor.
  61       *
  62       * @var    string
  63       * @since  3.2
  64       */
  65      protected $assetField;
  66  
  67      /**
  68       * The authorField of the editor.
  69       *
  70       * @var    string
  71       * @since  3.2
  72       */
  73      protected $authorField;
  74  
  75      /**
  76       * The asset of the editor.
  77       *
  78       * @var    string
  79       * @since  3.2
  80       */
  81      protected $asset;
  82  
  83      /**
  84       * The buttons of the editor.
  85       *
  86       * @var    mixed
  87       * @since  3.2
  88       */
  89      protected $buttons;
  90  
  91      /**
  92       * The hide of the editor.
  93       *
  94       * @var    array
  95       * @since  3.2
  96       */
  97      protected $hide;
  98  
  99      /**
 100       * The editorType of the editor.
 101       *
 102       * @var    array
 103       * @since  3.2
 104       */
 105      protected $editorType;
 106  
 107      /**
 108       * The parent class of the field
 109       *
 110       * @var  string
 111       * @since 4.0.0
 112       */
 113      protected $parentclass;
 114  
 115      /**
 116       * Method to get certain otherwise inaccessible properties from the form field object.
 117       *
 118       * @param   string  $name  The property name for which to get the value.
 119       *
 120       * @return  mixed  The property value or null.
 121       *
 122       * @since   3.2
 123       */
 124      public function __get($name)
 125      {
 126          switch ($name) {
 127              case 'height':
 128              case 'width':
 129              case 'assetField':
 130              case 'authorField':
 131              case 'asset':
 132              case 'buttons':
 133              case 'hide':
 134              case 'editorType':
 135                  return $this->$name;
 136          }
 137  
 138          return parent::__get($name);
 139      }
 140  
 141      /**
 142       * Method to set certain otherwise inaccessible properties of the form field object.
 143       *
 144       * @param   string  $name   The property name for which to set the value.
 145       * @param   mixed   $value  The value of the property.
 146       *
 147       * @return  void
 148       *
 149       * @since   3.2
 150       */
 151      public function __set($name, $value)
 152      {
 153          switch ($name) {
 154              case 'height':
 155              case 'width':
 156              case 'assetField':
 157              case 'authorField':
 158              case 'asset':
 159                  $this->$name = (string) $value;
 160                  break;
 161  
 162              case 'buttons':
 163                  $value = (string) $value;
 164  
 165                  if ($value === 'true' || $value === 'yes' || $value === '1') {
 166                      $this->buttons = true;
 167                  } elseif ($value === 'false' || $value === 'no' || $value === '0') {
 168                      $this->buttons = false;
 169                  } else {
 170                      $this->buttons = explode(',', $value);
 171                  }
 172                  break;
 173  
 174              case 'hide':
 175                  $value = (string) $value;
 176                  $this->hide = $value ? explode(',', $value) : array();
 177                  break;
 178  
 179              case 'editorType':
 180                  // Can be in the form of: editor="desired|alternative".
 181                  $this->editorType  = explode('|', trim((string) $value));
 182                  break;
 183  
 184              default:
 185                  parent::__set($name, $value);
 186          }
 187      }
 188  
 189      /**
 190       * Method to attach a Form object to the field.
 191       *
 192       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 193       * @param   mixed              $value    The form field value to validate.
 194       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 195       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 196       *                                       full field name would end up being "bar[foo]".
 197       *
 198       * @return  boolean  True on success.
 199       *
 200       * @see     FormField::setup()
 201       * @since   3.2
 202       */
 203      public function setup(\SimpleXMLElement $element, $value, $group = null)
 204      {
 205          $result = parent::setup($element, $value, $group);
 206  
 207          if ($result === true) {
 208              $this->height      = $this->element['height'] ? (string) $this->element['height'] : '500';
 209              $this->width       = $this->element['width'] ? (string) $this->element['width'] : '100%';
 210              $this->assetField  = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
 211              $this->authorField = $this->element['created_by_field'] ? (string) $this->element['created_by_field'] : 'created_by';
 212              $this->asset       = $this->form->getValue($this->assetField) ?: (string) $this->element['asset_id'];
 213  
 214              $buttons    = (string) $this->element['buttons'];
 215              $hide       = (string) $this->element['hide'];
 216              $editorType = (string) $this->element['editor'];
 217  
 218              if ($buttons === 'true' || $buttons === 'yes' || $buttons === '1') {
 219                  $this->buttons = true;
 220              } elseif ($buttons === 'false' || $buttons === 'no' || $buttons === '0') {
 221                  $this->buttons = false;
 222              } else {
 223                  $this->buttons = !empty($hide) ? explode(',', $buttons) : array();
 224              }
 225  
 226              $this->hide        = !empty($hide) ? explode(',', (string) $this->element['hide']) : array();
 227              $this->editorType  = !empty($editorType) ? explode('|', trim($editorType)) : array();
 228          }
 229  
 230          return $result;
 231      }
 232  
 233      /**
 234       * Method to get the field input markup for the editor area
 235       *
 236       * @return  string  The field input markup.
 237       *
 238       * @since   1.6
 239       */
 240      protected function getInput()
 241      {
 242          // Get an editor object.
 243          $editor = $this->getEditor();
 244          $params = array(
 245              'autofocus' => $this->autofocus,
 246              'readonly'  => $this->readonly || $this->disabled,
 247              'syntax'    => (string) $this->element['syntax'],
 248          );
 249  
 250          return $editor->display(
 251              $this->name,
 252              htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8'),
 253              $this->width,
 254              $this->height,
 255              $this->columns,
 256              $this->rows,
 257              $this->buttons ? (\is_array($this->buttons) ? array_merge($this->buttons, $this->hide) : $this->hide) : false,
 258              $this->id,
 259              $this->asset,
 260              $this->form->getValue($this->authorField),
 261              $params
 262          );
 263      }
 264  
 265      /**
 266       * Method to get an Editor object based on the form field.
 267       *
 268       * @return  Editor  The Editor object.
 269       *
 270       * @since   1.6
 271       */
 272      protected function getEditor()
 273      {
 274          // Only create the editor if it is not already created.
 275          if (empty($this->editor)) {
 276              $editor = null;
 277  
 278              if ($this->editorType) {
 279                  // Get the list of editor types.
 280                  $types = $this->editorType;
 281  
 282                  // Get the database object.
 283                  $db = $this->getDatabase();
 284  
 285                  // Build the query.
 286                  $query = $db->getQuery(true)
 287                      ->select($db->quoteName('element'))
 288                      ->from($db->quoteName('#__extensions'))
 289                      ->where(
 290                          [
 291                              $db->quoteName('element') . ' = :editor',
 292                              $db->quoteName('folder') . ' = ' . $db->quote('editors'),
 293                              $db->quoteName('enabled') . ' = 1',
 294                          ]
 295                      );
 296  
 297                  // Declare variable before binding.
 298                  $element = '';
 299                  $query->bind(':editor', $element);
 300                  $query->setLimit(1);
 301  
 302                  // Iterate over the types looking for an existing editor.
 303                  foreach ($types as $element) {
 304                      // Check if the editor exists.
 305                      $db->setQuery($query);
 306                      $editor = $db->loadResult();
 307  
 308                      // If an editor was found stop looking.
 309                      if ($editor) {
 310                          break;
 311                      }
 312                  }
 313              }
 314  
 315              // Create the JEditor instance based on the given editor.
 316              if ($editor === null) {
 317                  $editor = Factory::getApplication()->get('editor');
 318              }
 319  
 320              $this->editor = Editor::getInstance($editor);
 321          }
 322  
 323          return $this->editor;
 324      }
 325  }


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