Server-only block array attribute
-
I have a plugin that registers a simple custom block server-side only using register_block_type(). It works fine, but I’m looking to add an array attribute to allow the editor to select multiple options. I could do it with multiple boolean attributes, but I thought I’d try with an array as it’d be neater in the GUI.
So I added the following to the attributes array element:
'show' => [ 'label' => 'Information to show', 'type' => 'array', 'items' => [ 'type' => 'string', ], 'enum' => ['Price', 'Location', 'Description', 'Status', 'Conditions', 'Contact'], 'default' => ['Price'], 'autoGenerateControl' => true, ],Note that I had to add autoGenerateControl to get the multi-select control to display on the settings bar, after I did that it displayed as expected with the default value correctly loaded.
But if I try to add or remove options using the multi-select control, two things happen:
- The post/page isn’t marked as “dirty” so I can’t save it, and
- The block itself displays “Error loading block: [object Object]”
Note that if I remove all options from the multi-select control, the block displays as expected, it only displays this message if I select one or more options. Even though the render function is not yet coded to use the array attribute at all.
Is it possible to do this? I’d really prefer if it can be done with server-side hooks only, as I’d rather not have to refactor the whole plugin to use npm. Any help appreciated.
You must be logged in to reply to this topic.