Methods
The following methods can be placed in your field type file and will be run by PyroStreams at various times. The only truly required method is form_output, the rest are optional.
Basic Data Formatting
The following functions are available for data formatting and saving and make up the core of what you can do with PyroStreams field types.
form_output($data, $entry_id, $field)
The only required method. This should return the form output for this field. It takes the following parameters:
Parameter | Type | Description |
---|---|---|
data | array | An array of data on the field. |
entry_id | int | The current entry id. This is null if not being used in an editing entry context. |
field | object | Object of field data. |
The $data parameter contains the following values:
Parameter | Description |
---|---|
form_slug | The slug of the form input. Usually is used as the "name" attribute. |
value | The current value of the input. Left blank if there is no value. |
custom | Contains an associative array of the custom parameter values for your field. IE: max_length. |
Example:
public function form_output($data)
{
$options['name'] = $data['form_slug'];
$options['id'] = $data['form_slug'];
$options['value'] = $data['value'];
return form_input($options);
}
pre_output($input, $data)
When data needs a singular display, you can use this plugin to provide it. For instance, in the encrypt field type, the `pre_output` function is used to unencrypt the data before it is displayed. If you use the `pre_output_plugin` function (see below) to return multiple variables to be used in a plugin context, you can use `pre_output` to return a canonical display for places where there is no opportunity to choose which variables to display. For instance, in the Date/Time field type, the `pre_ouput` function formats the date according to the site's date formatting setting.
This plugin takes the following parameters:
Parameter | Description |
---|---|
input | The value stored in the database. A string. |
data | An associative array of the custom parameter values for your field. IE: max_length. |
Example:
public function pre_output($input)
{
$this->CI->load->library('encrypt');
return $this->CI->encrypt->decode($input);
}
When being used in a plugin, this method will be overridden by the pre_output_plugin method, if it exists.
pre_output_plugin($input, $params, $row_slug)
Sometimes you need to return multiple pieces of data for a single field. For instance, the image field returns separate variable for the image filename, the full path, the mime type, and more. If you do return an array of variables using `pre_output_plugin`, they can be accessed like this:
{{ field_slug:array_key }}
This is only really needed on the plugin side, however, as on the back end you just need one string that represents the data point in your table (which is what pre_output can be used for).
The `pre_output_plugin` method takes the following parameters:
Parameter | Description |
---|---|
input | The value stored in the database. A string. |
params | An associative array of the custom parameter values for your field. IE: max_length. |
field_slug | String - the field slug. |
Example:
public function pre_output_plugin($input)
{
if ($input == 'a')
{
return array('choice' => 'you chose a!', 'a_was_chosen' => true);
}
else
{
return array('choice' => 'you did not choose a', 'a_was_chosen' => false);
}
}
You can also return a string with the pre_output_plugin
method if you'd like.
pre_save($input, $field, $stream, $row_id, $form_data)
This method allows you to modify the form input data in some way before it is saved to the database. It takes four parameters:
Parameter | Type | Description |
---|---|---|
input | mixed | The value from the form input, to be saved into the database. |
field | object | An object containing the field slug, field_name, and an associative array of custom mateter values under "field_data". Used like `$field->field_slug` or `$field->field_data['max_length']`. |
stream | object | An object of stream data. IE: `$stream->stream_slug`. |
row_id | int | The id of the current row. Is `null` if this is a new entry and it hasn't been saved yet. |
form_data | array | An array containing the values from all the form fields. |
Example:
public function pre_save($input)
{
$this->CI->load->library('encrypt');
return $this->CI->encrypt->encode($input);
}
event($field)
This function is called before any other field function is called, allowing you to do things like add metadata to the admin pages, for example. This is where you can put the functions:
public function event($field)
{
$this->CI->type->add_css('datetime', 'datepicker.css');
}
Alternative Formatting
The above functions assume that we want to store a value in the database, but that isn't always the case. For instance, for the multiple relationship field type, we never store anything in the database – all data is stored in a separate binding table. Having an actual column in the database would be unecessary and a waste of space.
To deal with special cases like this, streams has provisions for alternative functionality that breaks away from the one-to-one data in column model. The following functions override other basic functions to give you flexibility in how you field type works.
Setting your Field Type to alt_process
If you want to tap into the alt process functionality, you first need to add a class variable to your field type that tells streams to treat your field type as an alternate process field type:
public $alt_process = true;
After that, you are free from having to store values in the database. You can use the constructs/destructs to do whatever actions you'd like. For instance, on the multiple relationship field, there is a separate plugin that is used to display related entries using the binding table create and maintained separately from the normal field processes.
However, you need to have a way to display data from this field on the back end if the user wants to. To display data on the back end, you should use the alt_pre_output
plugin below.
alt_pre_output($row_id, $params, $field_type, $stream)
The alt_pre_output
method will be called in lieu of pre_output
on the back end. It allows users to show a canonical data display when viewing entry data on the back end.
Parameter | Type | Description |
---|---|---|
row_id | int | The id of the current row. Is null if this is a new entry and it hasn't been saved yet. |
params | array | An associative array of the custom parameter values for your field. IE: max_length. |
field_type | object | The actual field type object. |
stream | object | An object of stream data. IE: $stream->stream_slug |
Constructs/Destructs
Construct/destruct functions tap into streams functionality when data and field assignments are created and destoryed.
field_assignment_construct($field, $stream)
Called right before a field is added to a stream.
Parameter | Type | Description |
---|---|---|
field | object | Object of the field that is being added to the stream. |
stream | object | Object of the stream that the field is being added to. |
field_assignment_destruct($field, $stream)
Identical to field_assignment_construct
only called right before a field assignment is removed.
entry_destruct($entry, $field, $stream)
Called right before an entry is deleted.
Parameter | Type | Description |
---|---|---|
entry | object | Object of the entry that is being deleted. |
field | object | Object of the field that is being deleted. |
stream | object | Object of the stream that the field is being deleted. |
Wondering where entry_construct
is? You can use pre_save
for that.