MK
(@mkarimzada)
Hi there,
You can get parent’s ID from child element and access the attributes via select hook.
For example:
import { select } from "@wordpress/data";
const parentClientId = select( 'core/block-editor' ).getBlockHierarchyRootClientId( this.props.clientId );
const parentAttributes = select('core/block-editor').getBlockAttributes( parentClientId );
I hope this helps.
Hi Murtaza,
I need this done in PHP and not in JS.
So basically inside the referenced function here: https://github.com/WordPress/gutenberg/blob/trunk/docs/how-to-guides/block-tutorial/creating-dynamic-blocks.md
<?php
/**
* Plugin Name: Gutenberg examples dynamic
*/
function gutenberg_examples_dynamic_render_callback( $block_attributes, $content ) {
-
This reply was modified 2 years, 8 months ago by
landwire.
MK
(@mkarimzada)
Hi Sascha,
As you mentioned earlier no Javascript, but I think this is a lot easier to be done via Javascript. The correct dynamic solution would be creating a REST endpoint and calling it on save() using JS while passing child/parent attributes. Then inside your PHP register callback access the same REST endpoint and check the column width.
Another method would be using WP_Block_Type_Registry.
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
foreach ( $block_type->providesContext as $attribute_name ) {}
Also, you could parse the blocks and get the attributes – Not sure if this works in your callback and I’ve not tested this yet but it would look something like this:
global $post;
$blocks = parse_blocks( $post->post_content );
foreach( $blocks as $block ) {
// now you can access attributes via $block['attrs']
}
I hope this helps.
Ok, I will try some of your suggestions. Not sure I understand all of it 100%, but I will give it a shot. As I do not have any experience in creating REST endpoints, so I will leave this to last.
To be quite honest I was hoping for something simple like this in PHP. That’s how simple it should be in my opinion:
$parent_block = get_parent_block();
$parent_blocks = get_parent_blocks();