diff --git a/modules/next/config/schema/next.schema.yml b/modules/next/config/schema/next.schema.yml
index bde59648..5baafbfa 100644
--- a/modules/next/config/schema/next.schema.yml
+++ b/modules/next/config/schema/next.schema.yml
@@ -36,6 +36,9 @@ next.next_entity_type_config.*:
label: 'Site resolver'
configuration:
type: next.site_resolver.configuration.[%parent.site_resolver]
+ preview_enabled:
+ type: boolean
+ label: 'Preview mode enabled'
revalidator:
type: string
label: 'Revalidator'
diff --git a/modules/next/next.install b/modules/next/next.install
index 0afb4e60..2c711b96 100644
--- a/modules/next/next.install
+++ b/modules/next/next.install
@@ -104,3 +104,26 @@ function next_update_9106() {
$config->set('debug', FALSE)
->save();
}
+
+/**
+ * Add the preview_enabled property to the next_entity_type_config.
+ */
+function next_update_9107() {
+ $entity_definition_update = \Drupal::entityDefinitionUpdateManager();
+
+ $storage_definition = BaseFieldDefinition::create('string')
+ ->setLabel(t('Preview mode enabled'));
+ $entity_definition_update->installFieldStorageDefinition('preview_enabled', 'next_entity_type_config', 'next_entity_type_config', $storage_definition);
+}
+
+/**
+ * Enable preview mode on all Next.js entity types.
+ */
+function next_update_9108() {
+ /** @var \Drupal\next\Entity\NextEntityTypeConfigInterface[] $next_entity_type_configs */
+ $next_entity_type_configs = \Drupal::entityTypeManager()->getStorage('next_entity_type_config')->loadMultiple();
+ foreach ($next_entity_type_configs as $next_entity_type_config) {
+ $next_entity_type_config->set('preview_enabled', TRUE);
+ $next_entity_type_config->save();
+ }
+}
diff --git a/modules/next/src/Entity/NextEntityTypeConfig.php b/modules/next/src/Entity/NextEntityTypeConfig.php
index c6284c17..9160f446 100644
--- a/modules/next/src/Entity/NextEntityTypeConfig.php
+++ b/modules/next/src/Entity/NextEntityTypeConfig.php
@@ -44,6 +44,7 @@
* "id",
* "site_resolver",
* "configuration",
+ * "preview_enabled",
* "revalidator",
* "revalidator_configuration"
* },
@@ -78,6 +79,13 @@ class NextEntityTypeConfig extends ConfigEntityBase implements NextEntityTypeCon
*/
protected $configuration = [];
+ /**
+ * Whether the preview mode is enabled.
+ *
+ * @var bool
+ */
+ protected $preview_enabled = FALSE;
+
/**
* The revalidator.
*
@@ -129,6 +137,13 @@ public function setSiteResolver(string $plugin_id): NextEntityTypeConfigInterfac
return $this;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function isPreviewEnabled(): bool {
+ return $this->preview_enabled;
+ }
+
/**
* {@inheritdoc}
*/
diff --git a/modules/next/src/Entity/NextEntityTypeConfigInterface.php b/modules/next/src/Entity/NextEntityTypeConfigInterface.php
index 213c31c2..5c4e211f 100644
--- a/modules/next/src/Entity/NextEntityTypeConfigInterface.php
+++ b/modules/next/src/Entity/NextEntityTypeConfigInterface.php
@@ -31,6 +31,14 @@ public function getSiteResolver(): ?SiteResolverInterface;
*/
public function setSiteResolver(string $plugin_id): self;
+ /**
+ * Checks if the preview mode is enabled.
+ *
+ * @return bool
+ * Return true/false if preview mode is enabled.
+ */
+ public function isPreviewEnabled(): bool;
+
/**
* Returns the revalidator plugin.
*
diff --git a/modules/next/src/Form/NextEntityTypeConfigForm.php b/modules/next/src/Form/NextEntityTypeConfigForm.php
index 37d2e65b..579f99e5 100644
--- a/modules/next/src/Form/NextEntityTypeConfigForm.php
+++ b/modules/next/src/Form/NextEntityTypeConfigForm.php
@@ -107,6 +107,36 @@ public function form(array $form, FormStateInterface $form_state) {
],
];
+ $form['site_resolver'] = [
+ '#title' => $this->t('Plugin'),
+ '#description' => $this->t('Select a plugin to use when validating the draft url for this entity type.'),
+ '#type' => 'select',
+ '#options' => array_column($this->siteResolverManager->getDefinitions(), 'label', 'id'),
+ '#default_value' => $entity->getSiteResolver() ? $entity->getSiteResolver()->getId() : NULL,
+ '#required' => TRUE,
+ '#limit_validation_errors' => [['site_resolver']],
+ '#submit' => ['::submitSiteResolver'],
+ '#executes_submit_callback' => TRUE,
+ '#ajax' => [
+ 'callback' => '::ajaxReplaceSiteResolverSettingsForm',
+ 'wrapper' => 'site-resolver-settings',
+ 'method' => 'replace',
+ ],
+ ];
+
+ $form['site_resolver_settings_container'] = [
+ '#type' => 'container',
+ '#prefix' => '
',
@@ -126,35 +156,21 @@ public function form(array $form, FormStateInterface $form_state) {
'#group' => 'settings',
];
- $form['draft_mode']['site_resolver'] = [
- '#title' => $this->t('Plugin'),
- '#description' => $this->t('Select a plugin to use when validating the draft url for this entity type.'),
- '#type' => 'select',
- '#options' => array_merge(['' => $this->t('None')], array_column($this->siteResolverManager->getDefinitions(), 'label', 'id')),
- '#default_value' => $entity->getSiteResolver() ? $entity->getSiteResolver()->getId() : NULL,
- '#limit_validation_errors' => [['site_resolver']],
- '#submit' => ['::submitSiteResolver'],
+ $form['draft_mode']['preview_enabled'] = [
+ '#title' => $this->t('Enabled'),
+ '#description' => $this->t('Enable draft mode.'),
+ '#type' => 'checkbox',
+ '#default_value' => $entity->isPreviewEnabled(),
+ '#limit_validation_errors' => [['preview_enabled']],
+ '#submit' => ['::submitPreviewEnabled'],
'#executes_submit_callback' => TRUE,
'#ajax' => [
- 'callback' => '::ajaxReplaceSiteResolverSettingsForm',
- 'wrapper' => 'site-resolver-settings',
+ 'callback' => '::ajaxReplaceSettingsForm',
+ 'wrapper' => 'settings-container',
'method' => 'replace',
],
];
- $form['draft_mode']['site_resolver_settings_container'] = [
- '#type' => 'container',
- '#prefix' => '
',
- '#suffix' => '
',
- ];
-
- $site_resolver = $entity->getSiteResolver();
- if ($site_resolver instanceof ConfigurableSiteResolverInterface) {
- $form['configuration'] = [];
- $subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state);
- $form['draft_mode']['site_resolver_settings_container']['configuration'] = $site_resolver->buildConfigurationForm($form['configuration'], $subform_state);
- }
-
$form['revalidation'] = [
'#title' => $this->t('On-demand Revalidation'),
'#description' => $this->t('Configure on-demand revalidation for the entity type.'),
@@ -204,6 +220,14 @@ public function submitId(array $form, FormStateInterface $form_state) {
$form_state->setRebuild();
}
+ /**
+ * Handles submit call when preview mode is selected.
+ */
+ public function submitPreviewEnabled(array $form, FormStateInterface $form_state) {
+ $this->entity = $this->buildEntity($form, $form_state);
+ $form_state->setRebuild();
+ }
+
/**
* Handles switching the id selector.
*/
diff --git a/modules/next/src/Render/MainContent/HtmlRenderer.php b/modules/next/src/Render/MainContent/HtmlRenderer.php
index b60a2036..a81e4e61 100644
--- a/modules/next/src/Render/MainContent/HtmlRenderer.php
+++ b/modules/next/src/Render/MainContent/HtmlRenderer.php
@@ -108,7 +108,7 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
}
$next_entity_type_config = $this->nextEntityTypeManager->getConfigForEntityType($entity->getEntityTypeId(), $entity->bundle());
- if (!$next_entity_type_config) {
+ if (!$next_entity_type_config || !$next_entity_type_config->isPreviewEnabled()) {
return $build;
}
diff --git a/modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php b/modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php
index ad134adb..6b98da5e 100644
--- a/modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php
+++ b/modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php
@@ -60,6 +60,7 @@ public function testSiteResolver() {
/** @var \Drupal\next\Entity\NextEntityTypeConfigInterface $entity_type_config */
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => FALSE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -124,6 +125,7 @@ public function testRevalidator() {
/** @var \Drupal\next\Entity\NextEntityTypeConfigInterface $entity_type_config */
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => FALSE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -140,4 +142,32 @@ public function testRevalidator() {
$this->assertSame('path', $revalidator->getId());
}
+ /**
+ * Tests the preview enabled property.
+ *
+ * @covers ::isPreviewEnabled
+ */
+ public function testPreviewEnabled() {
+ $blog_site = NextSite::create(['id' => 'blog']);
+ $blog_site->save();
+
+ // Create entity type config.
+ /** @var \Drupal\next\Entity\NextEntityTypeConfigInterface $entity_type_config */
+ $entity_type_config = NextEntityTypeConfig::create([
+ 'id' => 'node.page',
+ 'preview_enabled' => TRUE,
+ 'site_resolver' => 'site_selector',
+ 'configuration' => [
+ 'sites' => [
+ 'blog' => 'blog',
+ ],
+ ],
+ ]);
+ $entity_type_config->save();
+ $this->assertTrue($entity_type_config->isPreviewEnabled());
+
+ $entity_type_config->set('preview_enabled', FALSE)->save();
+ $this->assertFalse($entity_type_config->isPreviewEnabled());
+ }
+
}
diff --git a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php
index e8c69d07..fcab1f44 100644
--- a/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php
+++ b/modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php
@@ -50,6 +50,7 @@ protected function setUp(): void {
// Create entity type config.
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
diff --git a/modules/next/tests/src/Kernel/NextEntityTypeManagerTest.php b/modules/next/tests/src/Kernel/NextEntityTypeManagerTest.php
index f3df8a0a..8b17fd19 100644
--- a/modules/next/tests/src/Kernel/NextEntityTypeManagerTest.php
+++ b/modules/next/tests/src/Kernel/NextEntityTypeManagerTest.php
@@ -53,6 +53,7 @@ public function testGetSitesForEntity() {
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -98,6 +99,7 @@ public function testGetSiteResolver() {
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -121,6 +123,7 @@ public function testGetRevalidator() {
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
diff --git a/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php b/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php
index a7de9793..45ff192e 100644
--- a/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php
+++ b/modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php
@@ -67,6 +67,7 @@ public function testRevalidate() {
// Create entity type config.
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
diff --git a/modules/next/tests/src/Kernel/Plugin/SimpleOauthPreviewUrlGeneratorTest.php b/modules/next/tests/src/Kernel/Plugin/SimpleOauthPreviewUrlGeneratorTest.php
index 9afc524a..0216aab9 100644
--- a/modules/next/tests/src/Kernel/Plugin/SimpleOauthPreviewUrlGeneratorTest.php
+++ b/modules/next/tests/src/Kernel/Plugin/SimpleOauthPreviewUrlGeneratorTest.php
@@ -68,6 +68,7 @@ protected function setUp(): void {
// Create entity type config.
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
diff --git a/modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php b/modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php
index 0cd090fd..4c57af5a 100644
--- a/modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php
+++ b/modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php
@@ -71,6 +71,7 @@ protected function setUp(): void {
// Create entity type config.
$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
diff --git a/modules/next/tests/src/Kernel/Renderer/MainContent/HtmlRendererTest.php b/modules/next/tests/src/Kernel/Renderer/MainContent/HtmlRendererTest.php
index 7eaaacd2..12c31412 100644
--- a/modules/next/tests/src/Kernel/Renderer/MainContent/HtmlRendererTest.php
+++ b/modules/next/tests/src/Kernel/Renderer/MainContent/HtmlRendererTest.php
@@ -26,6 +26,13 @@ class HtmlRendererTest extends KernelTestBase {
*/
protected static $modules = ['filter', 'next', 'node', 'system', 'user'];
+ /**
+ * The next entity type config.
+ *
+ * @var \Drupal\next\Entity\NextEntityTypeConfigInterface
+ */
+ protected $entityTypeConfig;
+
/**
* {@inheritdoc}
*/
@@ -63,8 +70,9 @@ protected function setUp(): void {
$blog->save();
// Create entity type config.
- $entity_type_config = NextEntityTypeConfig::create([
+ $this->entityTypeConfig = NextEntityTypeConfig::create([
'id' => 'node.page',
+ 'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -72,7 +80,7 @@ protected function setUp(): void {
],
],
]);
- $entity_type_config->save();
+ $this->entityTypeConfig->save();
$this->setUpCurrentUser();
}
@@ -100,6 +108,16 @@ public function testPrepare() {
$preview_url = 'https://blog.com/node/2';
$fields = $this->xpath("//iframe[contains(@src, '$preview_url')]");
$this->assertEmpty($fields);
+
+ // Disable preview.
+ $this->entityTypeConfig->set('preview_enabled', FALSE);
+ $this->entityTypeConfig->save();
+ $request = Request::create($page->toUrl()->toString(), 'GET');
+ $response = $this->container->get('http_kernel')->handle($request);
+ $this->setRawContent($response->getContent());
+
+ $fields = $this->xpath("//iframe");
+ $this->assertEmpty($fields);
}
}