Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

Custom Option Type Input Value Not Updating On Change Only Save Publish

Open NwpDeveloper opened this issue 10 years ago • 9 comments

When I the print a custom option in customizer the value doesn't update in the iframe when input is changed. It only updates on save.

However, if it's a default Unyson option it updates on change and save.

Example: I duplicated text option and run fw_print(fw_get_db_customizer_option()) in view.php and view it in customizer. The option_type default value is set to empty string, but the print returns empty array.

If I type text into input the print doesn't update the iframe until I hit Save & Publish.

NwpDeveloper avatar Mar 27 '16 19:03 NwpDeveloper

It doesn't look like the _get_value_from_input method is working properly when I create a custom option. I'm unable to print the default value defined in customizer.php and it doesn't update on change.

Using init.php and framework-extensions

NwpDeveloper avatar Mar 28 '16 00:03 NwpDeveloper

Example: Duplicate of text option as foo option.

customizer.php

'test1' => array(
     'type' => 'fw-foo',
     'value' => 'test1',    
),

view.php

$option_value = fw_get_db_customizer_option('test1');
fw_print($option_value);

The fw_print($option_value) doesn't update when iframe refreshes on change only after I hit 'save and publish' in customizer.

However, I am seeing the values change in $_POST. Am I missing something from the documentation on how to implement a custom option?

NwpDeveloper avatar Mar 28 '16 00:03 NwpDeveloper

On which action you register your option type?

ghost avatar Mar 28 '16 10:03 ghost

function _action_theme_include_custom_option_types() {
    require_once dirname(__FILE__) . '/option-types/fw-foo/class-fw-option-type-foo.php';
}
add_action('fw_option_types_init', '_action_theme_include_custom_option_types');

That's inside {theme}/inc/includes/option-types.php

NwpDeveloper avatar Mar 28 '16 14:03 NwpDeveloper

This is the class:

class FW_Option_Type_Foo extends FW_Option_Type {
    public function get_type() {
        return 'fw-foo';
    }

    /**
     * @internal
     * {@inheritdoc}
     */
    protected function _enqueue_static( $id, $option, $data ) {
    }

    /**
     * @param string $id
     * @param array $option
     * @param array $data
     *
     * @return string
     *
     * @internal
     */
    protected function _render( $id, $option, $data ) {
        $option['attr']['value'] = (string) $data['value'];
        fw_print(debug_backtrace(true));
        return '<input ' . fw_attr_to_html( $option['attr'] ) . ' type="text" />';
    }

    /**
     * @param array $option
     * @param array|null|string $input_value
     *
     * @return string
     *
     * @internal
     */
    protected function _get_value_from_input( $option, $input_value ) {

        return (string) ( is_null( $input_value ) ? $option['value'] : $input_value );
    }

    /**
     * @internal
     */
    protected function _get_defaults() {
        return array(
            'value' => ''
        );
    }
}

FW_Option_Type::register('FW_Option_Type_Foo');

NwpDeveloper avatar Mar 28 '16 14:03 NwpDeveloper

If that class inside simple.php then iframe refresh updates fine. If option in theme doesn't update on refresh only save.

NwpDeveloper avatar Mar 28 '16 14:03 NwpDeveloper

  1. I created {theme}/test/option.php with this code

  2. Included it in {theme}/functions.php

    function _action_theme_include_test_option_type() {
        require_once dirname(__FILE__) . '/test/option.php';
    }
    add_action('fw_option_types_init', '_action_theme_include_test_option_type');
    
  3. Added in {theme}/framework-customizations/theme/options/customizer.php

    $options = array(
        'test' => array(
            'type' => 'fw-foo',
            'value' => 'testing',
        ),
    );
    
  4. In {theme}/header.php I added

    <?php fw_print(fw_get_db_customizer_option()); ?>
    

How when I change the input value in customizer the preview is updated and the value from fw_print() too.

ghost avatar Mar 31 '16 12:03 ghost

I followed directions from docs on how to create custom options. http://manual.unyson.io/en/latest/options/create-option-type.html#content

I think the issue is where fw_option_types_init is called. It works fine when in functions.php, but I was following the docs and call from {theme}/inc/hooks.php. I also tried anther example where it was in {theme}/inc/include/option-types.php.

So is the correct way to call fw_option_types_init inside functions.php and not in hooks.php or option-types.php?

NwpDeveloper avatar Mar 31 '16 18:03 NwpDeveloper

It is correct to call it very early, before the action happens. I don't know why your hooks.php is executed later.

ghost avatar Mar 31 '16 18:03 ghost