Skip to content

Commit 94f2390

Browse files
committedMar 29, 2022
(TXT) - Add examples about session values in yml files.
1 parent b0b5acd commit 94f2390

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed
 

‎_posts/2022-03-30-drupal-tips-changing-session-lifetime-for-users.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ session.gc_maxlifetime = 3600
7070
session.gc_probability = 1
7171
session.gc_divisor = 1000
7272
```
73-
But in the Drupal context, we can change these values from a Drupal installation, inside the file `services.yml` in `docroot/sites/default/`:
73+
But in the Drupal context, we can change these values from a Drupal installation, inside the file `default.services.yml` in `web/sites/default/` or in `core.services.yml` in `web/core/`:
7474

7575
```
7676
parameters:
@@ -80,7 +80,7 @@ parameters:
8080
gc_maxlifetime: 200000
8181
cookie_lifetime: 2000000
8282
```
83-
As you can see expanded in the original block:
83+
As you can see expanded in the original block: from `default.services.yml`:
8484
```
8585
session.storage.options:
8686
# Default ini options for sessions.
@@ -134,17 +134,30 @@ gc_maxlifetime: 3600 (only one hour of lifetime for the session).
134134
cookie_lifetime: 0 (deleted when user close the browser tab).
135135
```
136136

137-
## Overwriting default values by switching service class
137+
## Overwriting default values by switching service class
138138

139+
**The bad news** is that the above values are statically part of the service container after a Drupal installation, and cannot be modified "dynamically". And these previous values are in the by-default configuration of the Drupal installation.
139140

141+
**The good news** is that we can add others values by switching with another class to replace the service provision. . Besides this is part of the Container in Drupal, so can only be overwritten from a custom Drupal module using a pair of classes. We can do something like:
142+
143+
- `gc_maxlifetime: 3600` (only one hour of lifetime for the session, and then make the session available for the garbage collector).
144+
- `cookie_lifetime: 0 ` (deleted when user closes the browser tab).
145+
146+
Only we need change the class responsible for the sessions management, used in service `session_configuration` as described in `core.services.yml`:
147+
148+
```
149+
session_configuration:
150+
class: Drupal\Core\Session\SessionConfiguration
151+
arguments: ['%session.storage.options%']
152+
```
140153

141154
From a ServiceProvider class, by linking the service to our new custom class:
142155

143156
```
144157
public function alter(ContainerBuilder $container) {
145158
$definition = $container->getDefinition('session_configuration');
146-
$definition->setClass('Drupal\st_session_manager\StSessionConfiguration');
147-
}
159+
$definition->setClass('Drupal\custom_session_manager\StSessionConfiguration');
160+
}
148161
```
149162

150163

0 commit comments

Comments
 (0)