CMB2 nestable list field type 02

Handling dynamic list in a CMB2 nestable list field type.

I’ve updated the cmb2-tad-fields plugin to be able to deal with dynamic list while preserving user choices as much as possible during updates.
The plugin adds some additional field types to the meta box juggling Custom Meta Boxes 2 WordPress plugin. Starting with a list like this one (an extract of a meta box listing)

... 
array(
    'name' => 'Nested list field',
    'desc' => 'A nested list field',
    'id' => $prefix . 'nested_list',
    'type' => 'tad_nested_list',
    'primary_key' => 'id', // what's unique to each option
    'options' => array(
        array('id' => 1, 'text' => 'One'),
        array('id' => 2, 'text' => 'Two'),
        array('id' => 3, 'text' => 'Three',),
        array('id' => 4, 'text' => 'Four'),
        array('id' => 5, 'text' => 'Five'),
    )
)
...

the field will output this Initial output and the user will sort the entries to his/her liking and update the page/post. First list sorting But what happens if, later, one option is removed from the list?

... 
array(
    'name' => 'Nested list field',
    'desc' => 'A nested list field',
    'id' => $prefix . 'nested_list',
    'type' => 'tad_nested_list',
    'primary_key' => 'id', // what's unique to each option
    'options' => array(
        array('id' => 1, 'text' => 'One'),
        // array('id' => 2, 'text' => 'Two'),
        array('id' => 3, 'text' => 'Three',),
        array('id' => 4, 'text' => 'Four'),
        array('id' => 5, 'text' => 'Five'),
    )
)
...

What happens is that removed elements are removed and their children are reassigned to the closest ancestor. Plucked list Adding elements will simply add them to the list

... 
array(
    'name' => 'Nested list field',
    'desc' => 'A nested list field',
    'id' => $prefix . 'nested_list',
    'type' => 'tad_nested_list',
    'primary_key' => 'id', // what's unique to each option
    'options' => array(
        array('id' => 1, 'text' => 'One'),
        // array('id' => 2, 'text' => 'Two'),
        array('id' => 3, 'text' => 'Three',),
        array('id' => 4, 'text' => 'Four'),
        array('id' => 5, 'text' => 'Five'),
        array('id' => 6, 'text' => 'New'),
    )
)
...

New list element appended

Next

Tests and list groups.