BlockRendererV2
This class inherits the methods and behavior of AbstractBlockRenderer.
Registration of the block is done by register_block() that locates the block.json file and registers the block with WordPress . In addition to AbstractBlockRenderer this extended class adds the following:
- change_metadata: This method is called by the acf filter block_type_metadata.
- add_block_variations: This method is called by the change_metadata method.
- add_icon: : This method is called by the change_metadata method.
Overview
This class extends HighGround\Bulldozer\AbstractBlockRenderer
Methods
| Name | Return Type | Summary/Returns |
|---|---|---|
| add_block_variations() | array or false | Register the block variants. |
| add_class() | void | Add class to block classes. |
| add_css() | string | Add css to the compiled css. Returns: the compiled css |
| add_css_var() | Add css variable with the value based on an acf field. | |
| add_icon() | Empty function that can be overwritten by the blocks to add a custom icon. | |
| add_modifier_class() | Add modifier class to block classes. | |
| add_notification() | Compose a notification to be shown in the backend. | |
| create_inner_blocks() | string | Generate inner blocks appender. Returns: $inner_blocks the inner blocks appender |
| get_attribute() | mixed | Get block attribute. Returns: the attribute value |
| get_block_alignment() | string | Get the block alignment. |
| get_block_id() | string | Get the block id. |
| get_field() | mixed | get ACF field value. Returns: $field the field value |
| get_post_id() | string | Get the block alignment. |
| hide_from_inserter() | Empty function that can be overwritten by the blocks to add custom logic to hide the block from the inserter. | |
| is_full_width() | bool | Check if the block is full width. |
| is_preview() | bool | Check if the block is rendered in preview mode. |
| is_wide_width() | bool | Check if the block is wide width. |
| register_requirements() | Whether the block meets the requirements and should be registered. | |
| set_alignment() | void | Set the block alignment. |
| set_anchor() | void | Set the block anchor. |
| set_attribute() | void | Set a block attribute. |
| set_disabled() | void | Mark the renderer's block as disabled. |
Class Methods
add_block_variations()
Register the block variants.
see https://www.advancedcustomfields.com/blog/acf-5-9-introducing-block-variations/
Returns: array|false
PHP
public function register_requirements(): bool
{
return class_exists('RankMath\Helper', false);
}
add_class()
Add class to block classes.
When an array is passed, it will merge the array with the existing classes.
since 5.2.0
add_class( string|array $class )
Returns: void
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $class | string or array | the class or array of classes |
PHP
public function block_context($context): array
{
$this->add_class(['section', 'has-background']);
return $context;
}
add_css()
Add css to the compiled css.
since 5.5.1
add_css( string $css )
Returns: string the compiled css
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $css | string | the css to add |
add_css_var()
Add css variable with the value based on an acf field.
since 1.8.0
add_css_var( string $field_name, string $css_var_name, false|string $selector = false )
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $field_name | string | acf field name |
| $css_var_name | string | the css variable without the -- prefix |
| $selector | false or string | the css selector where the css variable should be applied |
PHP
public function block_context($context): array
{
$this->add_css_var('color_card_bg', 'card-base-background-color', '.crd');
return $context;
}
add_icon()
Empty function that can be overwritten by the blocks to add a custom icon.
PHP
public function add_icon(): string|false
{
return '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-grid-3x3-gap-fill" viewBox="0 0 16 16"><path d="M1 2a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2zm5 0a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V2zm5 0a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1h-2a1 1 0 0 1-1-1V2zM1 7a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V7zm5 0a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V7zm5 0a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1h-2a1 1 0 0 1-1-1V7zM1 12a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1v-2zm5 0a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1v-2zm5 0a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1h-2a1 1 0 0 1-1-1v-2z"/></svg>';
}
add_modifier_class()
Add modifier class to block classes.
add_modifier_class( string $modifier )
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $modifier | string | the part after the -- from the BEM principle |
PHP
public function block_context($context): array
{
if ($this->get_field('is_featured')) {
$this->add_modifier_class('featured');
}
return $context;
}
add_notification()
Compose a notification to be shown in the backend.
add_notification( string $message, string $type )
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $message | string | the message, translatable |
| $type | string | type of notification, can be notice, warning or error |
PHP
public function block_context($context): array
{
$posts = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 3,
])->to_array();
if (empty($posts)) {
$this->add_notification(__('Please add some posts.', 'wp-lemon-child'), 'warning');
}
create_inner_blocks()
Generate inner blocks appender.
since 3.3.0
create_inner_blocks( array|false $allowed_blocks = false, array|false $template = false, false|string $classes = false, false|string $orientation = false, bool|string $templatelock = false )
Returns: string $inner_blocks the inner blocks appender
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $allowed_blocks | array or false | array with allowed blocks or false |
| $template | array or false | array with template |
| $classes | false or string | string with classes |
| $orientation | false or string | string with orientation, can be 'horizontal' or 'vertical' |
| $templatelock | bool or string | true or one of 'all' or 'insert'. True defaults to 'all'. |
PHP
public function block_context($context): array
{
$args = [
'InnerBlocks' => self::create_inner_blocks($allowed_blocks, $template, 'row archive-content', 'horizontal'),
];
return array_merge($context, $args);
}
get_attribute()
Get block attribute.
since 5.5.1
get_attribute( string $attribute_name )
Returns: mixed the attribute value
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $attribute_name | string | the attribute name |
PHP
public function block_context($context): array
{
$align = $this->get_attribute('align');
return $context;
}
get_block_alignment()
Get the block alignment.
since 5.5.1
Returns: string
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
{
$alignment = $this->get_block_alignment();
return $context;
}
get_block_id()
Get the block id.
since 5.5.1
Returns: string
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
{
$block_id = $this->get_block_id();
return $context;
}
get_field()
get ACF field value.
since 5.2.0
get_field( string $field_name )
Returns: mixed $field the field value
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $field_name | string | the field name |
PHP
public function block_context($context): array
{
$is_featured = $this->get_field('is_featured');
return $context;
}
get_post_id()
Get the block alignment.
since 5.9.1
Returns: string
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
{
$post_id = $this->get_post_id();
return $context;
}
hide_from_inserter()
Empty function that can be overwritten by the blocks to add custom logic to hide the block from the inserter.
PHP
public function hide_from_inserter(): bool
{
$env = get_constant('WP_ENV');
return $env === 'production';
}
is_full_width()
Check if the block is full width.
since 5.5.1
Returns: bool
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
{
if ($this->is_full_width()) {
$this->add_class('has-background');
}
return $context;
}
is_preview()
Check if the block is rendered in preview mode.
This is true when the block is rendered in the backend.
Use this method to conditionally load assets or change the rendering.
since 5.5.1
Returns: bool
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
{
if ($this->is_preview()) {
$this->add_notification(__('This is a preview mode.', 'wp-lemon-child'), 'notice');
}
return $context;
}
is_wide_width()
Check if the block is wide width.
since 5.5.1
Returns: bool
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
{
if ($this->is_wide_width()) {
$this->add_class('has-background');
}
return $context;
}
register_requirements()
Whether the block meets the requirements and should be registered.
This method can be overwritten by the block to add requirements on a per block basis.
PHP
public function register_requirements(): bool
{
return class_exists('RankMath\Helper', false);
}
set_alignment()
Set the block alignment.
since 5.5.1
set_alignment( string $alignment )
Returns: void
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $alignment | string | the alignment, can be wide or full |
PHP
public function block_context($context): array
{
$this->set_alignment('full');
return $context;
}
set_anchor()
Set the block anchor.
since 5.5.1
set_anchor( string $value )
Returns: void
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $value | string | the anchor value |
PHP
public function block_context($context): array
{
$this->set_anchor('my-custom-anchor');
return $context;
}
set_attribute()
Set a block attribute.
since 5.5.1
set_attribute( string $attribute_name, mixed $value )
Returns: void
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
| Name | Type | Description |
|---|---|---|
| $attribute_name | string | the attribute name |
| $value | mixed | the value to set |
PHP
public function block_context($context): array
{
$this->set_attribute('align', 'full');
return $context;
}
set_disabled()
Mark the renderer's block as disabled.
Sets the internal flag indicating the block is disabled so that subsequent rendering logic can treat this block as inactive or skip its output.
since 5.5.1
Returns: void
This method is inherited from \HighGround\Bulldozer\AbstractBlockRenderer.
PHP
public function block_context($context): array
if (empty($this->get_field('gallery'))) {
$this->add_notification(__('Add images to the slider', 'wp-lemon-child'), 'warning');
$this->set_disabled();
return $context;
}
return $context;
}