Skip to content

Commit

Permalink
Added a couple of PHP form plugin hooks, for onJSReady and onJSOpts.
Browse files Browse the repository at this point in the history
Lastter is semi useful, can tweak form's JS options, which are in
$formModel->jsOpts.  Like, for setting group repeat max dynamically,
with $formModel->jsOpts->maxRepeat[x] = y;
  • Loading branch information
cheesegrits committed Oct 11, 2014
1 parent 7e04105 commit bc445cd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
10 changes: 10 additions & 0 deletions components/com_fabrik/models/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ class FabrikFEModelForm extends FabModelForm
*/
public $formPluginShim = array();

/**
* JS options on load, only used when calling onJSOpts plugin
* so plugin code can access and modify them
*
* @since 3.2
*
* @var array
*/
public $jsOpts = null;

/**
* Constructor
*
Expand Down
8 changes: 6 additions & 2 deletions components/com_fabrik/views/form/view.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ protected function _addJavascript($listId)
{
$app = JFactory::getApplication();
$package = $app->getUserState('com_fabrik.package', 'fabrik');
$pluginManager = FabrikWorker::getPluginManager();
$input = $app->input;
$document = JFactory::getDocument();
$model = $this->getModel();
Expand Down Expand Up @@ -454,7 +455,11 @@ protected function _addJavascript($listId)
FabrikHelperHTML::tips('.hasTip', array(), "$('$bkey')");
$model->getFormCss();
$opts = $this->jsOpts();
$opts = json_encode($opts);

$model->jsOpts = $opts;
$res = $pluginManager->runPlugins('onJSOpts', $model);

$opts = json_encode($model->jsOpts);

if (!FabrikHelperHTML::inAjaxLoadedPage())
{
Expand Down Expand Up @@ -557,7 +562,6 @@ protected function _addJavascript($listId)
$script[] = "new FloatingTips('#" . $bkey . " .fabrikTip', " . json_encode($tipOpts) . ");";
}

$pluginManager = FabrikWorker::getPluginManager();
$res = $pluginManager->runPlugins('onJSReady', $model);

if (in_array(false, $res))
Expand Down
2 changes: 2 additions & 0 deletions plugins/fabrik_form/php/forms/fields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<option value="">PLG_FORM_PHP_PROCESS_NEVER</option>
<option value="onBeforeLoad">PLG_FORM_PHP_PROCESS_ONBEFORELOAD</option>
<option value="onLoad">PLG_FORM_PHP_PROCESS_ONLOAD</option>
<option value="onJSReady">PLG_FORM_PHP_PROCESS_ONJSREADY</option>
<option value="onJSOpts">PLG_FORM_PHP_PROCESS_ONJSOPTS</option>
<option value="onCanEditGroup">PLG_FORM_PHP_PROCESS_ONCANEDITGROUP</option>
<option value="onBeforeProcess">PLG_FORM_PHP_PROCESS_ONBEFOREPROCESS</option>
<option value="onBeforeStore">PLG_FORM_PHP_PROCESS_ONBEFORESTORE</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ PLG_FORM_PHP_REQUIRE_ONCE_LABEL="Require Once"
PLG_FORM_PHP_PROCESS_NEVER="Never"
PLG_FORM_PHP_PROCESS_ONBEFORELOAD="Before the form data is loaded (onBeforeLoad)"
PLG_FORM_PHP_PROCESS_ONLOAD="Before the form is loaded (onLoad)"
PLG_FORM_PHP_PROCESS_ONJSREADY="When the form JS is assembled and ready during page load (onJSReady)"
PLG_FORM_PHP_PROCESS_ONJSOPTS="Prior to JSON encoding of main JS options, in $opts (onJSOpts)"
PLG_FORM_PHP_PROCESS_ONCANEDITGROUP="While groups are rendered on the form (onCanEditGroup)"
PLG_FORM_PHP_PROCESS_ONBEFOREPROCESS="Start of form submission (onBeforeProcess)"
PLG_FORM_PHP_PROCESS_ONBEFORESTORE="After any images have been uploaded (onBeforeStore)"
Expand Down
38 changes: 38 additions & 0 deletions plugins/fabrik_form/php/php.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,44 @@ public function onBeforeLoad()
return true;
}

/**
* Run during form rendering, when all the form's JS is assembled and ready
* data found in $formModel->data
*
* @return bool
*/

public function onJSReady()
{
$params = $this->getParams();

if ($params->get('only_process_curl') == 'onJSReady')
{
return $this->_runPHP();
}

return true;
}

/**
* Run during form rendering, when all the form's JS is assembled and ready
* data found in $formModel->data
*
* @return bool
*/

public function onJSOpts(&$opts)
{
$params = $this->getParams();

if ($params->get('only_process_curl') == 'onJSOpts')
{
return $this->_runPHP();
}

return true;
}

/**
* Process the plugin, called when form is submitted
*
Expand Down

0 comments on commit bc445cd

Please sign in to comment.