Skip to content

Commit f0439ad

Browse files
author
Bojan Bogdanovic
committed
Introducing 'preview_enabled' on the next_entity_type_config entity
1 parent ccb42de commit f0439ad

13 files changed

+155
-27
lines changed

Diff for: modules/next/config/schema/next.schema.yml

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ next.next_entity_type_config.*:
3636
label: 'Site resolver'
3737
configuration:
3838
type: next.site_resolver.configuration.[%parent.site_resolver]
39+
preview_enabled:
40+
type: boolean
41+
label: 'Preview mode enabled'
3942
revalidator:
4043
type: string
4144
label: 'Revalidator'

Diff for: modules/next/next.install

+23
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,26 @@ function next_update_9106() {
104104
$config->set('debug', FALSE)
105105
->save();
106106
}
107+
108+
/**
109+
* Add the preview_enabled property to the next_entity_type_config.
110+
*/
111+
function next_update_9107() {
112+
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
113+
114+
$storage_definition = BaseFieldDefinition::create('string')
115+
->setLabel(t('Preview mode enabled'));
116+
$entity_definition_update->installFieldStorageDefinition('preview_enabled', 'next_entity_type_config', 'next_entity_type_config', $storage_definition);
117+
}
118+
119+
/**
120+
* Enable preview mode on all Next.js entity types.
121+
*/
122+
function next_update_9108() {
123+
/** @var \Drupal\next\Entity\NextEntityTypeConfigInterface[] $next_entity_type_configs */
124+
$next_entity_type_configs = \Drupal::entityTypeManager()->getStorage('next_entity_type_config')->loadMultiple();
125+
foreach ($next_entity_type_configs as $next_entity_type_config) {
126+
$next_entity_type_config->set('preview_enabled', TRUE);
127+
$next_entity_type_config->save();
128+
}
129+
}

Diff for: modules/next/src/Entity/NextEntityTypeConfig.php

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* "id",
4545
* "site_resolver",
4646
* "configuration",
47+
* "preview_enabled",
4748
* "revalidator",
4849
* "revalidator_configuration"
4950
* },
@@ -78,6 +79,13 @@ class NextEntityTypeConfig extends ConfigEntityBase implements NextEntityTypeCon
7879
*/
7980
protected $configuration = [];
8081

82+
/**
83+
* Whether the preview mode is enabled.
84+
*
85+
* @var bool
86+
*/
87+
protected $preview_enabled = FALSE;
88+
8189
/**
8290
* The revalidator.
8391
*
@@ -129,6 +137,13 @@ public function setSiteResolver(string $plugin_id): NextEntityTypeConfigInterfac
129137
return $this;
130138
}
131139

140+
/**
141+
* {@inheritdoc}
142+
*/
143+
public function isPreviewEnabled(): bool {
144+
return $this->preview_enabled;
145+
}
146+
132147
/**
133148
* {@inheritdoc}
134149
*/

Diff for: modules/next/src/Entity/NextEntityTypeConfigInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function getSiteResolver(): ?SiteResolverInterface;
3131
*/
3232
public function setSiteResolver(string $plugin_id): self;
3333

