From dea70a28357a9f0b8f502ff35d52de830b609e6a Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Sun, 22 Oct 2023 21:43:16 -0700 Subject: [PATCH] Next preview plugin and event subscriber --- src/EventSubscriber/EntityEventSubscriber.php | 32 +++++++++++++++ .../PreviewUrlGenerator/SimplePreview.php | 40 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/Plugin/Next/PreviewUrlGenerator/SimplePreview.php diff --git a/src/EventSubscriber/EntityEventSubscriber.php b/src/EventSubscriber/EntityEventSubscriber.php index 2cb682ae..4ce5d5d5 100644 --- a/src/EventSubscriber/EntityEventSubscriber.php +++ b/src/EventSubscriber/EntityEventSubscriber.php @@ -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; @@ -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. * diff --git a/src/Plugin/Next/PreviewUrlGenerator/SimplePreview.php b/src/Plugin/Next/PreviewUrlGenerator/SimplePreview.php new file mode 100644 index 00000000..becbc0b5 --- /dev/null +++ b/src/Plugin/Next/PreviewUrlGenerator/SimplePreview.php @@ -0,0 +1,40 @@ +toUrl()->toString(); + $query['secret'] = $next_site->getPreviewSecret(); + + return Url::fromUri($next_site->getPreviewUrl(), [ + 'query' => $query, + ]); + } + + /** + * {@inheritdoc} + */ + public function validate(Request $request) {} + +}