Skip to content

Commit

Permalink
inject all the dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tjheffner committed Nov 15, 2023
1 parent ac2602b commit 1c377d8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace Drupal\va_gov_preview\EventSubscriber;

use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Http\RequestStack;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\feature_toggle\FeatureStatus;
use Drupal\next\NextEntityTypeManagerInterface;
use Drupal\next\NextSettingsManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\preprocess_event_dispatcher\Event\PagePreprocessEvent;
use Drupal\va_gov_backend\Service\ExclusionTypes;
use Drupal\va_gov_build_trigger\Form\PreviewForm;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -39,6 +43,34 @@ class PreviewEventSubscriber implements EventSubscriberInterface {
*/
protected $routeMatch;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* The language manager interface.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;

/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;

/**
* The VA gov backend exclusion types service.
*
* @var \Drupal\va_gov_backend\Service\ExclusionTypes
*/
protected $exclusionTypes;

/**
* The next entity type manager.
*
Expand Down Expand Up @@ -67,6 +99,14 @@ class PreviewEventSubscriber implements EventSubscriberInterface {
* The string entity type service.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* Interface for classes representing the result of routing.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter.
* @param \Drupal\va_gov_backend\Service\ExclusionTypes $exclusion_types
* The va_gov_backend exclusion types service.
* @param \Drupal\next\NextEntityTypeManagerInterface $next_entity_type_manager
* Interface for retrieving all connected Next.js sites.
* @param \Drupal\next\NextSettingsManagerInterface $next_settings_manager
Expand All @@ -77,12 +117,20 @@ class PreviewEventSubscriber implements EventSubscriberInterface {
public function __construct(
EntityTypeManager $entityTypeManager,
RouteMatchInterface $route_match,
RequestStack $request_stack,
LanguageManagerInterface $language_manager,
DateFormatter $date_formatter,
ExclusionTypes $exclusion_types,
NextEntityTypeManagerInterface $next_entity_type_manager,
NextSettingsManagerInterface $next_settings_manager,
FeatureStatus $feature_status,
) {
$this->entityTypeManager = $entityTypeManager;
$this->routeMatch = $route_match;
$this->requestStack = $request_stack;
$this->languageManager = $language_manager;
$this->dateFormatter = $date_formatter;
$this->exclusionTypes = $exclusion_types;
$this->nextEntityTypeManager = $next_entity_type_manager;
$this->nextSettingsManager = $next_settings_manager;
$this->nextPreviewEnabled = $feature_status->getStatus(self::NEXT_PREVIEW_FEATURE_NAME);
Expand Down Expand Up @@ -114,12 +162,12 @@ public function preprocessPage(PagePreprocessEvent $event): void {
}

$route_name = $this->routeMatch->getRouteName();
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$language = $this->languageManager->getCurrentLanguage()->getId();

// Make sure we aren't on the node form or an excluded type.
if (($route_name !== 'entity.node.edit_form') && ($language === 'en')) {
$last_saved_by_an_editor = $node->get('field_last_saved_by_an_editor')->value;
$vars['page']['last_saved_by_an_editor'] = $last_saved_by_an_editor ? \Drupal::service('date.formatter')->format($last_saved_by_an_editor, 'custom', 'F j Y g:ia') : 'Unknown';
$vars['page']['last_saved_by_an_editor'] = $last_saved_by_an_editor ? $this->dateFormatter->format($last_saved_by_an_editor, 'custom', 'F j Y g:ia') : 'Unknown';

$button = $this->generatePreviewButton($node);
$vars['page']['sidebar_second']['#markup'] = $button;
Expand All @@ -143,14 +191,14 @@ protected function generatePreviewButton(NodeInterface $node): string|null {
return NULL;
}
// Make sure we aren't on /training-guide.
$current_uri = \Drupal::request()->getRequestUri();
$current_uri = $this->requestStack->getCurrentRequest()->getRequestUri();
if ($current_uri === '/training-guide') {
return NULL;
}

$node = $this->routeMatch->getParameter('node');
$nid = $node->id();
$host = \Drupal::request()->getHost();
$host = $this->requestStack->getCurrentRequest()->getHost();
$preview_form = new PreviewForm();
$url = $preview_form->getEnvironment($host, $nid);
}
Expand All @@ -167,7 +215,7 @@ protected function generatePreviewButton(NodeInterface $node): string|null {
* TRUE if the node type has a corresponding next config entity.
*/
protected function checkNextEnabledTypes(string $type): bool {
// Replace this array with a config check.
// @todo Replace this array with a proper config check.
$enabled_types = ['news_story', 'story_listing'];
return in_array($type, $enabled_types);
}
Expand All @@ -182,7 +230,7 @@ protected function checkNextEnabledTypes(string $type): bool {
* TRUE if the node type is excluded from preview.
*/
protected function checkExcludedTypes(NodeInterface $node): bool {
$exclusion_types_from_config = \Drupal::service('va_gov_backend.exclusion_types')->getExcludedTypes();
$exclusion_types_from_config = $this->exclusionTypes->getExcludedTypes();
$list_types = [
// List pages don't play nicely with preview.
'event_listing',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ services:
arguments: ['@messenger', '@string_translation']
tags:
- { name: encoder, priority: 10, format: static_html }
va_gov_preview.entity_event_subscriber:
va_gov_preview.preview_event_subscriber:
class: Drupal\va_gov_preview\EventSubscriber\PreviewEventSubscriber
arguments:
[
'@entity_type.manager',
'@current_route_match',
'@request_stack',
'@language_manager',
'@date.formatter',
'@va_gov_backend.exclusion_types',
'@next.entity_type.manager',
'@next.settings.manager',
'@feature_toggle.feature_status'
Expand Down

0 comments on commit 1c377d8

Please sign in to comment.