34+
/**
35+
* Checks if the preview mode is enabled.
36+
*
37+
* @return bool
38+
* Return true/false if preview mode is enabled.
39+
*/
40+
public function isPreviewEnabled(): bool;
41+
3442
/**
3543
* Returns the revalidator plugin.
3644
*

Diff for: modules/next/src/Form/NextEntityTypeConfigForm.php

+48-24
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,36 @@ public function form(array $form, FormStateInterface $form_state) {
107107
],
108108
];
109109

110+
$form['site_resolver'] = [
111+
'#title' => $this->t('Plugin'),
112+
'#description' => $this->t('Select a plugin to use for resolving the site for this entity type.'),
113+
'#type' => 'select',
114+
'#options' => array_column($this->siteResolverManager->getDefinitions(), 'label', 'id'),
115+
'#default_value' => $entity->getSiteResolver() ? $entity->getSiteResolver()->getId() : NULL,
116+
'#required' => TRUE,
117+
'#limit_validation_errors' => [['site_resolver']],
118+
'#submit' => ['::submitSiteResolver'],
119+
'#executes_submit_callback' => TRUE,
120+
'#ajax' => [
121+
'callback' => '::ajaxReplaceSiteResolverSettingsForm',
122+
'wrapper' => 'site-resolver-settings',
123+
'method' => 'replace',
124+
],
125+
];
126+
127+
$form['site_resolver_settings_container'] = [
128+
'#type' => 'container',
129+
'#prefix' => '<div id="site-resolver-settings">',
130+
'#suffix' => '</div>',
131+
];
132+
133+
$site_resolver = $entity->getSiteResolver();
134+
if ($site_resolver instanceof ConfigurableSiteResolverInterface) {
135+
$form['configuration'] = [];
136+
$subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state);
137+
$form['site_resolver_settings_container']['configuration'] = $site_resolver->buildConfigurationForm($form['configuration'], $subform_state);
138+
}
139+
110140
$form['settings_container'] = [
111141
'#type' => 'container',
112142
'#prefix' => '<div id="settings-container">',
@@ -126,35 +156,21 @@ public function form(array $form, FormStateInterface $form_state) {
126156
'#group' => 'settings',
127157
];
128158

129-
$form['preview_mode']['site_resolver'] = [
130-
'#title' => $this->t('Plugin'),
131-
'#description' => $this->t('Select a plugin to use for resolving the preview site for this entity type.'),
132-
'#type' => 'select',
133-
'#options' => array_merge(['' => $this->t('None')], array_column($this->siteResolverManager->getDefinitions(), 'label', 'id')),
134-
'#default_value' => $entity->getSiteResolver() ? $entity->getSiteResolver()->getId() : NULL,
135-
'#limit_validation_errors' => [['site_resolver']],
136-
'#submit' => ['::submitSiteResolver'],
159+
$form['preview_mode']['preview_enabled'] = [
160+
'#title' => $this->t('Enabled'),
161+
'#description' => $this->t('Enable preview mode.'),
162+
'#type' => 'checkbox',
163+
'#default_value' => $entity->isPreviewEnabled(),
164+
'#limit_validation_errors' => [['preview_enabled']],
165+
'#submit' => ['::submitPreviewEnabled'],
137166
'#executes_submit_callback' => TRUE,
138167
'#ajax' => [
139-
'callback' => '::ajaxReplaceSiteResolverSettingsForm',
140-
'wrapper' => 'site-resolver-settings',
168+
'callback' => '::ajaxReplaceSettingsForm',
169+
'wrapper' => 'settings-container',
141170
'method' => 'replace',
142171
],
143172
];
144173

145-
$form['preview_mode']['site_resolver_settings_container'] = [
146-
'#type' => 'container',
147-
'#prefix' => '<div id="site-resolver-settings">',
148-
'#suffix' => '</div>',
149-
];
150-
151-
$site_resolver = $entity->getSiteResolver();
152-
if ($site_resolver instanceof ConfigurableSiteResolverInterface) {
153-
$form['configuration'] = [];
154-
$subform_state = SubformState::createForSubform($form['configuration'], $form, $form_state);
155-
$form['preview_mode']['site_resolver_settings_container']['configuration'] = $site_resolver->buildConfigurationForm($form['configuration'], $subform_state);
156-
}
157-
158174
$form['revalidation'] = [
159175
'#title' => $this->t('On-demand Revalidation'),
160176
'#description' => $this->t('Configure on-demand revalidation for the entity type.'),
@@ -204,6 +220,14 @@ public function submitId(array $form, FormStateInterface $form_state) {
204220
$form_state->setRebuild();
205221
}
206222

223+
/**
224+
* Handles submit call when preview mode is selected.
225+
*/
226+
public function submitPreviewEnabled(array $form, FormStateInterface $form_state) {
227+
$this->entity = $this->buildEntity($form, $form_state);
228+
$form_state->setRebuild();
229+
}
230+
207231
/**
208232
* Handles switching the id selector.
209233
*/
@@ -223,7 +247,7 @@ public function submitSiteResolver(array $form, FormStateInterface $form_state)
223247
* Handles switching the site resolver selector.
224248
*/
225249
public function ajaxReplaceSiteResolverSettingsForm($form, FormStateInterface $form_state) {
226-
return $form['preview_mode']['site_resolver_settings_container'];
250+
return $form['site_resolver_settings_container'];
227251
}
228252

