User Tools

Site Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
using_helper_for_options [2018/08/26 12:33] – Add [Related Hooks] and [Sophisticated Usage]. Traumflugusing_helper_for_options [2019/01/28 18:48] (current) – [Parameter Description] Traumflug
Line 325: Line 325:
 == 'submit':'name' == == 'submit':'name' ==
 Name of the HTML button. This is the name of the action PHP sees on submit. Defaults to //submitOptions//, appended by ''$this->table'' of the controller. Name of the HTML button. This is the name of the action PHP sees on submit. Defaults to //submitOptions//, appended by ''$this->table'' of the controller.
 +
 +Note: providing a name not matching one of the well known names (//submitReset//, //submitFilter//, //submitOptions//,..., see ''AdminController->initProcess()'' and ''AdminController->postProcess()''), bypasses automatic actions like storing values in the database. This is an advantage if the module isn't about storing parameters in the database. In this case, incoming data should be handled in the constructor or in ''postProcess()'' of the controller.
  
 == 'submit':'id' == == 'submit':'id' ==
Line 398: Line 400:
 </code> </code>
  
 +===== Panel with Multiple Tabs =====
 +
 +TODO: Helper can also separate fields into multiple tabs. For an example, see
 +AdminThemesController. Key to success is obviously an array 'tabs' in the top level, then an entry 'tab' in each field.
 + 
 ===== Related Hooks ===== ===== Related Hooks =====
  
 ''AdminController->renderOptions()'' calls hook //'action'.''$this->controller_name''.'OptionsModifier'// before rendering. This hooks allows to edit ''$this->fields_options'' and add template variables to ''$this->tpl_option_vars''. ''AdminController->renderOptions()'' calls hook //'action'.''$this->controller_name''.'OptionsModifier'// before rendering. This hooks allows to edit ''$this->fields_options'' and add template variables to ''$this->tpl_option_vars''.
  
 +
 +===== Making Variables Available for JavaScript =====
 +
 +In some cases it's necessary to forward variables from PHP to JavaScript. While the Helper engine has no direct support for this, there are options available.
 +
 +
 +==== Using One Hidden Field Per Variable ====
 +
 +If only one, or only few variables need forwarding, using a [[Helper field 'hidden'|hidden field]] is a good choice.
 +
 +=== PHP Code ===
 +
 +Snippet to insert as one of the [[Helper field|Helper fields]]:
 +<code php>
 +'MY_CONTROLLER_VARIABLE' => [
 +    'type'        => 'hidden',
 +    'auto_value'  => false,
 +    'value'       => _TB_VERSION_,
 +],
 +</code>
 +
 +=== JavaScript Code ===
 +
 +Example to use the value in JavaScript:
 +<code javascript>
 +console.log($('input[name=MY_CONTROLLER_VARIABLE]').val());
 +</code>
 +
 +=== Caveats ===
 +
 +TODO: describe how to avoid storing the value in the database.
 +
 +==== Using One Hidden Field for Many Variables ====
 +
 +With JSON encoding, the same technique can be used for many variables:
 +
 +=== PHP Code ===
 +
 +Snippet to insert as one of the Helper fields:
 +<code php>
 +'MY_CONTROLLER_VARIABLES' => [
 +    'type'        => 'hidden',
 +    'auto_value'  => false,
 +    'value'       => htmlspecialchars(json_encode([
 +        'currentVersion'    => _TB_VERSION_,
 +        'repoApiUrl'        => 'https://api.thirtybees.com/',
 +        'availableChannels' => [ 'channel1', 'channel2' ],
 +    ])),
 +],
 +</code>
 +
 +=== JavaScript Code ===
 +
 +Example to use the value in JavaScript:
 +<code javascript>
 +let json = JSON.parse($('input[name=MY_CONTROLLER_VARIABLES]').val());
 +console.log(json.currentVersion);
 +console.log(json.repoApiUrl);
 +console.log(json.availableChannels);
 +</code>
 +
 +==== Avoid Database Storage ====
 +
 +Especially when using fields just for passing parameters, one probably doesn't want them to be stored in the database. That'd be just a waste of resources.
 +
 +=== PHP Code ===
 +
 +The simple trick is to put an empty field handler method into the controller:
 +<code php>
 +public function updateOptionMycontrollerOption() { }
 +</code>
 +That's all. For finding the required method name, see [[#Usage for Additional Tasks|above]].
  
 ===== Sophisticated Usage ===== ===== Sophisticated Usage =====
using_helper_for_options.1535279624.txt.gz · Last modified: 2018/08/26 12:33 by Traumflug