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

How to display Additional CSS Class (Gutenberg) #642

Closed
ESirop opened this issue Jan 15, 2019 · 6 comments
Closed

How to display Additional CSS Class (Gutenberg) #642

ESirop opened this issue Jan 15, 2019 · 6 comments
Labels
good first issue Feel free to contribute a PR to resolve issues like this one. [type] bug

Comments

@ESirop
Copy link

ESirop commented Jan 15, 2019

Hello,

I'm using CF to put create new blocks on Gutenberg, and we still have the possibility to add an Additional CSS Class. Sadly, I can't find how to display this class (documentation or Google).

As a back up plan, I can make a new text field which would be the additial class, but it means I'll have to use it on every block and I don't really want to display an additional field for each block (client won't necesserely understand what it's for, etc.)

Any ideas?

Thanks

P.S : still in love with the plugin since 3 years <3

@ESirop ESirop changed the title How to display the Additional CSS Class (Gutenberg) How to display Additional CSS Class (Gutenberg) Jan 15, 2019
@vvasilev-
Copy link
Collaborator

Hi @ESirop,

Thanks for your feedback. Perhaps, we missed to pass the additional attributes of the block to the render callback. It'll be fixed in next minor version. :)

@vvasilev- vvasilev- added [type] bug good first issue Feel free to contribute a PR to resolve issues like this one. labels Jan 15, 2019
@dmtrmrv
Copy link

dmtrmrv commented Feb 13, 2019

Any updates on this? Thank you.

@vvasilev-
Copy link
Collaborator

Hi @ESirop, @dmtrmrv,

Sorry for the late response. This feature is now available in our latest release - v3.1.0-beta.1. 😄
You can get the CSS class from $attributes argument passed to the render callback.

@drdogbot7
Copy link

Is there an example of this? I'm just getting an empty array when I try to access the $attributes variable.

@yunusga
Copy link

yunusga commented Oct 12, 2021

@drdogbot7 Same problem

@saad-allah
Copy link

@drdogbot7 @yunusga u can try this

<?php

use Carbon_Fields\Block;
use Carbon_Fields\Field;

Block::make( __( 'My Shiny Gutenberg Block' ) )
	->add_fields( array(
		Field::make( 'text', 'heading', __( 'Block Heading' ) ),
		Field::make( 'image', 'image', __( 'Block Image' ) ),
		Field::make( 'rich_text', 'content', __( 'Block Content' ) ),
	) )
	->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {

   $additional_class  = isset($attributes['className']) ? $attributes['className'] : '';
		?>

		<div class="block <?php echo $additional_class; ?>">
			<div class="block__heading">
				<h1><?php echo esc_html( $fields['heading'] ); ?></h1>
			</div><!-- /.block__heading -->

			<div class="block__image">
				<?php echo wp_get_attachment_image( $fields['image'], 'full' ); ?>
			</div><!-- /.block__image -->

			<div class="block__content">
				<?php echo apply_filters( 'the_content', $fields['content'] ); ?>
			</div><!-- /.block__content -->
		</div><!-- /.block -->

		<?php
	} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Feel free to contribute a PR to resolve issues like this one. [type] bug
Projects
None yet
Development

No branches or pull requests

6 participants