Freeze readonly fields
authoreileen <emcnaughton@wikimedia.org>
Sun, 2 May 2021 22:16:56 +0000 (10:16 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 2 May 2021 22:19:40 +0000 (10:19 +1200)
CRM/Admin/Form/Generic.php
CRM/Admin/Form/Setting.php
CRM/Admin/Form/SettingTrait.php

index 6c4a66ceff7175c309fc7c60a2e9cf0f44c95511..a44e5a55a7a372ef20effedb2544d763a9dac5d2 100644 (file)
@@ -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([
index 57e2c9fff52f6b18b7f451c0554f88bf20d9a7cc..0011769f4de6d79c36dda9213402304b8e3243c9 100644 (file)
@@ -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]);
-    }
   }
 
   /**
index 1dac8aadaccd64a4224e941e17e3d8639fbd4f19..4d96dcc6c66e35050b839118ae90b016c49620d1 100644 (file)
@@ -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]);
+    }
   }
 
   /**