Versions Compared

Key

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

...

You add a PHP class in the folder for custom rule conditions. The folder is in the customization folder directory in

/rule_condition_types/

You pick a name for your condition type (like ‘Example’). The PHP class needs to be called CustomCondition followed by your type name. So for type ‘Example’ you got CustomConditionExample. The file name is called the same (CustomConditionExample.php). The class extends ConfigboxCondition. So you got class CustomConditionExample extends ConfigboxCondition.

...

Each type gets a panel in the rule editor. This method returns the HTML to use for it. It essentially contains a list of conditions (the HTML of each condition has a certain structure, you will see more about it that in the method getConditionHtml().

...

getConditionHtml($conditionData, $forEditing = true)

This method takes the condition’s data Condition Data as parameter and returns the HTML for the condition. Depending on $forEditing, it returns HTML suitable for the editor or just visual HTML for the form’s property. You see an example of the HTML in Example’s PHP class, method CustomConditionExample::readMe(). The gist of it is that each condition is wrapped in a <span> Element with HTML 5 data attributes containing the condition’s data. In the <span> is a human-readable name of the condition, the operator to choose from and a text field for the set point, the value the thing you test for should have. Those text fields have the CSS class ‚input’ and a data attribute called ‚data-condition-key’ that holds the key of the entry in the condition data. During saving, those text fields are stored like normal condition data, no need to have them as data in the wrapping <span> element.Whatever you add as condition data will stored when the rule is stored and will be available to you in the following methodHTML structure is essentially this:

  • A wrapping <span> with the CSS classes item and condition

  • Condition data in form of data attributes in that <span> (attribute data-type is required, rest is up to implementation of the condition type)

  • Optionally <input> elements that add to the Condition Data. They must have the CSS class ‘input' and a data attribute ‘data-data-key’ that holds the Condition Data Item’s key and an attribute ‘value’ that holds the value (and facilitates user input in the rule editor)

  • You add any HTML as you see fit to make the condition practical for the administrator

Example:

Code Block
languagehtml
<span class="item condition" data-type="example" data-operator="..." data-name="...">
  ...
</span>

See notes about data-operator later.

Code Block
languagehtml
<span class="item condition" data-type="example" data-operator="..." data-name="...">
  <span class="condition-name">...</span>
  <span class="condition-operator">...<span>
  <input class="input" data-data-key="..." value="..." />
</span>

Note: The .condition-operator <span> is special - the rule editor JS takes care of displaying the choices on click and storing the choice in the .condition’s data attribute ‘data-operator’. Making use of the operator is optional.

The example uses … for simplicity. Use the $conditionData array which contains key/value pairs of all condition data.

How does all that play out? When the admin saves the rule, the system will scan through the conditions that it finds in the rule area, processes the data attributes in the <span>, processes the .input text fields within and and creates so called ‘Condition Data’. Together with the data of combinators (OR, AND) and the parentheses, it creates ‘Rule Data’ - essentially a JS array of objects that eventually will be saved as a JSON string.

See CustomConditionExample::readMe() for a line-by-line explanation of the HTML structure.

...

This method is called when the condition is evaluated as part of the whole rule. $conditionData contains everything that was set as $conditionData Condition Data in the previous method. $selections holds an a standardised array with the current configuration that is tested.

The method has to return true or false. The method should be as fast as possible. Cache Developers need to take great care about performance of this method. Cache/Memoize where you can since this method may be called multiple times each time a customer makes a selection in the configurator.

...

Data Key

Data Value

Description

type (required)

Example

The name of your condition’s type

name

Condition 1, Condition 2, Condition 3

Only used for display

fieldname

field_1, field_2, field_3

Arbitrary values to use in evaluating the condition

operator

==

The default operator for evaluating the condition

shouldValue

(empty) – will be populated by the user in the editor

The value the thing to test for should have for the condition to be met

You can have make as much many condition data items in your conditions condition type as you need, the see fit. The data will be available to you in the method getEvaluationResult().

...