Magento получает значения конфигурации системы из объекта $observer
-
29-09-2020 - |
Вопрос
В моей системе установлен Magento CE 1.9.2.4.
Теперь я хочу проверить конфигурацию системы моего модуля при нажатии кнопки «Сохранить конфигурацию».
Ниже приведен мой код наблюдателя:
<?php
class Mycompany_StoreLocator_Model_Observer extends Varien_Event_Observer
{
public function validateDefaultCountry($observer) {
echo "<pre/>";print_r($observer->getEvent());die;
}
}
Ниже мой config.xml:
<?xml version="1.0"?>
<!--
/**
* Mycompany_Mymodule extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Mycompany
* @package Mycompany_Mymodule
* @copyright Copyright (c) 2008 Mycompany LLC
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* @category Mycompany
* @package Mycompany_Mymodule
* @author Boris (Moshe) Gurevich <moshe@Mycompany.com>
*/
-->
<config>
<modules>
<Mycompany_Mymodule>
<version>0.2.6</version>
</Mycompany_Mymodule>
</modules>
<global>
<models>
<mymodule>
<class>Mycompany_Mymodule_Model</class>
<resourceModel>mymodule_mysql4</resourceModel>
</mymodule>
<mymodule_mysql4>
<class>Mycompany_Mymodule_Model_Mysql4</class>
<entities>
<location>
<table>mymodule_location</table>
</location>
</entities>
</mymodule_mysql4>
</models>
<events>
<admin_system_config_changed_section_mymodule>
<observers>
<mymodule>
<type>singleton</type>
<class>mymodule/observer</class>
<method>validateDefaultCountry</method>
</mymodule>
</observers>
</admin_system_config_changed_section_mymodule>
</events>
<resources>
<mymodule_setup>
<setup>
<module>Mycompany_Mymodule</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
<mymodule_write>
<connection>
<use>core_write</use>
</connection>
</mymodule_write>
<mymodule_read>
<connection>
<use>core_read</use>
</connection>
</mymodule_read>
</resources>
<helpers>
<mymodule><class>Mycompany_Mymodule_Helper</class></mymodule>
</helpers>
<blocks>
<mymodule><class>Mycompany_Mymodule_Block</class></mymodule>
</blocks>
<mymodule>
<private_fields></private_fields>
</mymodule>
</global>
<frontend>
<routers>
<mymodule>
<use>standard</use>
<args>
<module>Mycompany_Mymodule</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
<translate>
<modules>
<Mycompany_Mymodule>
<files>
<default>Mycompany_Mymodule.csv</default>
</files>
</Mycompany_Mymodule>
</modules>
</translate>
<layout>
<updates>
<mymodule module="Mycompany_Mymodule">
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
</frontend>
<admin>
<routers>
<mymoduleadmin>
<use>admin</use>
<args>
<module>Mycompany_Mymodule</module>
<frontName>mymoduleadmin</frontName>
</args>
</mymoduleadmin>
</routers>
</admin>
<adminhtml>
<menu>
<mycompany>
<title>Mycompany</title>
<sort_order>71</sort_order>
<children>
<mymodule translate="title" module="mymodule">
<title>Advance Store Locator</title>
<sort_order>3</sort_order>
<action>mymoduleadmin/adminhtml_location</action>
</mymodule>
</children>
</mycompany>
</menu>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<mymodule translate="title" module="mymodule">
<title>Advance Store Locator</title>
<sort_order>50</sort_order>
</mymodule>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
<translate>
<modules>
<Mycompany_Mymodule>
<files>
<default>Mycompany_Mymodule.csv</default>
</files>
</Mycompany_Mymodule>
</modules>
</translate>
</adminhtml>
<default>
<mymodule>
<general>
<google_geo_url><![CDATA[https://maps.google.com/maps/geo]]></google_geo_url>
<show_search>1</show_search>
<show_map>0</show_map>
</general>
</mymodule>
</default>
</config>
Как я могу получить поле конфигурации системы, которое сохраняется при запуске Observer?
Точно так же, как получение цитаты из события оформления заказа $observer->getEvent()->getQuote()
Решение
для этого вам не нужно добавлять наблюдателя.
Для значений system->config вы можете использовать серверную модель.
В system.xml
где вы объявляете свое поле, сделайте это:
<field_name_here translate="label">
<label>Label here</label>
<frontend_type>text</frontend_type>
<backend_model>[module]/something_here</backend_model><!-- add this one -->
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</field_name_here>
Затем вы создаете модель [Namespace]_[Module]_Model_Something_Here
<?php
class [Namespace]_[Module]_Model_Something_Here extends Mage_Core_Model_Config_Data
{
protected function _beforeSave()
{
//get the value being saved
$value = $this->getValue();
$isValid = your custom validation here
if (!$isValid) {
Mage::throwException(
Mage::helper('[module]')->__('your error message here')
);
}
}
}
Это предотвратит сохранение значения в случае, если оно неверно, и вы увидите сообщение об ошибке вверху страницы.
Вы можете увидеть рабочий пример для поля base_url
от app/code/core/Mage/Core/etc/system.xml
который использует серверную модель adminhtml/system_config_backend_baseurl
что переводится в класс Mage_Adminhtml_Model_System_Config_Backend_Baseurl