[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_fields/src/Plugin/ -> FieldsPlugin.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_fields
   6   *
   7   * @copyright   (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Fields\Administrator\Plugin;
  12  
  13  use Joomla\CMS\Filesystem\Folder;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Plugin\CMSPlugin;
  17  use Joomla\CMS\Plugin\PluginHelper;
  18  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Abstract Fields Plugin
  26   *
  27   * @since  3.7.0
  28   */
  29  abstract class FieldsPlugin extends CMSPlugin
  30  {
  31      /**
  32       * Affects constructor behavior. If true, language files will be loaded automatically.
  33       *
  34       * @var    boolean
  35       * @since  3.7.0
  36       */
  37      protected $autoloadLanguage = true;
  38  
  39      /**
  40       * Application object.
  41       *
  42       * @var    \Joomla\CMS\Application\CMSApplication
  43       * @since  4.0.0
  44       */
  45      protected $app;
  46  
  47      /**
  48       * Returns the custom fields types.
  49       *
  50       * @return  string[][]
  51       *
  52       * @since   3.7.0
  53       */
  54      public function onCustomFieldsGetTypes()
  55      {
  56          // Cache filesystem access / checks
  57          static $types_cache = array();
  58  
  59          if (isset($types_cache[$this->_type . $this->_name])) {
  60              return $types_cache[$this->_type . $this->_name];
  61          }
  62  
  63          $types = array();
  64  
  65          // The root of the plugin
  66          $root = JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name;
  67  
  68          foreach (Folder::files($root . '/tmpl', '.php') as $layout) {
  69              // Strip the extension
  70              $layout = str_replace('.php', '', $layout);
  71  
  72              // The data array
  73              $data = array();
  74  
  75              // The language key
  76              $key = strtoupper($layout);
  77  
  78              if ($key != strtoupper($this->_name)) {
  79                  $key = strtoupper($this->_name) . '_' . $layout;
  80              }
  81  
  82              // Needed attributes
  83              $data['type'] = $layout;
  84  
  85              if ($this->app->getLanguage()->hasKey('PLG_FIELDS_' . $key . '_LABEL')) {
  86                  $data['label'] = Text::sprintf('PLG_FIELDS_' . $key . '_LABEL', strtolower($key));
  87  
  88                  // Fix wrongly set parentheses in RTL languages
  89                  if ($this->app->getLanguage()->isRtl()) {
  90                      $data['label'] = $data['label'] . '&#x200E;';
  91                  }
  92              } else {
  93                  $data['label'] = $key;
  94              }
  95  
  96              $path = $root . '/fields';
  97  
  98              // Add the path when it exists
  99              if (file_exists($path)) {
 100                  $data['path'] = $path;
 101              }
 102  
 103              $path = $root . '/rules';
 104  
 105              // Add the path when it exists
 106              if (file_exists($path)) {
 107                  $data['rules'] = $path;
 108              }
 109  
 110              $types[] = $data;
 111          }
 112  
 113          // Add to cache and return the data
 114          $types_cache[$this->_type . $this->_name] = $types;
 115  
 116          return $types;
 117      }
 118  
 119      /**
 120       * Prepares the field value.
 121       *
 122       * @param   string     $context  The context.
 123       * @param   \stdclass  $item     The item.
 124       * @param   \stdclass  $field    The field.
 125       *
 126       * @return  string
 127       *
 128       * @since   3.7.0
 129       */
 130      public function onCustomFieldsPrepareField($context, $item, $field)
 131      {
 132          // Check if the field should be processed by us
 133          if (!$this->isTypeSupported($field->type)) {
 134              return;
 135          }
 136  
 137          // Merge the params from the plugin and field which has precedence
 138          $fieldParams = clone $this->params;
 139          $fieldParams->merge($field->fieldparams);
 140  
 141          // Get the path for the layout file
 142          $path = PluginHelper::getLayoutPath('fields', $this->_name, $field->type);
 143  
 144          // Render the layout
 145          ob_start();
 146          include $path;
 147          $output = ob_get_clean();
 148  
 149          // Return the output
 150          return $output;
 151      }
 152  
 153      /**
 154       * Transforms the field into a DOM XML element and appends it as a child on the given parent.
 155       *
 156       * @param   \stdClass    $field   The field.
 157       * @param   \DOMElement  $parent  The field node parent.
 158       * @param   Form         $form    The form.
 159       *
 160       * @return  \DOMElement
 161       *
 162       * @since   3.7.0
 163       */
 164      public function onCustomFieldsPrepareDom($field, \DOMElement $parent, Form $form)
 165      {
 166          // Check if the field should be processed by us
 167          if (!$this->isTypeSupported($field->type)) {
 168              return null;
 169          }
 170  
 171          // Detect if the field is configured to be displayed on the form
 172          if (!FieldsHelper::displayFieldOnForm($field)) {
 173              return null;
 174          }
 175  
 176          // Create the node
 177          $node = $parent->appendChild(new \DOMElement('field'));
 178  
 179          // Set the attributes
 180          $node->setAttribute('name', $field->name);
 181          $node->setAttribute('type', $field->type);
 182          $node->setAttribute('label', $field->label);
 183          $node->setAttribute('labelclass', $field->params->get('label_class', ''));
 184          $node->setAttribute('description', $field->description);
 185          $node->setAttribute('class', $field->params->get('class', ''));
 186          $node->setAttribute('hint', $field->params->get('hint', ''));
 187          $node->setAttribute('required', $field->required ? 'true' : 'false');
 188  
 189          if ($layout = $field->params->get('form_layout')) {
 190              $node->setAttribute('layout', $layout);
 191          }
 192  
 193          if ($field->default_value !== '') {
 194              $defaultNode = $node->appendChild(new \DOMElement('default'));
 195              $defaultNode->appendChild(new \DOMCdataSection($field->default_value));
 196          }
 197  
 198          // Combine the two params
 199          $params = clone $this->params;
 200          $params->merge($field->fieldparams);
 201  
 202          // Set the specific field parameters
 203          foreach ($params->toArray() as $key => $param) {
 204              if (is_array($param)) {
 205                  // Multidimensional arrays (eg. list options) can't be transformed properly
 206                  $param = count($param) == count($param, COUNT_RECURSIVE) ? implode(',', $param) : '';
 207              }
 208  
 209              if ($param === '' || (!is_string($param) && !is_numeric($param))) {
 210                  continue;
 211              }
 212  
 213              $node->setAttribute($key, $param);
 214          }
 215  
 216          // Check if it is allowed to edit the field
 217          if (!FieldsHelper::canEditFieldValue($field)) {
 218              $node->setAttribute('disabled', 'true');
 219          }
 220  
 221          // Return the node
 222          return $node;
 223      }
 224  
 225      /**
 226       * The form event. Load additional parameters when available into the field form.
 227       * Only when the type of the form is of interest.
 228       *
 229       * @param   Form       $form  The form
 230       * @param   \stdClass  $data  The data
 231       *
 232       * @return  void
 233       *
 234       * @since   3.7.0
 235       */
 236      public function onContentPrepareForm(Form $form, $data)
 237      {
 238          $path = $this->getFormPath($form, $data);
 239  
 240          if ($path === null) {
 241              return;
 242          }
 243  
 244          // Load the specific plugin parameters
 245          $form->load(file_get_contents($path), true, '/form/*');
 246      }
 247  
 248      /**
 249       * Returns the path of the XML definition file for the field parameters
 250       *
 251       * @param   Form       $form  The form
 252       * @param   \stdClass  $data  The data
 253       *
 254       * @return  string
 255       *
 256       * @since   4.0.0
 257       */
 258      protected function getFormPath(Form $form, $data)
 259      {
 260          // Check if the field form is calling us
 261          if (strpos($form->getName(), 'com_fields.field') !== 0) {
 262              return null;
 263          }
 264  
 265          // Ensure it is an object
 266          $formData = (object) $data;
 267  
 268          // Gather the type
 269          $type = $form->getValue('type');
 270  
 271          if (!empty($formData->type)) {
 272              $type = $formData->type;
 273          }
 274  
 275          // Not us
 276          if (!$this->isTypeSupported($type)) {
 277              return null;
 278          }
 279  
 280          $path = JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/params/' . $type . '.xml';
 281  
 282          // Check if params file exists
 283          if (!file_exists($path)) {
 284              return null;
 285          }
 286  
 287          return $path;
 288      }
 289  
 290      /**
 291       * Returns true if the given type is supported by the plugin.
 292       *
 293       * @param   string  $type  The type
 294       *
 295       * @return  boolean
 296       *
 297       * @since   3.7.0
 298       */
 299      protected function isTypeSupported($type)
 300      {
 301          foreach ($this->onCustomFieldsGetTypes() as $typeSpecification) {
 302              if ($type == $typeSpecification['type']) {
 303                  return true;
 304              }
 305          }
 306  
 307          return false;
 308      }
 309  }


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