Skip to content

Introducing 'preview_enabled' on the next_entity_type_config entity #660

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/next/config/schema/next.schema.yml
Original file line number Diff line number Diff line change
@@ -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'
23 changes: 23 additions & 0 deletions modules/next/next.install
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 15 additions & 0 deletions modules/next/src/Entity/NextEntityTypeConfig.php
Original file line number Diff line number Diff line change
@@ -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}
*/
8 changes: 8 additions & 0 deletions modules/next/src/Entity/NextEntityTypeConfigInterface.php
Original file line number Diff line number Diff line change
@@ -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.
*
70 changes: 47 additions & 23 deletions modules/next/src/Form/NextEntityTypeConfigForm.php
Original file line number Diff line number Diff line change
@@ -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' => '<div id="site-resolver-settings">',
'#suffix' => '</div>',
];

$site_resolver = $entity->getSiteResolver();
if ($site_resolver instanceof ConfigurableSiteResolverInterface) {
$form['configuration'] = [];
$subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state);
$form['site_resolver_settings_container']['configuration'] = $site_resolver->buildConfigurationForm($form['configuration'], $subform_state);
}

$form['settings_container'] = [
'#type' => 'container',
'#prefix' => '<div id="settings-container">',
@@ -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' => '<div id="site-resolver-settings">',
'#suffix' => '</div>',
];

$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.
*/
2 changes: 1 addition & 1 deletion modules/next/src/Render/MainContent/HtmlRenderer.php
Original file line number Diff line number Diff line change
@@ -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;
}

30 changes: 30 additions & 0 deletions modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php
Original file line number Diff line number Diff line change
@@ -59,6 +59,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' => [
@@ -123,6 +124,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' => [
@@ -139,4 +141,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());
}

}
Original file line number Diff line number Diff line change
@@ -48,6 +48,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' => [
3 changes: 3 additions & 0 deletions modules/next/tests/src/Kernel/NextEntityTypeManagerTest.php
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ public function testGetSitesForEntity() {

$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -97,6 +98,7 @@ public function testGetSiteResolver() {

$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
@@ -120,6 +122,7 @@ public function testGetRevalidator() {

$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
Original file line number Diff line number Diff line change
@@ -66,6 +66,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' => [
Original file line number Diff line number Diff line change
@@ -67,6 +67,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' => [
1 change: 1 addition & 0 deletions modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php
Original file line number Diff line number Diff line change
@@ -70,6 +70,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' => [
Original file line number Diff line number Diff line change
@@ -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}
*/
@@ -62,16 +69,17 @@ 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' => [
'blog' => 'blog',
],
],
]);
$entity_type_config->save();
$this->entityTypeConfig->save();

$this->setUpCurrentUser();
}
@@ -99,6 +107,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);
}

}