The InputFields class is automatically made available to any messenger. To begin using it simply initialize the object within your messenger method.
The following messenger methods must return an InputFields object:
Creating fields using the InputFields class allows you to define a set of input fields that may be displayed in a variety of contexts, without worrying about how those fields are contained. There are three aspects of creating a field, described below, that include: creating the field label, creating the field, and attaching the field to the label.
Creating a label will return a InputFields object.
<?php class MyMessenger extends Messenger { ... public function getConfigurationFields(&$vars = []) { // Create the label $fields = new InputFields(); $my_label = $fields->label('My Field Label', 'my_field_label_id'); $fields->setField($my_label); return $fields; } } ?> |
As with creating a label, creating a field will return a InputField object. The example below uses the InputFields::fieldText() method, but others exist for creating radio buttons, textarea fields, and everything else you'll need.
<?php class MyMessenger extends Messenger { ... public function getConfigurationFields(&$vars = []) { // Create the label $fields = new InputFields(); $my_label = $fields->label('My Field Label', 'my_field_id'); // Create the field $my_field = $fields->fieldText('my_field_name', 'default value', ['id' => "my_field_id"]); $fields->setField($my_label); return $fields; } } ?> |
A field may be attached to a label using the InputField::attach() method, or, instead, attach a label to a field using InputField::setLabel().
<?php class MyMessenger extends Messenger { ... public function getConfigurationFields(&$vars = []) { // Create the label $fields = new InputFields(); $my_label = $fields->label('My Field Label', 'my_field_id'); // Create the field $my_field = $fields->fieldText('my_field_name', 'default value', ['id' => "my_field_id"]); // Attach the field to the label $my_label->attach($my_field); // OR, attach the label to the field, instead // $my_field->setLabel($my_label); $fields->setField($my_label); return $fields; } } ?> |
Tooltips can be created using the InputFields::tooltip() method, then attached using InputField::attach(). In the example below we've eliminated unnecessary variable declarations to give a more concise example.
<?php class MyMessenger extends Messenger { ... public function getConfigurationFields(&$vars = []) { // Create the label $fields = new InputFields(); $my_label = $fields->label('My Field Label', 'my_field_id'); // Create and attach the field to the label $my_label->attach($fields->fieldText('my_field_name', 'default value', ['id' => "my_field_id"])); // Attach a tooltip to the label $my_label->attach($fields->tooltip('This is my tooltip')); $fields->setField($my_label); return $fields; } } ?> |
A tooltip may not be attached to a label that is, itself, attached to a field. |
... but that would be silly. |
Blesta supports InputFields up to two levels deep. This allows you to easily create a set of checkboxes or radio buttons associated with a single field.
<?php class MyMessenger extends Messenger { ... public function getConfigurationFields(&$vars = []) { // Create the label $fields = new InputFields(); $my_label = $fields->label('My Field Label', 'my_field_id'); // Create a field label displayed next to the checkbox $field_label = $fields->label('My Value', 'my_field_my_value_id'); // Create and attach the field to the label, set as checked (3rd param) if necessary $my_label->attach( $fields->fieldCheckbox( 'my_field_name', 'my_value', (isset($vars['my_field_name']) && $vars['my_field_name'] == 'my_value'), ['id' => 'my_field_my_value_id'], $field_label ) ); // Attach a tooltip to the label $my_label->attach($fields->tooltip('This is my tooltip')); $fields->setField($my_label); return $fields; } } ?> |
The InputFields class supports HTML content as well. You may choose to set nothing but HTML content, but the best practice is to set fields using the method mentioned in the section above and only set javascript and that sort of thing as HTML content.
To set HTML content use the InputFields::setHtml() method.
<?php class MyMessenger extends Messenger { ... public function getConfigurationFields(&$vars = []) { $fields = new InputFields(); $fields->setHtml(' <script type="text/javascript">alert(\'ok!\');</script> '); } } ?> |