Skip to content

Commit

Permalink
Update adminhtml/config (OpenMage#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbeaty authored Jan 17, 2023
1 parent 7b3c162 commit 38656dc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
6 changes: 4 additions & 2 deletions app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
. Mage::helper($helperName)->__((string)$element->label);
$hint = (string)$element->hint ? Mage::helper($helperName)->__((string)$element->hint) : '';

if ($element->backend_model) {
$model = Mage::getModel((string)$element->backend_model);
$helper = Mage::helper('adminhtml/config');
$backendClass = $helper->getBackendModelByFieldConfig($element);
if ($backendClass) {
$model = Mage::getModel($backendClass);
if (!$model instanceof Mage_Core_Model_Config_Data) {
Mage::throwException('Invalid config field backend model: ' . (string)$element->backend_model);
}
Expand Down
57 changes: 42 additions & 15 deletions app/code/core/Mage/Adminhtml/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,56 @@ class Mage_Adminhtml_Helper_Config extends Mage_Core_Helper_Abstract
protected $_moduleName = 'Mage_Adminhtml';

/**
* @var array
* Return information array of input types
*
* @param string $inputType
* @return array
*/
protected $defaultBackendClassMap = [
'color' => 'adminhtml/system_config_backend_color'
];
public function getInputTypes(string $inputType = null): array
{
$inputTypes = [
'color' => [
'backend_model' => 'adminhtml/system_config_backend_color'
]
];

if (is_null($inputType)) {
return $inputTypes;
} elseif (isset($inputTypes[$inputType])) {
return $inputTypes[$inputType];
}
return [];
}

/**
* Get field backend model class
* Return default backend model by input type
*
* @param Varien_Simplexml_Element $fieldConfig
* @return string
* @param string $inputType
* @return string|null
*/
public function getBackendClass(Varien_Simplexml_Element $fieldConfig): string
public function getBackendModelByInputType(string $inputType): ?string
{
$backendClass = (isset($fieldConfig->backend_model)) ? $fieldConfig->backend_model : false;
if ($backendClass) {
return (string) $backendClass;
$inputTypes = $this->getInputTypes();
if (!empty($inputTypes[$inputType]['backend_model'])) {
return $inputTypes[$inputType]['backend_model'];
}
return null;
}

if (isset($fieldConfig->frontend_type, $this->defaultBackendClassMap[(string)$fieldConfig->frontend_type])) {
return $this->defaultBackendClassMap[(string)$fieldConfig->frontend_type];
/**
* Get field backend model by field config node
*
* @param Varien_Simplexml_Element $fieldConfig
* @return string|null
*/
public function getBackendModelByFieldConfig(Varien_Simplexml_Element $fieldConfig): ?string
{
if (isset($fieldConfig->backend_model)) {
return (string)$fieldConfig->backend_model;
}

return 'core/config_data';
if (isset($fieldConfig->frontend_type)) {
return $this->getBackendModelByInputType((string)$fieldConfig->frontend_type);
}
return null;
}
}
5 changes: 4 additions & 1 deletion app/code/core/Mage/Adminhtml/Model/Config/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ public function save()
}

$helper = Mage::helper('adminhtml/config');
$backendClass = $helper->getBackendClass($fieldConfig);
$backendClass = $helper->getBackendModelByFieldConfig($fieldConfig);
if (!$backendClass) {
$backendClass = 'core/config_data';
}

/** @var Mage_Core_Model_Config_Data $dataObject */
$dataObject = Mage::getModel($backendClass);
Expand Down

0 comments on commit 38656dc

Please sign in to comment.