Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • getPackageFields
  • getAdminAddFields
  • getClientAddFields
  • getAdminEditFields
  • getClientEditFields

Creating ModuleFields

Creating fields using the ModuleFields class allows you to define a set of input fields that may be displayed in a variety of contextcontexts, without worrying about how those fields are contained. There are three aspects of creating a field, described below, that includinginclude: creating the field label, creating the field, and attaching the field to the label.

...

Code Block
languagephp
title/components/modules/my_module/my_module.php
<?php 
class MyModule extends Module { 

    ...

    public function getPackageFields($vars=null) {
        // Create the label
        $fields = new ModuleFields();
        $my_label = $fields->label("My Field Label", "my_field_id");
        
        // Create the field
        $my_field = $fields->fieldText("my_field_name", "default value", array('id' => "my_field_id"));
 
        $fields->setField($my_label);
        return $fields;
    }
}
?>

...

Code Block
languagephp
title/components/modules/my_module/my_module.php
<?php 
class MyModule extends Module { 

    ...

    public function getPackageFields($vars=null) {
        // Create the label
        $fields = new ModuleFields();
        $my_label = $fields->label("My Field Label", "my_field_id");
        
        // Create the field
        $my_field = $fields->fieldText("my_field_name", "default value", array('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;
    }
}
?>

...

Code Block
languagephp
title/components/modules/my_module/my_module.php
<?php 
class MyModule extends Module { 

    ...

    public function getPackageFields($vars=null) {
        // Create the label
        $fields = new ModuleFields();
        $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", array('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;
    }
}
?>

...

Info
titleMultiple tooltips may be created per label

... but that would be silly.

...


Complex Fields

Blesta supports ModuleFields up to two levels deep. This allows you to easily create a set of checkboxes or radio buttons associated with a single field.

Code Block
languagephp
title/components/modules/my_module/my_module.php
<?php 
class MyModule extends Module { 

    ...

    public function getPackageFields($vars=null) {
        // Create the label
        $fields = new ModuleFields();
        $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"), array('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;
    }
}
?>

...

Code Block
languagephp
title/components/modules/my_module/my_module.php
<?php
class MyModule extends Module {

    ...

    public function getPackageFields($vars=null) {
        $fields = new ModuleFields();
        $fields->setHtml("
            <script type=\"text/javascript\">alert('ok!');</script>
        ");

    }
}
?>

Overriding Package/Service Fields

Generally it is a good idea to name your fields such that they work within the scope of your own module. For example, if your module requires a field named "term", naming it instead "my_module_term" will prevent conflicts with fields used by the system. However, in some cases you may want to allow your module to set a service field directly. One such example is the quantity field (qty). Here's how you can set a quantity field when creating a service.

Code Block
languagephp
title/components/modules/my_module/my_module.php
<?php
class MyModule extends Module {

    ...

    public function getAdminAddFields($package, $vars=null) {
        Loader::loadHelpers($this, array("Html"));

        $fields = new ModuleFields();

		// Create qty label
        $qty = $fields->label("Quantity", "my_qty");
        // Create qty field and attach to qty label
        $qty->attach($fields->fieldText("qty", $this->Html->ifSet($vars->qty), array('id' => "my_qty")));
        // Set the label as a field
        $fields->setField($qty);

    }
}
?>