Hi, if I use select_advanced field on a MB block, and use multiple of this block on same page, only one of the blocks has working select2 functionality. This is due to all the inputs have same ID accross all the blocks.
I managed to quickfix it like so:
function dg_fix_metabox_select_advanced_gutenberg() {
// Ladataan vain Gutenberg editorissa
$screen = get_current_screen();
if ( $screen && $screen->is_block_editor ) : ?>
<script>
(function($) {
function forceMetaBoxSelectAdvanced($context) {
console.log('Re-initializing Select2 in:', $context);
$context.find('.rwmb-select_advanced').each(function() {
var $select = $(this);
var oldId = $select.attr('id');
// Varmista uniikki ID
var newId = 'dg-select2-' + Math.random().toString(36).substr(2, 9);
console.log('Rewriting ID:', oldId, '=>', newId);
$select.attr('id', newId);
// Varmista että optiot on olemassa
if (rwmb && rwmb.select2 && $select.data('options')) {
rwmb.select2.init($select);
}
});
}
$(document).on('mb_ready', function(e) {
setTimeout(function() {
forceMetaBoxSelectAdvanced($(document));
}, 300);
});
$(document).on('mb_block_view_init', function(e, view) {
setTimeout(function() {
forceMetaBoxSelectAdvanced(view.$el);
}, 300);
});
})(jQuery);
</script>
<?php
endif;
}
add_action( 'admin_footer', 'dg_fix_metabox_select_advanced_gutenberg' );
Please consider having unique ID:s on the input fields on gutenberg blocks.