Unable to overwrite prefix or namespace for ACF blocks
When I generated a new block called Hero and I tried to overwrite the prefix from acf/ to custom/ for example. Problem was that it still rendered with acf/ prefix in the Gutenberg editor. So all my fields were missing from the block. But, when I cached the block.json using wp acorn acf:cache, then it had the correct prefix. But problem with caching was that I had to add re-insert the block for it to work again. Since it showed that acf/hero block was missing. Same problem goes overwriting the namespace.
public $prefix = 'custom/';
It's possible using acf/ for the prefix is required in the latest versions of ACF Pro. I can look into it more/confirm sometime next week.
FWIW, you would always have to repair/re-insert your existing blocks when changing the prefix.
I was looking into this. ACF Pro hardcodes the namespace to acf/ when blocks are registered via PHP instead of block.json:
From the acf_validate_block_type() function in advanced-custom-fields-pro/pro/blocks.php:
if ( $block['name'] ) {
$block['name'] = 'acf/' . acf_slugify( $block['name'] );
}
After the block array is passed through that function, it's passed through the acf/register_block_type_args filter. So to overwrite the prefix for both cached and uncached blocks you would need to both set the $prefix property as done in the original post and hook into the acf/register_block_type_args with something like this:
add_filter('acf/register_block_type_args', function (array $block): array {
$block['name'] = str_replace('acf/', 'custom/', $block['name']);
return $block;
});
Edit: This is true as of the latest ACF Pro version, 6.3.11.
Thanks for looking into this @thunderdw
I think it might be sane to just deprecate being able to configure this in future versions. Is there ever really a purpose in customizing the namespace outside of some sort of extra customization? Otherwise, I'm open to a PR for a clean implementation of the workaround if others insist on having this.
This would now be a possible change to implement as of #311 with ACF Composer having it's own way to register ACF blocks with PHP– but it might catch someone off guard if they are customizing the namespace as-is since ACF currently converts my-namespace/example to acf/my-namespace-example.
With how ACF 6+ handles blocks – I'm going to heavily recommend not doing this. The $namespace property will still be exposed for compatibility purposes.