Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make changes for render method in Carbon_Fields\Container\Block_Container.php #1244

Open
htmldiz opened this issue Jul 28, 2024 · 1 comment

Comments

@htmldiz
Copy link
Contributor

htmldiz commented Jul 28, 2024

Version

  • Carbon Fields: ^3.6
  • WordPress:6.6.1
  • PHP:7.4

Expected Behavior

Expected $block variable in the function render_block

Actual Behavior

I tried to get post id in Gutenberg block Query loop. And found that usesContext are available only inside block.
Than I font that Carbon fields don't have $block variable inside render_callback method.

Before Changes

	public function render_block( $attributes, $content ) {
		$fields = $attributes['data'];
		$post_id = $this->get_post_id();
		// Unset the "data" property because we
		// pass it as separate argument to the callback.
		unset($attributes['data']);
		ob_start();
		call_user_func( $this->render_callback , $fields, $attributes, $content, $post_id );
		return ob_get_clean();
	}

After cahnges:

	public function render_block( $attributes, $content, \WP_Block $block = null ) { // added variable at the end 
		$fields = $attributes['data'];
		$post_id = $this->get_post_id();
		// Unset the "data" property because we
		// pass it as separate argument to the callback.
		unset($attributes['data']);
		ob_start();
		call_user_func( $this->render_callback , $fields, $attributes, $content, $post_id, $block ); // added variable at the end 
		$toReturn = ob_get_contents();
		ob_clean(); // changed way to get content from object otherwise $block is null
		return $toReturn;
	}
// are easy to reproduce

Steps to Reproduce the Problem

I tried to get post id in Gutenberg block Query loop. And found that usesContext are available only inside block.
Than I font that Carbon fields don't have $block variable inside render_callback method.

Comments

Please add any other comments here

@htmldiz
Copy link
Contributor Author

htmldiz commented Jul 28, 2024

You can reed more about it here https://developer.wordpress.org/news/2023/03/03/creating-a-custom-block-that-stores-post-meta/
Start in section: Adding usesContext to block.json

htmldiz added a commit to htmldiz/carbon-fields-plus that referenced this issue Jul 29, 2024
The issues missing Block variable: htmlburger#1244
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant