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

Add preview button display on unpublished nodes #446

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion modules/next/css/next.site_preview.iframe.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
color: #325e1c;
}

.next-site-preview-toolbar .operations .live-link .button {
.next-site-preview-toolbar .operations .live-link .preview-link .button {
margin-top: 0;
margin-bottom: 0;
}
Expand Down
1 change: 0 additions & 1 deletion modules/next/next.install
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function next_update_9103() {
}
}


/**
* Add the revalidator, revalidate_page and revalidate_paths to next_entity_type_config.
*/
Expand Down
6 changes: 6 additions & 0 deletions modules/next/next.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ next.settings:
route_name: next.settings
base_route: next.settings
weight: 20

entity.node.headless_preview:
title: Preview
route_name: entity.node.headless_preview
base_route: entity.node.canonical
weight: 5
15 changes: 15 additions & 0 deletions modules/next/next.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ next.validate_preview_url:
requirements:
_access: 'TRUE'
_format: 'json'

entity.node.headless_preview:
path: '/node/{node}/site-preview'
defaults:
_title_callback: '\Drupal\next\Controller\SitePreviewController::nodePreviewTitle'
_controller: '\Drupal\next\Controller\SitePreviewController::nodePreview'
requirements:
_entity_access: node.view
_module_dependencies: content_moderation
options:
_node_operation_route: TRUE
_admin_route: TRUE
parameters:
node:
type: entity:node
101 changes: 101 additions & 0 deletions modules/next/src/Controller/SitePreviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Drupal\next\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\next\NextEntityTypeManager;
use Drupal\next\Plugin\SitePreviewerManagerInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Defines a controller for the site preview of a node.
*
* @internal
* This is an internal part of Acquia CMS Headless and may be changed or
* removed at any time without warning. External code should not extend or
* use this class in any way!
*/
class SitePreviewController extends ControllerBase {
/**
* The next entity type manager.
*
* @var \Drupal\next\NextEntityTypeManager
*/
protected $nextEntityTypeManager;

/**
* The site previewer manager.
*
* @var \Drupal\next\Plugin\SitePreviewerManagerInterface
*/
protected $sitePreviewerManager;

/**
*
*/
public function __construct(NextEntityTypeManager $nextEntityTypeManager, SitePreviewerManagerInterface $sitePreviewerManager) {
$this->nextEntityTypeManager = $nextEntityTypeManager;
$this->sitePreviewerManager = $sitePreviewerManager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('next.entity_type.manager'),
$container->get('plugin.manager.next.site_previewer')
);
}

/**
* Displays the node title for preview.
*
* @param \Drupal\node\Entity\Node $node
* [description].
*
* @return string [description].
*/
public function nodePreviewTitle(Node $node) {
return 'Preview: ' . $node->getTitle();
}

/**
* Displays the next.js site preview of a node.
*/
public function nodePreview(Node $node) {
$storage = \Drupal::entityTypeManager()->getStorage($node->getEntityTypeId());
$revision = $storage->loadRevision($storage->getLatestRevisionId($node->id()));

$next_entity_type_config = $this->nextEntityTypeManager->getConfigForEntityType($revision->getEntityTypeId(), $revision->bundle());
$sites = $next_entity_type_config->getSiteResolver()->getSitesForEntity($revision);
if (!count($sites)) {
throw new \Exception('Next.js sites for the entity could not be resolved.');
}

$config = $this->config('next.settings');
$site_previewer_id = $config->get('site_previewer') ?? 'iframe';

/** @var \Drupal\next\Plugin\SitePreviewerInterface $site_previewer */
$site_previewer = $this->sitePreviewerManager->createInstance($site_previewer_id, $config->get('site_previewer_configuration') ?? []);
if (!$site_previewer) {
throw new PluginNotFoundException('Invalid site previewer.');
}

// Build preview.
$preview = $site_previewer->render($revision, $sites);

$context = [
'plugin' => $site_previewer,
'entity' => $revision,
'sites' => $sites,
];

// Allow modules to alter the preview.
$this->moduleHandler()->alter('next_site_preview', $preview, $context);

return $preview;
}

}