From 6be2178ca3365af99f5275bef631161aba971599 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 3 May 2021 10:16:56 +1200 Subject: [PATCH] Freeze readonly fields --- CRM/Admin/Form/Generic.php | 6 ------ CRM/Admin/Form/Setting.php | 6 ------ CRM/Admin/Form/SettingTrait.php | 25 +++++++++++++++++++++++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/CRM/Admin/Form/Generic.php b/CRM/Admin/Form/Generic.php index 6c4a66ceff..a44e5a55a7 100644 --- a/CRM/Admin/Form/Generic.php +++ b/CRM/Admin/Form/Generic.php @@ -24,7 +24,6 @@ class CRM_Admin_Form_Generic extends CRM_Core_Form { use CRM_Admin_Form_SettingTrait; protected $_settings = []; - protected $includesReadOnlyFields = FALSE; public $_defaults = []; /** @@ -57,11 +56,6 @@ class CRM_Admin_Form_Generic extends CRM_Core_Form { public function buildQuickForm() { $this->addFieldsDefinedInSettingsMetadata(); - // @todo look at sharing the code below in the settings trait. - if ($this->includesReadOnlyFields) { - CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]); - } - // @todo - do we still like this redirect? CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); $this->addButtons([ diff --git a/CRM/Admin/Form/Setting.php b/CRM/Admin/Form/Setting.php index 57e2c9fff5..0011769f4d 100644 --- a/CRM/Admin/Form/Setting.php +++ b/CRM/Admin/Form/Setting.php @@ -24,8 +24,6 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form { protected $_settings = []; - protected $includesReadOnlyFields; - /** * Set default values for the form. * @@ -66,10 +64,6 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form { ]); $this->addFieldsDefinedInSettingsMetadata(); - - if ($this->includesReadOnlyFields) { - CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]); - } } /** diff --git a/CRM/Admin/Form/SettingTrait.php b/CRM/Admin/Form/SettingTrait.php index 1dac8aadac..4d96dcc6c6 100644 --- a/CRM/Admin/Form/SettingTrait.php +++ b/CRM/Admin/Form/SettingTrait.php @@ -43,12 +43,28 @@ trait CRM_Admin_Form_SettingTrait { return 'Setting'; } + /** + * Fields defined as read only. + * + * @var array + */ + protected $readOnlyFields = []; + + /** + * Have read only fields been defined on the form. + * + * @return bool + */ + protected function hasReadOnlyFields(): bool { + return !empty($this->readOnlyFields); + } + /** * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings. * * @return array */ - protected function getSettingsMetaData() { + protected function getSettingsMetaData(): array { if (empty($this->settingsMetadata)) { $this->settingsMetadata = \Civi\Core\SettingsMetadata::getMetadata(['name' => array_keys($this->_settings)], NULL, TRUE); // This array_merge re-orders to the key order of $this->_settings. @@ -178,7 +194,7 @@ trait CRM_Admin_Form_SettingTrait { //Load input as readonly whose values are overridden in civicrm.settings.php. if (Civi::settings()->getMandatory($setting) !== NULL) { $props['html_attributes']['readonly'] = TRUE; - $this->includesReadOnlyFields = TRUE; + $this->readOnlyFields[] = $setting; } $add = 'add' . $quickFormType; @@ -253,6 +269,11 @@ trait CRM_Admin_Form_SettingTrait { $this->assign('setting_descriptions', $descriptions); $this->assign('settings_fields', $settingMetaData); $this->assign('fields', $this->getSettingsOrderedByWeight()); + // @todo look at sharing the code below in the settings trait. + if ($this->hasReadOnlyFields()) { + $this->freeze($this->readOnlyFields); + CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]); + } } /** -- 2.25.1