Home>Support>Modify Form and filters not working

Modify Form and filters not working

By C, 4 months ago. Last reply by Alex S, 4 months ago.

Hi,

I appear to have the exact same issue as described here. I’ve tried modify_form and the siteorigin_widgets_form_options filters but the updated form fields don’t carry through.

Hope you can help.

Thanks

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

  1. 4 months, 22 days ago Alex S
    Hi, I Work Here

    Hi,

    Thanks for getting in touch. I’ve run a few tests and modify_form, and the siteorigin_widgets_form_options filter is working as expected. Can you please provide me with a code snippet of your code? This will allow me to inspect your implementation.

    For information on using modify_form, and that filter, I recommend reading the docs here: https://siteorigin.com/docs/widgets-bundle/form-building/modifying-forms/

    Kind regards,
    Alex

  2. 4 months, 22 days ago C

    Hi Alex,

    Thanks for getting back to me. Those were the docs I was looking at. I’m sure I’m missing something but I can’t quite figure out what!

    Here’s the code:

    /*
    Widget Name: Summary
    Description: Summary pulled from category fields.
    Version: 1.0
    */
    
    
    class SiteOrigin_Widget_Summary_Widget extends SiteOrigin_Widget {
    
    	function __construct() {
    	
    		parent::__construct(
    			'summary',
    			__( 'Summary' ),
    			array(
    				'description' => __( 'A summary widget that pulls information from category fields.' )
    			),
    			array(
                    'width' => 250
    			),
    			array(
    				'title' => array(
    					'type' => 'text',
    					'label' => __( 'Title' ),
    				),
                    'image' => array(
    					'type' => 'media',
    					'label' => __( 'Image' ),
    					'library' => 'image'
    				),
    				'page' => array(
    					'type' => 'autocomplete',
    					'label' => __( 'Page' ),
    					'post_types' => 'pages',
    				),
    				'category' => array(
    					'type' => 'select',
    					'label' => __( 'Category' ),
    					'options' => []
    				),
    			),
    			plugin_dir_path(__FILE__)
    		);
    	}
    
    	function initialize(){
                
    	}
    
    	function get_template_name($instance) {
    		return 'base';
    	}
    
    
    	function get_style_name($instance) {
    		return false;
    	}
    
    	function modify_form( $form ) {
    		
            $form['category']['options'] = [ 
    			'one' => 'one',
    			'two' => 'two'
    		];
    
            return $form;
        }
    
    }
    
    siteorigin_widget_register('summary', __FILE__, 'SiteOrigin_Widget_Summary_Widget');
    
  3. 4 months, 21 days ago Alex S
    Hi, I Work Here

    Hi,

    Thank you for the snippet. modify_form isn’t triggered when you pass $form_options during the construct. You’ll need to add the categories directly to $form_options or pass an empty $form_options, and set the form options using get_widget_form (the latter being my recommendation in general). I’ll log a note to mention this restriction and suggestion soon.

    There’s a typo in the provided snippet. The post_types you’re passing to the autocomplete form is a string rather than an array. The core page post type is also singular. Here’s the fixed autocomplete form option:

    'page' => array(
    	'type' => 'autocomplete',
    	'label' => __( 'Page' ),
    	'post_types' => array(
    		'page'
    	),
    ),

    Kind regards,
    Alex

  4. 4 months, 20 days ago C

    Thanks Alex, very helpful as ever.

    I knew it would be something like that; annoyed with myself for not finding the alternative approach simply by looking at an existing widget! I was attempting to use modify_form because the category terms were not available when initialising the form fields in the constructor (too early perhaps?). get_widget_form is all I needed (without modify_form) as the terms are available at that point.

    The restriction and suggestion would be a useful addition to the docs. I notice the ‘Creating a Widget‘ page demonstrates initialising the form fields in the constructor so it might be worth including a reference to get_widget_form there too.

    Many thanks for correcting those other errors – above and beyond!

  5. 4 months, 20 days ago Alex S
    Hi, I Work Here

    Hi C,

    I’m glad that helped. This was definitely on us. I’ll submit a fix for this now.
    Please feel free to open a new thread if anything else comes up.

    Kind regards,
    Alex

  6. 4 months, 19 days ago C

    Hi Alex,

    I’ve just spotted where I got that autocomplete code from in case you want to update that too: https://siteorigin.com/docs/widgets-bundle/form-building/form-fields/#heading-autocomplete.

    Thanks

  7. 4 months, 12 days ago Alex S
    Hi, I Work Here

    Hi,

    Ah! Thanks for letting me know. I’ll submit a fix for that shortly.

    Kind regards,
    Alex

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