4
4
5
5
use Drupal \Core \Config \ConfigCrudEvent ;
6
6
use Drupal \Core \Config \ConfigEvents ;
7
+ use Drupal \Core \Config \ConfigFactoryInterface ;
7
8
use Drupal \Core \Menu \LocalTaskManager ;
8
9
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
9
10
@@ -24,14 +25,24 @@ final class ConfigSubscriber implements EventSubscriberInterface {
24
25
*/
25
26
private $ localTaskManager ;
26
27
28
+ /**
29
+ * The config factory.
30
+ *
31
+ * @var \Drupal\Core\Config\ConfigFactoryInterface
32
+ */
33
+ protected $ configFactory ;
34
+
27
35
/**
28
36
* ConfigSubscriber constructor.
29
37
*
30
38
* @param \Drupal\Core\Menu\LocalTaskManager $local_task_manager
31
39
* The local task plugin manager.
40
+ * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
41
+ * The config factory.
32
42
*/
33
- public function __construct (LocalTaskManager $ local_task_manager ) {
43
+ public function __construct (LocalTaskManager $ local_task_manager, ConfigFactoryInterface $ config_factory ) {
34
44
$ this ->localTaskManager = $ local_task_manager ;
45
+ $ this ->configFactory = $ config_factory ;
35
46
}
36
47
37
48
/**
@@ -49,7 +60,7 @@ public function onSave(ConfigCrudEvent $event) {
49
60
// Prevent system front page to get overridden
50
61
// if pure headless mode is on.
51
62
if ($ name === 'system.site ' ) {
52
- _acquia_cms_common_update_page_configurations ('system.site ' , [
63
+ $ this -> updatePageConfigurations ('system.site ' , [
53
64
'page.front ' => '/frontpage ' ,
54
65
]);
55
66
}
@@ -64,4 +75,31 @@ public static function getSubscribedEvents(): array {
64
75
];
65
76
}
66
77
78
+ /**
79
+ * Helper function to update configuration for specified key.
80
+ *
81
+ * This is being used for updating page CT configurations.
82
+ *
83
+ * @param string $config_name
84
+ * The configuration name which needs to be updated.
85
+ * @param array $configurations
86
+ * An array of drupal configurations.
87
+ */
88
+ public function updatePageConfigurations (string $ config_name , array $ configurations ) {
89
+ $ config = $ this ->configFactory ->getEditable ($ config_name );
90
+ $ need_save = FALSE ;
91
+ if ($ config ) {
92
+ foreach ($ configurations as $ key => $ value ) {
93
+ if ($ config ->get ($ key ) != $ value ) {
94
+ $ config ->set ($ key , $ value );
95
+ $ need_save = TRUE ;
96
+ }
97
+ }
98
+ // Only save if there's changes in value.
99
+ if ($ need_save ) {
100
+ $ config ->save ();
101
+ }
102
+ }
103
+ }
104
+
67
105
}
0 commit comments