Streams API CP Driver
The CP driver has functions that take care of common PyroCMS control panel routines. Since we know the whole data structure, including validation, we bypass a lot of annoying and repetitive coding.
You can call the entries driver like this:
$this->load->driver('Streams');
$this->streams->cp->function();
Functions
entries_table($stream_slug, $namespace_slug, $pagination = null, $pagination_uri = null, $view_override = false, $extra = array())
Allows you to create a table of entries on the back end, including pagination.
This function returns the table string unless $view_override is set to true, in which case this function will take care of loading the template view for the form.
Parameters
Var | Format | |
---|---|---|
stream_slug | string | The form stream slug. |
namespace_slug | string | The form stream namespace slug. |
pagination | int | If you want pagination activated, set this variable to the number of entries you'd like shown on each page. |
pagination_uri | string | URI to be used in pagination, not including the offset section. No trailing slash. |
view_override | bool | Setting this to true removes the need to load a template in your controller function. |
extra | array | An array of extra parameters that all have default values. See section below. |
$extra Parameters
Param | Format | Default | |
---|---|---|---|
title | string | null | A title for the header bar. |
buttons | array | null | An array of buttons for each row. Each button is an array of the label, url, and optionally confirm. See the example below for more information. -entry_id- is replaced with the row's entry id. |
filters | array | array() | An array of filter inputs to make available for searching entries. Each filter is a field slug with an optional array of dropdown options. |
Example:
$extra['title'] = 'FAQs';
$extra['buttons'] = array(
array(
'label' => lang('global:edit'),
'url' => 'admin/faq/edit/-entry_id-'
),
array(
'label' => lang('global:delete'),
'url' => 'admin/faq/delete/-entry_id-',
'confirm' => true
)
);
$extra['filters'] = array(
'status' => array(
0 => 'Disabled',
1 => 'Enabled'
),
'name',
'description'
);
$this->streams->cp->entries_table('faqs', 'faq', 15, 'admin/faq/index', true, $extra);
It is possible to sort records dynamically wherever you use this method by appending query string to your actual URL.
?order-<stream-slug>=<field-slug>[&sort-<field_slug>=asc|desc]
entry_form($stream_slug, $namespace_slug, $mode = 'new', $entry = null, $view_override = false, $extra = array(), $skips = array())
Creates an entry form for the control panel. Also manages and sets validation.
This function returns the form string unless $view_override is set to true, in which case this function will take care of loading the template view for the form.
Parameters
Var | Format | |
---|---|---|
stream_slug | string | The form stream slug. |
namespace_slug | string | The form stream namespace slug. |
mode | 'new' or 'edit' | Create a new entry or edit one. If set to edit, the $entry param cannot be null. |
entry | int | ID of the entry to edit. |
view_override | bool | Setting this to true removes the need to load a template in your controller function. |
extra | array | An array of extra parameters that all have default values. See section below. |
skips | array | You can remove fields from the form by adding their field slugs to this array. Field slugs in this array will not be put through validation. This is handy if you want to set a form value manually without allowing your user to do it. |
tabs | array | A optional multidimensional array that organizes your fields into tabs. Defaults to false. |
hidden | array | An optional array fields that will be hidden in the form. This is handy for str_id type fields. |
defualts | array | An optional associative array of fields => values that helps prepopulate the form with default data. |
$extra Parameters
Param | Format | Default | |
---|---|---|---|
return | string | current url | The URL to return to after the form submission. **-id-** in this string will get replaced by the edit or new entry id, depending on the entry mode (edit or new). |
success_message | string | generic successful entry submission message | Flash message to show after successful entry submission. |
failure_message | string | generic failed entry submission message | Flash message to show after failed entry submission. |
required | string | <span>*</span> | This defaults to the standard required marker for the PyroCMS CP, so it shouldn't usually be changed, but if you need to you can pass a new value here. |
title | string | null | A title for the header bar. |
email_notifications | bool | false | An array of email notification data. More docs coming on this. |
Example:
$extra = array(
'return' => 'admin/faqs',
'success_message' => lang('faqs:submit_success'),
'failure_message' => lang('faqs:submit_failure'),
'title' => lang('faqs:new')
);
$tabs = array(
array(
'title' => "General Information",
'id' => 'general-tab',
'fields' => array('str_id', 'question', 'answer')
),
array(
'title' => "Additional Information",
'id' => 'additional-tab',
'fields' => array('image', 'category')
)
);
$hidden = array('str_id');
$defaults = array(
'str_id' => md5(now()),
'question' => 'Type your question...'
);
$this->streams->cp->entry_form('faqs', 'faq', 'new', null, true, $extra, $skips = array(), $tabs, $hidden, $defaults);
field_form($stream_slug, $namespace_slug, $method = 'new', $return = null, $assign_id = null, $include_types = array(), $view_override = false, $extra = array())
Creates a custom field form.
This allows you to easily create a form that users can use to add new fields to a stream. This functions as the form assignment as well.
Parameters
Var | Format | |
---|---|---|
stream_slug | string | The form stream slug. |
namespace_slug | string | The form stream namespace slug. |
mode | 'new' or 'edit' | Create a new entry or edit one. If set to edit, the $entry param cannot be null. |
return | string | The URL to return to after the form submission. |
assign_id | int | The field assignment id. |
include_types | array | The field types to include. |
view_override | bool | Setting this to true removes the need to load a template in your controller function. |
extra | array | An array of extra parameters that all have default values. See section below. |
skips | array | You can remove fields from the form by adding their field slugs to this array. Field slugs in this array will not be put through validation. This is handy if you want to set a form value manually without allowing your user to do it. |
$extra Parameters
Param | Format | Default | |
---|---|---|---|
return | string | current url | The URL to return to after the form submission. **-id-** in this string will get replaced by the edit or new entry id, depending on the entry mode (edit or new). |
success_message | string | generic successful entry submission message | Flash message to show after successful entry submission. |
failure_message | string | generic failed entry submission message | Flash message to show after failed entry submission. |
required | string | <span>*</span> | This defaults to the standard required marker for the PyroCMS CP, so it shouldn't usually be changed, but if you need to you can pass a new value here. |
title | string | null | A title for the header bar. |
Example:
$extra = array(
'return' => 'admin/faqs',
'success_message' => lang('faqs:submit_success'),
'failure_message' => lang('faqs:submit_failure'),
'title' => lang('faqs:new')
);
$this->streams->cp->field_form('faqs', 'faq', 'new', 'admin/faqs/index', null, array(), true, $extra);
fields_table($namespace_slug, $pagination = null, $pagination_uri = null, $view_override = false, $extra = array(), $skips = array())
Easily create a table of fields in a certain namespace.
Parameters
Var | Format | |
---|---|---|
namespace | string | The form stream namespace slug. |
pagination | int | If you want pagination activated, set this variable to the number of entries you'd like shown on each page. |
pagination_uri | string | URI to be used in pagination, not including the offset section. No trailing slash. |
view_override | bool | Setting this to true removes the need to load a template in your controller function. |
extra | array | An array of extra parameters that all have default values. See section below. |
skips | array | You can remove fields from the form by adding their field slugs to this array. Field slugs in this array will not be put through validation. This is handy if you want to set a form value manually without allowing your user to do it. |
$extra Parameters
Param | Format | Default | |
---|---|---|---|
title | string | null | A title for the header bar. |
buttons | array | null | An array of buttons for each row. Each button is an array of the label, url, and optionally confirm. See the example below for more information. -entry_id- is replaced with the row's entry id. |
Example:
$extra['title'] = 'FAQ Fields';
$extra['buttons'] = array(
array(
'label' => lang('global:edit'),
'url' => 'admin/faqs/edit/-entry_id-'
),
array(
'label' => lang('global:delete'),
'url' => 'admin/faqs/delete/-entry_id-',
'confirm' => true,
)
);
$this->streams->cp->fields_table('faq', 15, 'admin/faqs/index', true, $extra)
assignments_table($stream_slug, $namespace_slug, $pagination = null, $pagination_uri = null, $view_override = false, $extra = array(), $skips = array())
Easily create a table of field assignments in a certain namespace.
Parameters
Var | Format | |
---|---|---|
namespace_slug | string | The form stream namespace slug. |
pagination | int | If you want pagination activated, set this variable to the number of entries you'd like shown on each page. |
pagination_uri | string | URI to be used in pagination, not including the offset section. No trailing slash. |
view_override | bool | Setting this to true removes the need to load a template in your controller function. |
extra | array | An array of extra parameters that all have default values. See section below. |
skips | array | You can remove fields from the form by adding their field slugs to this array. Field slugs in this array will not be put through validation. This is handy if you want to set a form value manually without allowing your user to do it. |
$extra Parameters
Param | Format | Default | |
---|---|---|---|
title | string | null | A title for the header bar. |
buttons | array | null | An array of buttons for each row. Each button is an array of the label, url, and optionally confirm. See the example below for more information. -entry_id- is replaced with the row's entry id. |
Example:
$extra['title'] = 'FAQ Fields';
$extra['buttons'] = array(
array(
'label' => lang('global:edit'),
'url' => 'admin/faqs/edit/-entry_id-'
),
array(
'label' => lang('global:delete'),
'url' => 'admin/faqs/delete/-entry_id-',
'confirm' => true,
)
);
$this->streams->cp->assignments_table('faqs', 'faq', 15, 'admin/faqs/index', true, $extra);
teardown_assignment_field($assign_id, $force_delete = false)
Tear down an assignment + field combo.
Usually we'd just delete the assignment, but we need to delete the field as well, since there is a 1-1 relationship here.
Parameters
Var | Format | |
---|---|---|
assign_id | int | The field assignment id. |
force_delete | bool | Set to true to force delete the field assignment. |
Example:
$this->streams->cp->teardown_assignment_field($assign_id, true);