goiblas / Map-Block-Leaflet

Map Block Leaflet for WordPress 🗺️
https://wordpress.org/plugins/map-block-leaflet
GNU General Public License v2.0
17 stars 10 forks source link

map_block_leaflet_render() disabled? #52

Closed Seriousness closed 1 year ago

Seriousness commented 1 year ago

It seems that "map_block_leaflet_render()" is removed, is there a replacement? I have used it to fill in a custom map by code from custom meta.

Thanks for all the work.

goiblas commented 1 year ago

Now, I'm using the render method of block.json

Maybe you can use the render_block filter https://developer.wordpress.org/reference/hooks/render_block/

Seriousness commented 1 year ago

First of all: Thank you!

I'll give it another go as soon as the plugin is updated from my other issue.

my try on this:

echo render_block(array(
                'blockName' => 'map-block-leaflet/map-block-leaflet',
                'attrs' => $mapmeta['attributes']
            ));

my $mapmeta looks like this, the $map vars are from the metabox customfield:

            'attributes' => [
                'lat' => [
                    'type' => 'number',
                    'default' => $map['latitude']
                ],
                'lng'  => [
                    'type'  => 'number',
                    'default' => $map['longitude']
                ],
                'themeUrl' => [
                    'type' => 'string',
                    'default' =>  'https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png'
                ],
                'themeAttribution' => [
                    'type' => 'string',
                    'default' => '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <a href="https://carto.com/attributions">CARTO</a>'
                ],
                'height' => [
                    'type' => 'number',
                    'default' => 220
                ],
                'disableScrollZoom'=> [
                    'type' => 'boolean',
                    'default' => true
                ],
                'zoom' => [
                    'type' => 'number',
                    'default' => $map['zoom']
                ],
                'themeId' => [
                    'type' => 'number',
                    'default' => 1
                ],
                'content' => [
                    'type' => 'string',
                    'default' => ''
                ]
            ] );
goiblas commented 1 year ago

I have done some testing and it seems that the render_block_data filter can help you for this

function update_map_block_leaflet_attrs($block) {
    if ( $block['blockName'] === 'map-block-leaflet/map-block-leaflet' ) {
                $block['attrs']['lat'] = 40.416775;
        $block['attrs']['lng'] = -3.703790;
    }
    return $block;
}

add_filter( 'render_block_data', 'update_map_block_leaflet_attrs' );

I hope to help you