[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/Model/ -> FormBehaviorTrait.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\MVC\Model;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Form\FormFactoryInterface;
  15  use Joomla\CMS\Form\FormField;
  16  use Joomla\CMS\Plugin\PluginHelper;
  17  use Joomla\Utilities\ArrayHelper;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Trait which supports form behavior.
  25   *
  26   * @since  4.0.0
  27   */
  28  trait FormBehaviorTrait
  29  {
  30      /**
  31       * Array of form objects.
  32       *
  33       * @var    Form[]
  34       * @since  4.0.0
  35       */
  36      protected $_forms = array();
  37  
  38      /**
  39       * Method to get a form object.
  40       *
  41       * @param   string   $name     The name of the form.
  42       * @param   string   $source   The form source. Can be XML string if file flag is set to false.
  43       * @param   array    $options  Optional array of options for the form creation.
  44       * @param   boolean  $clear    Optional argument to force load a new form.
  45       * @param   string   $xpath    An optional xpath to search for the fields.
  46       *
  47       * @return  Form
  48       *
  49       * @see     Form
  50       * @since   4.0.0
  51       * @throws  \Exception
  52       */
  53      protected function loadForm($name, $source = null, $options = array(), $clear = false, $xpath = null)
  54      {
  55          // Handle the optional arguments.
  56          $options['control'] = ArrayHelper::getValue((array) $options, 'control', false);
  57  
  58          // Create a signature hash. But make sure, that loading the data does not create a new instance
  59          $sigoptions = $options;
  60  
  61          if (isset($sigoptions['load_data'])) {
  62              unset($sigoptions['load_data']);
  63          }
  64  
  65          $hash = md5($source . serialize($sigoptions));
  66  
  67          // Check if we can use a previously loaded form.
  68          if (!$clear && isset($this->_forms[$hash])) {
  69              return $this->_forms[$hash];
  70          }
  71  
  72          // Get the form.
  73          Form::addFormPath(JPATH_COMPONENT . '/forms');
  74          Form::addFormPath(JPATH_COMPONENT . '/models/forms');
  75          Form::addFieldPath(JPATH_COMPONENT . '/models/fields');
  76          Form::addFormPath(JPATH_COMPONENT . '/model/form');
  77          Form::addFieldPath(JPATH_COMPONENT . '/model/field');
  78  
  79          try {
  80              $formFactory = $this->getFormFactory();
  81          } catch (\UnexpectedValueException $e) {
  82              $formFactory = Factory::getContainer()->get(FormFactoryInterface::class);
  83          }
  84  
  85          $form = $formFactory->createForm($name, $options);
  86  
  87          // Load the data.
  88          if (substr($source, 0, 1) === '<') {
  89              if ($form->load($source, false, $xpath) == false) {
  90                  throw new \RuntimeException('Form::loadForm could not load form');
  91              }
  92          } else {
  93              if ($form->loadFile($source, false, $xpath) == false) {
  94                  throw new \RuntimeException('Form::loadForm could not load file');
  95              }
  96          }
  97  
  98          if (isset($options['load_data']) && $options['load_data']) {
  99              // Get the data for the form.
 100              $data = $this->loadFormData();
 101          } else {
 102              $data = array();
 103          }
 104  
 105          // Allow for additional modification of the form, and events to be triggered.
 106          // We pass the data because plugins may require it.
 107          $this->preprocessForm($form, $data);
 108  
 109          // Load the data into the form after the plugins have operated.
 110          $form->bind($data);
 111  
 112          // Store the form for later.
 113          $this->_forms[$hash] = $form;
 114  
 115          return $form;
 116      }
 117  
 118      /**
 119       * Method to get the data that should be injected in the form.
 120       *
 121       * @return  array  The default data is an empty array.
 122       *
 123       * @since   4.0.0
 124       */
 125      protected function loadFormData()
 126      {
 127          return [];
 128      }
 129  
 130      /**
 131       * Method to allow derived classes to preprocess the data.
 132       *
 133       * @param   string  $context  The context identifier.
 134       * @param   mixed   &$data    The data to be processed. It gets altered directly.
 135       * @param   string  $group    The name of the plugin group to import (defaults to "content").
 136       *
 137       * @return  void
 138       *
 139       * @since   4.0.0
 140       */
 141      protected function preprocessData($context, &$data, $group = 'content')
 142      {
 143          // Get the dispatcher and load the users plugins.
 144          PluginHelper::importPlugin($group);
 145  
 146          // Trigger the data preparation event.
 147          Factory::getApplication()->triggerEvent('onContentPrepareData', array($context, &$data));
 148      }
 149  
 150      /**
 151       * Method to allow derived classes to preprocess the form.
 152       *
 153       * @param   Form    $form   A Form object.
 154       * @param   mixed   $data   The data expected for the form.
 155       * @param   string  $group  The name of the plugin group to import (defaults to "content").
 156       *
 157       * @return  void
 158       *
 159       * @see     FormField
 160       * @since   4.0.0
 161       * @throws  \Exception if there is an error in the form event.
 162       */
 163      protected function preprocessForm(Form $form, $data, $group = 'content')
 164      {
 165          // Import the appropriate plugin group.
 166          PluginHelper::importPlugin($group);
 167  
 168          // Trigger the form preparation event.
 169          Factory::getApplication()->triggerEvent('onContentPrepareForm', array($form, $data));
 170      }
 171  
 172      /**
 173       * Get the FormFactoryInterface.
 174       *
 175       * @return  FormFactoryInterface
 176       *
 177       * @since   4.0.0
 178       * @throws  \UnexpectedValueException May be thrown if the FormFactory has not been set.
 179       */
 180      abstract public function getFormFactory(): FormFactoryInterface;
 181  }


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