• Resolved Martin Taylor

    (@docdunning)


    The block I’ve defined needs a yes/no switch, so I first tried the Checkbox field, defaulting to checked.

    So – when the block is added to a post or page, the checkbox is ticked. But there’s a problem … the value I receive in my “block.php” script for the checkbox is set to false. I have to untick and then tick the checkbox in order to get a value of true into my “block.php” script.

    I tried switching to a Toggle field, but the same happens. When I use block_value(“fieldname”), it’s always false until the toggle has been switched off and on again.

    The workaround is to use a default of “Off” / “Unchecked” etc, and force the user to switch it on, but that’s not ideal.

    I’d like the default to be “On”, but it looks like it doesn’t work. Or have i misunderstood something?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Thanks for letting me know about that.

    That should have been fixed in this PR, but it’s not released on wp.org yet:
    https://github.com/studiopress/genesis-custom-blocks/pull/60

    I’m not sure when we’ll release it.

    Could you do me a huge favor and see if this .zip of the plugin fixes it?

    https://github.com/studiopress/genesis-custom-blocks/pull/60#issuecomment-802247810

    Thanks, Martin!

    Plugin Contributor Ryan Kienstra

    (@ryankienstra)

    Though that won’t do exactly what you mentioned. It makes the default of the Toggle and Checkbox fields false, like in the previous editor from 1.0.3.

    Thread Starter Martin Taylor

    (@docdunning)

    @ryankienstra

    Hi Ryan – I think that has done the trick. I see what you mean, that when creating the block, the default is now “false”, but that’s not the issue that I had.

    My issue was when using the block. Although the default was “true”, and the UI was showing the Toggle / Checkbox controls as checked, they were passing “false” to the block.php handler.

    I did have to delete and recreate the block with the updated plugin before it worked as expected, but that’s no big deal in my case.

    Happy to do some more checks/diagnostics if that would help.

    Martin

    Plugin Contributor Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Ah, OK. I should have mentioned that recreating the field or block would be needed.

    I did have to delete and recreate the block with the updated plugin before it worked as expected, but that’s no big deal in my case.

    After recreating the block, did block_field() or block_value() have the correct result?

    For example, here are the steps I took:
    1. Create a new GCB block
    2. Add a new Checkbox field, leaving the default as unchecked: https://cldup.com/lN_AwV5TwQ.png
    3. Go to Gutenberg
    4. Add that block: https://cldup.com/K_uxa0TQf0.gif
    5. Click away from it so the template displays
    6. Expected: block_field( 'your-field-name' ) echoes 'No'

    Thread Starter Martin Taylor

    (@docdunning)

    Hi @ryankienstra

    All good, as far as I can see. I set up a block with two Checkboxes and two Toggles, with one of each defaulting “True” and the other “False”.

    My block.php code runs like this:

    <?php
    echo 'Check false: ',block_field( 'check-false' ),'<br>';
    echo 'Check true: ',block_field( 'check-true' ),'<br>';
    echo 'Toggle false: ',block_field( 'toggle-false' ),'<br>';
    echo 'Toggle true: ',block_field( 'toggle-true' ),'<br>';
    echo 'Check false: ';var_dump(block_value( 'check-false' )); echo '<br>';
    echo 'Check true: ';var_dump(block_value( 'check-true' )); echo '<br>';
    echo 'Toggle false: ';var_dump(block_value( 'toggle-false' )); echo '<br>';
    echo 'Toggle true: ';var_dump(block_value( 'toggle-true' )); echo '<br>';

    I add the block to a page and click away, and see this (as expected):
    Check false: No
    Check true: Yes
    Toggle false: No
    Toggle true: Yes
    Check false: bool(false)
    Check true: bool(true)
    Toggle false: bool(false)
    Toggle true: bool(true)

    I won’t use this pre-release version on the live site as yet though.

    Plugin Contributor Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Great, thanks a lot for testing it and sharing the code.

    Right, that .zip file isn’t production-ready, unfortunately. I’m not sure when it’ll be released, but I’ll ping you here if you’d like.

    Plugin Contributor Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Sorry for the long delay. This issue should be fixed in yesterday’s 1.2.0 release.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Toggle and Checkbox always initially false’ is closed to new replies.