Skip to content

Commit

Permalink
New Next preview plugin and event subscriber (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish authored Oct 23, 2023
1 parent d55a52f commit 807a442
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"drupal/mathjax": "^4.0",
"drupal/menu_block": "^1.10",
"drupal/name": "^1.0@RC",
"drupal/next": ">=1.6",
"drupal/paragraphs": "^1.15",
"drupal/pathauto": "^1.11",
"drupal/pdb": "^1.0",
Expand Down
35 changes: 35 additions & 0 deletions src/EventSubscriber/EntityEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Drupal\core_event_dispatcher\Event\Entity\EntityUpdateEvent;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Drupal\next\Entity\NextSiteInterface;
use Drupal\node\NodeInterface;
use Drupal\stanford_profile_helper\StanfordDefaultContentInterface;
use Drupal\user\RoleInterface;
Expand Down Expand Up @@ -160,6 +161,40 @@ protected static function insertNode(NodeInterface $node): void {
}
}

/**
* When a new Next site is created, create all Next node entity type configs.
*
* @param \Drupal\next\Entity\NextSiteInterface $next_site
* Created next site config.
*/
protected static function insertNextSite(NextSiteInterface $next_site): void {
$next_storage = \Drupal::entityTypeManager()
->getStorage('next_entity_type_config');
$node_types = \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple();

// Create each of the node type bundle configs.
foreach (array_keys($node_types) as $node_bundle) {

// Make sure one doesn't already exist.
if (!$next_storage->load("node.$node_bundle")) {
$next_storage->create([
'id' => "node.$node_bundle",
'site_resolver' => 'site_selector',
'revalidator' => 'path',
'configuration' => [
'sites' => [$next_site->id() => $next_site->id()],
],
'revalidator_configuration' => [
'revalidate_page' => TRUE,
'additional_paths' => FALSE,
],
])->save();
}
}
}

/**
* On node update event listener.
*
Expand Down
39 changes: 39 additions & 0 deletions src/Plugin/Next/PreviewUrlGenerator/SimplePreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Drupal\stanford_profile_helper\Plugin\Next\PreviewUrlGenerator;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\next\Entity\NextSiteInterface;
use Drupal\next\Plugin\PreviewUrlGeneratorBase;
use Symfony\Component\HttpFoundation\Request;

/**
* Provides the preview_url_generator plugin based on simple query string.
*
* @PreviewUrlGenerator(
* id = "simple_preview",
* label = "Simple Preview",
* description = "Use the preview token string for the parameter."
* )
*/
class SimplePreview extends PreviewUrlGeneratorBase {

/**
* {@inheritdoc}
*/
public function generate(NextSiteInterface $next_site, EntityInterface $entity, string $resource_version = NULL): ?Url {
return Url::fromUri($next_site->getPreviewUrl(), [
'query' => [
'slug' => $entity->toUrl()->toString(),
'secret' => $next_site->getPreviewSecret(),
],
]);
}

/**
* {@inheritdoc}
*/
public function validate(Request $request) {}

}
15 changes: 15 additions & 0 deletions tests/src/Kernel/EventSubscriber/EntityEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\next\Entity\NextEntityTypeConfig;
use Drupal\next\Entity\NextSite;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\path_alias\Entity\PathAlias;
use Drupal\redirect\Entity\Redirect;
use Drupal\Tests\stanford_profile_helper\Kernel\SuProfileHelperKernelTestBase;
Expand Down Expand Up @@ -167,4 +170,16 @@ public function testConfigPages() {
->get('xmlsitemap_base_url'));
}

public function testNextSite() {
$this->assertEmpty(NextEntityTypeConfig::loadMultiple());
NextSite::create([
'label' => 'Blog',
'id' => 'blog',
'base_url' => 'https://blog.com',
'preview_url' => 'https://blog.com/api/preview',
'preview_secret' => 'one'
])->save();
$this->assertNotEmpty(NextEntityTypeConfig::loadMultiple());
}

}
1 change: 1 addition & 0 deletions tests/src/Kernel/SuProfileHelperKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ abstract class SuProfileHelperKernelTestBase extends KernelTestBase {
'paragraphs',
'options',
'file',
'next',
];

/**
Expand Down

0 comments on commit 807a442

Please sign in to comment.