HTML entities are not being decoded when using WordPress REST API for Gutenberg blocks

It's not immediately obvious, but there is in fact a method made available in the Blocks API to do this.

At the top of your block code, type:

const { decodeEntities } = wp.htmlEntities;

Then you can use it like this:

const options = response.data.map((post) => {
    return {
        label: decodeEntities(post.title.rendered),
        value: post.id,
    };
});

Bazoozaa! HTML entities are gone.


And why not using rest_prepare_<post_type> filter ?

$post_type = "post"
add_filter( "rest_prepare_".$post_type, 'prefix_title_entity_decode'] , 10, 1 ); 
function prefix_title_entity_decode($response){
    $data = $response->get_data();
    $data['title']['rendered'] = html_entity_decode( $data['title']['rendered']);
    $response->set_data($data);
    return $response;
}