Home>Support>Indexing broken when using custom field in repeater

Indexing broken when using custom field in repeater

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');

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 8 years, 6 months ago Greg Priday
    Hi, I Work Here

    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?

  2. 8 years, 6 months ago Jim Robert Nilsson

    Hi Greg

    Yes, the problem for me is that the #products# variable is not being replaced by the repeater index in my HTML.

    Screen Shot 2016-03-08 at 10.19.53

    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?

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More