Unverified Commit eba2a2df authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3027653 by clayfreeman, tim.plunkett, Pavel Ruban, raman.b,...

Issue #3027653 by clayfreeman, tim.plunkett, Pavel Ruban, raman.b, ankithashetty, cboyden, larowlan, mpotter, phenaproxima: Allow block and layout plugins to determine if they are being previewed

(cherry picked from commit 1c4a6d6c)
parent f28a7c05
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Plugin\PreviewAwarePluginInterface;
use Drupal\Core\Render\PreviewFallbackInterface;

/**
@@ -19,7 +20,7 @@
 *
 * @ingroup block_api
 */
abstract class BlockBase extends PluginBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface, ContextAwarePluginInterface {
abstract class BlockBase extends PluginBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewAwarePluginInterface, PreviewFallbackInterface, ContextAwarePluginInterface {

  use BlockPluginTrait {
    buildConfigurationForm as traitBuildConfigurationForm;
+14 −0
Original line number Diff line number Diff line
@@ -30,6 +30,13 @@ trait BlockPluginTrait {
  use MessengerTrait;
  use PluginWithFormsTrait;

  /**
   * Whether the plugin is being rendered in preview mode.
   *
   * @var bool
   */
  protected $inPreview = FALSE;

  /**
   * The transliteration service.
   *
@@ -281,4 +288,11 @@ public function setTransliteration(TransliterationInterface $transliteration) {
    $this->transliteration = $transliteration;
  }

  /**
   * {@inheritdoc}
   */
  public function setInPreview(bool $in_preview): void {
    $this->inPreview = $in_preview;
  }

}
+16 −1
Original line number Diff line number Diff line
@@ -8,15 +8,23 @@
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Plugin\PreviewAwarePluginInterface;

/**
 * Provides a default class for Layout plugins.
 */
class LayoutDefault extends PluginBase implements LayoutInterface, PluginFormInterface {
class LayoutDefault extends PluginBase implements LayoutInterface, PluginFormInterface, PreviewAwarePluginInterface {

  use ContextAwarePluginAssignmentTrait;
  use ContextAwarePluginTrait;

  /**
   * Whether the plugin is being rendered in preview mode.
   *
   * @var bool
   */
  protected $inPreview = FALSE;

  /**
   * The layout definition.
   *
@@ -118,4 +126,11 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
    $this->configuration['label'] = $form_state->getValue('label');
  }

  /**
   * {@inheritdoc}
   */
  public function setInPreview(bool $in_preview): void {
    $this->inPreview = $in_preview;
  }

}
+24 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Plugin;

/**
 * Provides an interface to support preview mode injection in plugins.
 *
 * Block and layout plugins can implement this interface to be informed when
 * preview mode is being used in Layout Builder.
 *
 * @see \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent
 * @see \Drupal\layout_builder\Section::toRenderArray()
 */
interface PreviewAwarePluginInterface {

  /**
   * Set preview mode for the plugin.
   *
   * @param bool $in_preview
   *   TRUE if the plugin should be set to preview mode, FALSE otherwise.
   */
  public function setInPreview(bool $in_preview): void;

}
+5 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\layout_builder\Event;

use Drupal\Core\Cache\CacheableResponseTrait;
use Drupal\Core\Plugin\PreviewAwarePluginInterface;
use Drupal\layout_builder\SectionComponent;
use Drupal\Component\EventDispatcher\Event;

@@ -68,6 +69,10 @@ public function __construct(SectionComponent $component, array $contexts, $in_pr
    $this->contexts = $contexts;
    $this->plugin = $component->getPlugin($contexts);
    $this->inPreview = $in_preview;

    if ($this->plugin instanceof PreviewAwarePluginInterface) {
      $this->plugin->setInPreview($in_preview);
    }
  }

  /**
Loading