229253
/**

Diff for: modules/next/src/Render/MainContent/HtmlRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
108108
}
109109

110110
$next_entity_type_config = $this->nextEntityTypeManager->getConfigForEntityType($entity->getEntityTypeId(), $entity->bundle());
111-
if (!$next_entity_type_config) {
111+
if (!$next_entity_type_config || !$next_entity_type_config->isPreviewEnabled()) {
112112
return $build;
113113
}
114114

Diff for: modules/next/tests/src/Kernel/Entity/NextEntityTypeConfigTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testSiteResolver() {
6060
/** @var \Drupal\next\Entity\NextEntityTypeConfigInterface $entity_type_config */
6161
$entity_type_config = NextEntityTypeConfig::create([
6262
'id' => 'node.page',
63+
'preview_enabled' => FALSE,
6364
'site_resolver' => 'site_selector',
6465
'configuration' => [
6566
'sites' => [
@@ -124,6 +125,7 @@ public function testRevalidator() {
124125
/** @var \Drupal\next\Entity\NextEntityTypeConfigInterface $entity_type_config */
125126
$entity_type_config = NextEntityTypeConfig::create([
126127
'id' => 'node.page',
128+
'preview_enabled' => FALSE,
127129
'site_resolver' => 'site_selector',
128130
'configuration' => [
129131
'sites' => [
@@ -140,4 +142,32 @@ public function testRevalidator() {
140142
$this->assertSame('path', $revalidator->getId());
141143
}
142144

145+
/**
146+
* Tests the preview enabled property.
147+
*
148+
* @covers ::isPreviewEnabled
149+
*/
150+
public function testPreviewEnabled() {
151+
$blog_site = NextSite::create(['id' => 'blog']);
152+
$blog_site->save();
153+
154+
// Create entity type config.
155+
/** @var \Drupal\next\Entity\NextEntityTypeConfigInterface $entity_type_config */
156+
$entity_type_config = NextEntityTypeConfig::create([
157+
'id' => 'node.page',
158+
'preview_enabled' => TRUE,
159+
'site_resolver' => 'site_selector',
160+
'configuration' => [
161+
'sites' => [
162+
'blog' => 'blog',
163+
],
164+
],
165+
]);
166+
$entity_type_config->save();
167+
$this->assertTrue($entity_type_config->isPreviewEnabled());
168+
169+
$entity_type_config->set('preview_enabled', FALSE)->save();
170+
$this->assertFalse($entity_type_config->isPreviewEnabled());
171+
}
172+
143173
}

Diff for: modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function setUp(): void {
5050
// Create entity type config.
5151
$entity_type_config = NextEntityTypeConfig::create([
5252
'id' => 'node.page',
53+
'preview_enabled' => TRUE,
5354
'site_resolver' => 'site_selector',
5455
'configuration' => [
5556
'sites' => [

Diff for: modules/next/tests/src/Kernel/NextEntityTypeManagerTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function testGetSitesForEntity() {
5353

5454
$entity_type_config = NextEntityTypeConfig::create([
5555
'id' => 'node.page',
56+
'preview_enabled' => TRUE,
5657
'site_resolver' => 'site_selector',
5758
'configuration' => [
5859
'sites' => [
@@ -98,6 +99,7 @@ public function testGetSiteResolver() {
9899

99100
$entity_type_config = NextEntityTypeConfig::create([
100101
'id' => 'node.page',
102+
'preview_enabled' => TRUE,
101103
'site_resolver' => 'site_selector',
102104
'configuration' => [
103105
'sites' => [
@@ -121,6 +123,7 @@ public function testGetRevalidator() {
121123

122124
$entity_type_config = NextEntityTypeConfig::create([
123125
'id' => 'node.page',
126+
'preview_enabled' => TRUE,
124127
'site_resolver' => 'site_selector',
125128
'configuration' => [
126129
'sites' => [

Diff for: modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function testRevalidate() {
6767
// Create entity type config.
6868
$entity_type_config = NextEntityTypeConfig::create([
6969
'id' => 'node.page',
70+
'preview_enabled' => TRUE,
7071
'site_resolver' => 'site_selector',
7172
'configuration' => [
7273
'sites' => [

Diff for: modules/next/tests/src/Kernel/Plugin/SimpleOauthPreviewUrlGeneratorTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function setUp(): void {
5959
// Create entity type config.
6060
$entity_type_config = NextEntityTypeConfig::create([
6161
'id' => 'node.page',
62+
'preview_enabled' => TRUE,
6263
'site_resolver' => 'site_selector',
6364
'configuration' => [
6465
'sites' => [

Diff for: modules/next/tests/src/Kernel/Plugin/SiteResolverTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ protected function setUp(): void {
7171
// Create entity type config.
7272
$entity_type_config = NextEntityTypeConfig::create([
7373
'id' => 'node.page',
74+
'preview_enabled' => TRUE,
7475
'site_resolver' => 'site_selector',
7576
'configuration' => [
7677
'sites' => [

Diff for: modules/next/tests/src/Kernel/Renderer/MainContent/HtmlRendererTest.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class HtmlRendererTest extends KernelTestBase {
2626
*/
2727
protected static $modules = ['filter', 'next', 'node', 'system', 'user'];
2828

29+
/**
30+
* The next entity type config.
31+
*
32+
* @var \Drupal\next\Entity\NextEntityTypeConfigInterface
33+
*/
34+
protected $entityTypeConfig;
35+
2936
/**
3037
* {@inheritdoc}
3138
*/
@@ -63,16 +70,17 @@ protected function setUp(): void {
6370
$blog->save();
6471

6572
// Create entity type config.
66-
$entity_type_config = NextEntityTypeConfig::create([
73+
$this->entityTypeConfig = NextEntityTypeConfig::create([
6774
'id' => 'node.page',
75+
'preview_enabled' => TRUE,
6876
'site_resolver' => 'site_selector',
6977
'configuration' => [
7078
'sites' => [
7179
'blog' => 'blog',
7280
],
7381
],
7482
]);
75-
$entity_type_config->save();
83+
$this->entityTypeConfig->save();
7684

7785
$this->setUpCurrentUser();
7886
}
@@ -100,6 +108,16 @@ public function testPrepare() {
100108
$preview_url = 'https://blog.com/node/2';
101109
$fields = $this->xpath("//iframe[contains(@src, '$preview_url')]");
102110
$this->assertEmpty($fields);
111+
112+
// Disable preview.
113+
$this->entityTypeConfig->set('preview_enabled', FALSE);
114+
$this->entityTypeConfig->save();
115+
$request = Request::create($page->toUrl()->toString(), 'GET');
116+
$response = $this->container->get('http_kernel')->handle($request);
117+
$this->setRawContent($response->getContent());
118+
119+
$fields = $this->xpath("//iframe");
120+
$this->assertEmpty($fields);
103121
}
104122

105123
}

0 commit comments

Comments
 (0)