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

NYS-166: Fix bill pages not loading on multidev environments, and other potential issues (by moving calls to getCurrentRequest() from class constructor to methods) #229

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 11 additions & 7 deletions web/modules/custom/nys_accumulator/src/Service/Accumulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\nys_senators\SenatorsHelper;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand All @@ -34,11 +33,11 @@ class Accumulator implements ContainerInjectionInterface {
protected Connection $database;

/**
* Drupal's Current Request service.
* Symfony's RequestStack service.
*
* @var \Symfony\Component\HttpFoundation\Request
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected Request $request;
protected RequestStack $requestStack;

/**
* NYS Senators Helper service.
Expand All @@ -50,10 +49,15 @@ class Accumulator implements ContainerInjectionInterface {
/**
* Constructor.
*/
public function __construct(EventDispatcherInterface $dispatcher, Connection $database, RequestStack $request, SenatorsHelper $helper) {
public function __construct(
EventDispatcherInterface $dispatcher,
Connection $database,
RequestStack $requestStack,
SenatorsHelper $helper,
) {
$this->dispatcher = $dispatcher;
$this->database = $database;
$this->request = $request->getCurrentRequest();
$this->requestStack = $requestStack;
$this->helper = $helper;
}

Expand All @@ -76,7 +80,7 @@ public function newEntry(): AccumulatorEntry {
return new AccumulatorEntry(
$this->dispatcher,
$this->database,
$this->request,
$this->requestStack->getCurrentRequest(),
$this->helper
);
}
Expand Down
25 changes: 12 additions & 13 deletions web/modules/custom/nys_senators/src/SenatorsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\nys_senators\Service\Microsites;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
Expand All @@ -33,11 +32,11 @@ class SenatorsHelper {
protected CacheBackendInterface $cache;

/**
* The current request from Drupal's Request Stack service.
* Symfony's RequestStack service.
*
* @var \Symfony\Component\HttpFoundation\Request
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected Request $request;
protected RequestStack $requestStack;

/**
* NYS Senators Microsites service.
Expand All @@ -53,20 +52,20 @@ class SenatorsHelper {
* The entity type manager service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The backend cache.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* A request stack service.
* @param \Drupal\nys_senators\Service\Microsites $microsites
* NYS Senators Microsites service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, CacheBackendInterface $cache_backend, RequestStack $request_stack, Microsites $microsites) {
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
CacheBackendInterface $cache_backend,
RequestStack $requestStack,
Microsites $microsites,
) {
$this->entityTypeManager = $entity_type_manager;
$this->cache = $cache_backend;
// Adding this condition because for some reason
// it throws an error for anonymous users
// when viewing a bill.
if ($request_stack->getCurrentRequest() !== NULL) {
$this->request = $request_stack->getCurrentRequest();
}
$this->requestStack = $requestStack;
$this->microsites = $microsites;
}

Expand Down Expand Up @@ -111,7 +110,7 @@ public function getMicrositeUrl(Term $senator): string {
* @see static::compileMicrosites()
*/
protected function generateMicrositeUrl(string $name): string {
return $this->request->getSchemeAndHttpHost() .
return $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() .
'/senators/' . strtolower(str_replace([' ', '.'], ['-', ''], $name));
}

Expand Down
11 changes: 5 additions & 6 deletions web/modules/custom/nys_senators/src/Service/Microsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
Expand Down Expand Up @@ -71,11 +70,11 @@ class Microsites {
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Symphony's Request object.
* Symfony's RequestStack service.
*
* @var \Symfony\Component\HttpFoundation\Request
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected Request $request;
protected RequestStack $requestStack;

/**
* Constructor.
Expand All @@ -89,7 +88,7 @@ public function __construct(
$this->cache = $cache;
$this->theme = $theme;
$this->entityTypeManager = $entityTypeManager;
$this->request = $requestStack->getCurrentRequest();
$this->requestStack = $requestStack;
}

/**
Expand Down Expand Up @@ -228,7 +227,7 @@ protected function compileMicrosites(): array {
$senator_id = $page->field_senator_multiref->entity->id() ?? 0;
$url_options = [
'absolute' => TRUE,
'base_url' => $this->request->getSchemeAndHttpHost(),
'base_url' => $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost(),
];
$url = $page->toUrl('canonical', $url_options)->toString();
}
Expand Down
Loading