Hi there, thanks for some awesome tools.
I have made a custom field class for a Widget I’m building. The custom field resides in a plugin based on this sample code: https://github.com/siteorigin/so-dev-examples/blob/develop/extend-widgets-bundle/custom-fields/better-text.class.php. I have followed your documentation and I got the field working as it should, except when I put it inside a repeater. When I put it inside a repeater the indexing of the name-field seems to be incorrect. Instead of a number I get a variable that looks like #products# where products is the name of my repeater. Like so:
widgets[c60][products][#products#][start-date]
What can possibly be the reason for this?
My Custom Field class:
class My_Custom_Date_Time_Picker extends SiteOrigin_Widget_Field_Base {
protected function render_field( $value, $instance ) {
?>
<input
type="date"
id="<?php echo $this->element_id ?>"
name="<?php echo $this->element_name ?>"
value="<?php echo esc_attr( $value ); ?>"
/>
<?php
}
protected function sanitize_field_input( $value, $instance ) {
$sanitized_value = sanitize_text_field( $value );
return $sanitized_value;
}
}
My Widget code
class TestWidget extends SiteOrigin_Widget {
function __construct() {
//Here you can do any preparation required before calling the parent constructor, such as including additional files or initializing variables.
//Call the parent constructor with the required arguments.
parent::__construct(
// The unique id for your widget.
'test',
// The name of the widget for display purposes.
__('Test', 'jimbo'),
// The $widget_options array, which is passed through to WP_Widget.
// It has a couple of extras like the optional help URL, which should link to your sites help or support page.
array(
'description' => __('A simple test', 'jimbo'),
),
//The $control_options array, which is passed through to WP_Widget
array(
),
//The $form_options array, which describes the form fields used to configure SiteOrigin widgets. We'll explain these in more detail later.
array(
'products' => array(
'type' => 'repeater',
'label' => __( 'Products', 'jimbo' ),
'item_name' => __( 'Product (article number)', 'jimbo' ),
'fields' => array(
'start-date' => array(
'type' => 'date-time-picker',
'label' => __( 'Choose a start date', 'jimbo' )
)
)
)
),
//The $base_folder path string.
plugin_dir_path(__FILE__)
);
}
function get_template_name($instance) {
return 'test-template';
}
function get_style_name($instance) {
return '';
}
}
siteorigin_widget_register('test', __FILE__, 'TestWidget');
Hi Jim
This is actually expected behaviour. If you try using a standard text field, you’ll see it gives the same type of name attribute. The widgets bundle replaces the #products# part of the field’s name attribute using Javascript. Is this not happening for you?
Hi Greg
Yes, the problem for me is that the #products# variable is not being replaced by the repeater index in my HTML.
I don’t get any javascript errors and all the original fields from your plugin is working as expected. Do you have any idea why this might happen?