By clayfreeman on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
9.4.x
Introduced in version:
9.4.0
Issue links:
Description:
A new interface, PreviewAwarePluginInterface, has been added for block and layout plugins to declare support for preview mode injection. This feature is only used when rendering layouts or blocks with Layout Builder at this time.
Block plugins extending BlockBase and layout plugins extending LayoutDefault support this new functionality out of the box. If your code does not extend these base classes, you will need to declare support for preview mode injection by manually implementing the new interface.
Plugins extending the above base classes can detect when preview mode is being used by checking $this->inPreview as shown below:
Block plugins extending BlockBase
/**
* An example block that uses preview mode detection.
*
* @Block(
* id = "preview_aware_block",
* admin_label = "Preview-aware block",
* )
*/
class PreviewAwareBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$markup = $this->t('This block is being rendered normally.');
if ($this->inPreview) {
$markup = $this->t('This block is being rendered in preview mode.');
}
return [
'#markup' => $markup,
];
}
}
Layout plugins extending LayoutDefault
/**
* An example layout that uses preview mode detection.
*
* @Layout(
* id = "preview_aware_layout",
* label = @Translation("Preview-aware layout"),
* regions = {
* "main" = {
* "label" = @Translation("Main Region")
* }
* },
* )
*/
class PreviewAwareLayout extends LayoutDefault {
/**
* {@inheritdoc}
*/
public function build(array $regions) {
$build = parent::build($regions);
if ($this->inPreview) {
$build['main']['#attributes']['class'][] = 'layout-preview';
}
return $build;
}
}
Impacts:
Module developers