Skip to content

Commit

Permalink
Issue #3324881: Check for session before calling Request::getSession()
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Burge authored and Nedjo Rogers committed Dec 15, 2022
1 parent 8a34ef8 commit 25e3358
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions features.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ function features_post_update_features_assigner_args() {
function features_post_update_prefixed_dependencies() {
// Empty post-update hook.
}

/**
* Clear caches due to changes in features_assigner service arguments.
*/
function features_post_update_features_assigner_args2() {
// Empty post-update hook.
}
2 changes: 1 addition & 1 deletion features.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler']
features_assigner:
class: Drupal\features\FeaturesAssigner
arguments: ['@features.manager', '@plugin.manager.features_assignment_method', '@entity_type.manager', '@config.factory', '@config.storage', '%install_profile%']
arguments: ['@features.manager', '@plugin.manager.features_assignment_method', '@entity_type.manager', '@config.factory', '@config.storage', '%install_profile%', '@request_stack']
calls:
- [initFeaturesManager]
features_generator:
Expand Down
5 changes: 4 additions & 1 deletion modules/features_ui/src/Form/FeaturesEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ public function getFormId() {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $featurename = '') {
$session = $this->getRequest()->getSession();
if ($this->getRequest()->hasSession()) {
$session = $this->getRequest()->getSession();
}

$trigger = $form_state->getTriggeringElement();
if (isset($trigger['#name']) && $trigger['#name'] == 'package') {
// Save current bundle name for later ajax callback.
Expand Down
19 changes: 14 additions & 5 deletions src/FeaturesAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class responsible for performing package assignment.
Expand Down Expand Up @@ -78,6 +79,13 @@ class FeaturesAssigner implements FeaturesAssignerInterface {
*/
protected $installProfile;

/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;

/**
* Constructs a new FeaturesAssigner object.
*
Expand All @@ -94,13 +102,14 @@ class FeaturesAssigner implements FeaturesAssignerInterface {
* @param string $install_profile
* The name of the currently active installation profile.
*/
public function __construct(FeaturesManagerInterface $features_manager, PluginManagerInterface $assigner_manager, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, StorageInterface $config_storage, $install_profile) {
public function __construct(FeaturesManagerInterface $features_manager, PluginManagerInterface $assigner_manager, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, StorageInterface $config_storage, $install_profile, RequestStack $request_stack) {
$this->featuresManager = $features_manager;
$this->assignerManager = $assigner_manager;
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->configStorage = $config_storage;
$this->installProfile = $install_profile;
$this->request = $request_stack->getCurrentRequest();
$this->bundles = $this->getBundleList();
$this->currentBundle = $this->getBundle(FeaturesBundleInterface::DEFAULT_BUNDLE);
// Ensure bundle information is fresh.
Expand Down Expand Up @@ -259,8 +268,8 @@ public function findBundle(array $info, $features_info = NULL) {
*/
public function setCurrent(FeaturesBundleInterface $bundle) {
$this->currentBundle = $bundle;
if (\Drupal::request()->hasSession()) {
$session = \Drupal::request()->getSession();
if ($this->request->hasSession()) {
$session = $this->request->getSession();
$session->set('features_current_bundle', $bundle->getMachineName());
}
return $bundle;
Expand Down Expand Up @@ -429,8 +438,8 @@ public function renameBundle($old_machine, $new_machine) {
*/
public function loadBundle($machine_name = NULL) {
if (!isset($machine_name)) {
$session = \Drupal::request()->getSession();
if (isset($session)) {
if ($this->request->hasSession()) {
$session = $this->request->getSession();
$machine_name = isset($session) ? $session->get('features_current_bundle', FeaturesBundleInterface::DEFAULT_BUNDLE) : FeaturesBundleInterface::DEFAULT_BUNDLE;
}
}
Expand Down

0 comments on commit 25e3358

Please sign in to comment.