Skip to content

Commit

Permalink
Merge pull request #13345 from GinkgoFJG/generic-settings-form
Browse files Browse the repository at this point in the history
Added support to generic settings form for sorting settings by weight.
  • Loading branch information
eileenmcnaughton authored Jan 3, 2019
2 parents 75d01ce + 51f3527 commit a7e974f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CRM/Admin/Form/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ public function setDefaultValues() {
$this->setDefaultsForMetadataDefinedFields();
return $this->_defaults;
}

/**
* Build the form object.
*/
public function buildQuickForm() {
$filter = array_pop($this->urlPath);
$filter = $this->getSettingPageFilter();
$settings = civicrm_api3('Setting', 'getfields', [])['values'];
foreach ($settings as $key => $setting) {
if (isset($setting['settings_pages'][$filter])) {
$this->_settings[$key] = $setting;
}
}
// @todo sort settings by weight.

$this->addFieldsDefinedInSettingsMetadata();

// @todo look at sharing the code below in the settings trait.
Expand Down
47 changes: 46 additions & 1 deletion CRM/Admin/Form/SettingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
*/
trait CRM_Admin_Form_SettingTrait {

/**
* The setting page filter.
*
* @var string
*/
private $_filter;

/**
* @var array
*/
Expand Down Expand Up @@ -118,6 +125,44 @@ protected function getSettingMetadataItem($setting, $item) {
return CRM_Utils_Array::value($item, $this->getSettingsMetaData()[$setting]);
}

/**
* @return string
*/
protected function getSettingPageFilter() {
if (!isset($this->_filter)) {
// Get the last URL component without modifying the urlPath property.
$urlPath = array_values($this->urlPath);
$this->_filter = end($urlPath);
}
return $this->_filter;
}

/**
* Returns a re-keyed copy of the settings, ordered by weight.
*
* @return array
*/
protected function getSettingsOrderedByWeight() {
$settingMetaData = $this->getSettingsMetaData();
$filter = $this->getSettingPageFilter();

usort($settingMetaData, function ($a, $b) use ($filter) {
// Handle cases in which a comparison is impossible. Such will be considered ties.
if (
// A comparison can't be made unless both setting weights are declared.
!isset($a['settings_pages'][$filter]['weight'], $b['settings_pages'][$filter]['weight'])
// A pair of settings might actually have the same weight.
|| $a['settings_pages'][$filter]['weight'] === $b['settings_pages'][$filter]['weight']
) {
return 0;
}

return $a['settings_pages'][$filter]['weight'] > $b['settings_pages'][$filter]['weight'] ? 1 : -1;
});

return $settingMetaData;
}

/**
* Add fields in the metadata to the template.
*/
Expand Down Expand Up @@ -207,7 +252,7 @@ protected function addFieldsDefinedInSettingsMetadata() {
// setting_description should be deprecated - see Mail.tpl for metadata based tpl.
$this->assign('setting_descriptions', $descriptions);
$this->assign('settings_fields', $settingMetaData);
$this->assign('fields', $settingMetaData);
$this->assign('fields', $this->getSettingsOrderedByWeight());
}

/**
Expand Down

0 comments on commit a7e974f

Please sign in to comment.