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

Can't retrive my block's attribute when using server-side rendering #4117

Closed
sycatsce opened this issue Dec 21, 2017 · 2 comments
Closed

Can't retrive my block's attribute when using server-side rendering #4117

sycatsce opened this issue Dec 21, 2017 · 2 comments

Comments

@sycatsce
Copy link

Issue Overview

The $attributes parameter of my block server-side rendering function keeps returning me an empty array, even after saving the block in the editor.

Wordpress : v4.9.1
Gutenberg : v1.9.1

Expected Behavior

I expected to retrieve my block's attributes from the $attributes parameter.

Current Behavior

The $attributes parameter returns an empty array

Possible Solution

It looks like the callback function is called before the block is actually saved in the editor.

Code

block.js

var el = wp.element.createElement;
var registerBlockType = wp.blocks.registerBlockType;
var Editable = wp.blocks.Editable;

registerBlockType( 'some/block', {
	title: 'Header',
	icon: 'universal-access-alt',
	category: 'layout',
	attributes:
	{
		title: {
			type: 'array',
			source: 'children',
			selector: '.parTitle'
		},
		test: { value : "17" }
	},

	edit: function( props ) {
		
		return ( 
			el('div', { className:  props.className },
				el('h2', {}, 'Title'),
				el( Editable,
					{
						tagName: 'div',
						className: 'parTitle',
						placeholder: 'Write title here...',
						value: props.attributes.title,
						onChange: function( value ){ props.setAttributes( { title: value } )},
				        })
			)
		)
	},
	save: function( props ){
		return null;
	}
})

index.php

function gb_block_header_render( $attributes ) {
	return count($attributes); 
}

register_block_type( 'some/block', array (
	'render_callback' => 'gb_block_header_render',
));
@youknowriad
Copy link
Contributor

The attributes serialized into the block's content are not parsed server-side. Only the comments attributes are. There are some explorations about moving all the parsing server-side. I'm closing this as duplicate fo #2751

@ngdangtu-vn
Copy link

Only the comments attributes are

I don't understand what do you mean about the "comments attributes". But I fix my problem by make sure do not use source & selector in each attribute. Seems like what you said, any attribute with source & selector will be serialized to post_content. So the best way if anyone needs the block to be render from server side is using only type, default. (I haven't tried enum so I'm not sure)

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

3 participants