Skip to content
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

Introducing 'preview_enabled' on the next_entity_type_config entity #660

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -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'
Expand Down
23 changes: 23 additions & 0 deletions modules/next/next.install
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -44,6 +44,7 @@
* "id",
* "site_resolver",
* "configuration",
* "preview_enabled",
* "revalidator",
* "revalidator_configuration"
* },
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -129,6 +137,13 @@ public function setSiteResolver(string $plugin_id): NextEntityTypeConfigInterfac
return $this;
}

/**
* {@inheritdoc}
*/
public function isPreviewEnabled(): bool {
return $this->preview_enabled;
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions modules/next/src/Entity/NextEntityTypeConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
70 changes: 47 additions & 23 deletions modules/next/src/Form/NextEntityTypeConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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">',
Expand All @@ -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.'),
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/next/src/Render/MainContent/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
30 changes: 30 additions & 0 deletions modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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' => [
Expand All @@ -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());
}

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

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

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

$entity_type_config = NextEntityTypeConfig::create([
'id' => 'node.page',
'preview_enabled' => TRUE,
'site_resolver' => 'site_selector',
'configuration' => [
'sites' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
1 change: 1 addition & 0 deletions modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -63,16 +70,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();
}
Expand Down Expand Up @@ -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);
}

}