3
3
namespace Drupal \phpfastcache \Form ;
4
4
5
5
use Drupal \Component \Utility \Random ;
6
+ use Drupal \Core \Config \ConfigFactoryInterface ;
7
+ use Drupal \Core \Extension \ModuleHandler ;
6
8
use Drupal \Core \Form \ConfigFormBase ;
7
9
use Drupal \Core \Form \FormStateInterface ;
8
10
use Drupal \phpfastcache \Cache \PhpfastcacheBackendFactory ;
9
11
use Drupal \phpfastcache \Form \Fields \PhpfastcacheAdminFieldClassMap ;
10
12
use Drupal \phpfastcache \CacheManager ;
11
13
use Phpfastcache \Api as PhpfastcacheApi ;
12
14
use Phpfastcache \Exceptions \PhpfastcacheDriverCheckException ;
15
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
13
16
14
17
/**
15
18
* Configure phpfastcache settings for this site.
@@ -19,6 +22,32 @@ class PhpFastCacheAdminSettingsForm extends ConfigFormBase {
19
22
20
23
const PREFIX_REGEXP = '^\w*$ ' ;
21
24
25
+ /**
26
+ * @var \Drupal\Core\Extension\ModuleHandler
27
+ */
28
+ protected $ moduleHandler ;
29
+
30
+ /**
31
+ * PhpFastCacheAdminSettingsForm constructor.
32
+ *
33
+ * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
34
+ * @param \Drupal\Core\Extension\ModuleHandler $moduleHandler
35
+ */
36
+ public function __construct (ConfigFactoryInterface $ config_factory , ModuleHandler $ moduleHandler ) {
37
+ parent ::__construct ($ config_factory );
38
+ $ this ->moduleHandler = $ moduleHandler ;
39
+ }
40
+
41
+ /**
42
+ * {@inheritdoc}
43
+ */
44
+ public static function create (ContainerInterface $ container ) {
45
+ return new static (
46
+ $ container ->get ('config.factory ' ),
47
+ $ container ->get ('module_handler ' )
48
+ );
49
+ }
50
+
22
51
/**
23
52
* {@inheritdoc}
24
53
*/
@@ -94,27 +123,31 @@ public function buildForm(array $form, FormStateInterface $form_state) {
94
123
],
95
124
];
96
125
97
- // @todo conditional if webprofiler module
98
- $ form [ 'general ' ][ 'phpfastcache_settings_wrapper ' ][ 'phpfastcache_env ' ] = [
99
- '#default_value ' => (string ) $ config ->get ('phpfastcache_env ' ),
100
- '#description ' => $ this ->t (
101
- '<strong>Production</strong>: Will displays minimal information in case of failure.<br />
126
+ if ( $ this -> moduleHandler -> moduleExists ( ' devel ' )){
127
+ $ form [ 'general ' ][ 'phpfastcache_settings_wrapper ' ][ 'phpfastcache_env ' ] = [
128
+ '#default_value ' => (string ) $ config ->get ('phpfastcache_env ' ),
129
+ '#description ' => $ this ->t (
130
+ '<strong>Production</strong>: Will displays minimal information in case of failure.<br />
102
131
<strong>Development</strong>: Will displays very verbose information in case of failure. '
103
- ),
104
- '#required ' => TRUE ,
105
- '#options ' => [
106
- PhpfastcacheBackendFactory::ENV_DEV => t ('Development ' ),
107
- PhpfastcacheBackendFactory::ENV_PROD => t ('Production ' ),
108
- ],
109
- '#title ' => $ this ->t ('PhpFastCache environment ' ),
110
- '#type ' => 'select ' ,
111
- ];
132
+ ),
133
+ '#required ' => TRUE ,
134
+ '#options ' => [
135
+ PhpfastcacheBackendFactory::ENV_DEV => t ('Development ' ),
136
+ PhpfastcacheBackendFactory::ENV_PROD => t ('Production ' ),
137
+ ],
138
+ '#title ' => $ this ->t ('PhpFastCache environment ' ),
139
+ '#type ' => 'select ' ,
140
+ ];
141
+ }else if ((string ) $ config ->get ('phpfastcache_env ' ) === PhpfastcacheBackendFactory::ENV_DEV ){
142
+ $ config ->set ('phpfastcache_env ' , PhpfastcacheBackendFactory::ENV_PROD )
143
+ ->save ();
144
+ }
112
145
113
146
$ form [ 'general ' ][ 'phpfastcache_settings_wrapper ' ][ 'phpfastcache_prefix ' ] = [
114
147
'#default_value ' => (string ) ($ config ->get ('phpfastcache_prefix ' ) ?: $ randomService ->name (6 , TRUE )),
115
148
'#description ' => $ this ->t (
116
149
'The cache keyspace prefix that will be used to identify this website.
117
- This value length <strong>MUST</strong> be up to 8 chars and 4 chars minimum. <br />
150
+ This value length <strong>MUST</strong> be up to 8 chars and 2 chars minimum. <br />
118
151
This value <strong>MUST</strong> be unique depending your other Drupal installations on this cache backend. <br />
119
152
This value <strong>MUST</strong> be alpha-numeric ( ' . self ::PREFIX_REGEXP . ') '
120
153
),
@@ -205,7 +238,6 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
205
238
$ form_state ->setError ($ form , 'The driver chosen is unavailable ! ' );
206
239
}
207
240
208
-
209
241
/**
210
242
* Field Validation: phpfastcache_prefix
211
243
*/
@@ -228,11 +260,13 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
228
260
public function submitForm (array &$ form , FormStateInterface $ form_state ) {
229
261
$ config = $ this ->config ('phpfastcache.settings ' );
230
262
$ config ->set ('phpfastcache_enabled ' , (bool ) $ form_state ->getValue ('phpfastcache_enabled ' ))
231
- ->set ('phpfastcache_env ' , (string ) $ form_state ->getValue ('phpfastcache_env ' ))
232
263
->set ('phpfastcache_prefix ' , (string ) $ form_state ->getValue ('phpfastcache_prefix ' ))
233
264
->set ('phpfastcache_default_ttl ' , (int ) $ form_state ->getValue ('phpfastcache_default_ttl ' ))
234
265
->set ('phpfastcache_default_driver ' , (string ) $ form_state ->getValue ('phpfastcache_default_driver ' ));
235
266
267
+ if ($ this ->moduleHandler ->moduleExists ('devel ' )){
268
+ $ config ->set ('phpfastcache_env ' , (string ) $ form_state ->getValue ('phpfastcache_env ' ));
269
+ }
236
270
/*****************
237
271
* Drivers settings
238
272
*****************/
@@ -260,7 +294,7 @@ protected function isAvailableDriver(string $driverName): bool {
260
294
* using memcache and memcached or
261
295
* redis and predis together for example
262
296
*/
263
- @CacheManager::getInstance ($ driverName );
297
+ $ i = @CacheManager::getInstance ($ driverName );
264
298
return TRUE ;
265
299
} catch (PhpfastcacheDriverCheckException $ e ) {
266
300
return FALSE ;
0 commit comments