Skip to content

Commit

Permalink
Next preview plugin and event subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Oct 23, 2023
1 parent d55a52f commit dea70a2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
32 changes: 32 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,37 @@ 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();

foreach (array_keys($node_types) as $node_bundle) {
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
40 changes: 40 additions & 0 deletions src/Plugin/Next/PreviewUrlGenerator/SimplePreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 {
$query = [];
$query['slug'] = $entity->toUrl()->toString();
$query['secret'] = $next_site->getPreviewSecret();

return Url::fromUri($next_site->getPreviewUrl(), [
'query' => $query,
]);
}

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

}

0 comments on commit dea70a2

Please sign in to comment.