Magento ruft Systemkonfigurationswerte vom $observer-Objekt ab
-
29-09-2020 - |
Frage
Ich habe Magento CE 1.9.2.4 auf meinem System installiert.
Jetzt möchte ich die Systemkonfiguration meines Moduls validieren, wenn auf die Schaltfläche „Konfiguration speichern“ geklickt wird.
Unten ist mein Observer-Code:
<?php
class Mycompany_StoreLocator_Model_Observer extends Varien_Event_Observer
{
public function validateDefaultCountry($observer) {
echo "<pre/>";print_r($observer->getEvent());die;
}
}
Unten ist meine 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>
Wie kann ich das Systemkonfigurationsfeld abrufen, das gespeichert wird, wenn Observer ausgelöst wird?
Genauso wie das Einholen eines Angebots bei der Kasse $observer->getEvent()->getQuote()
Lösung
Sie müssen dafür keinen Beobachter hinzufügen.
Für system->config-Werte können Sie ein Backend-Modell verwenden.
In system.xml
Wo Sie Ihr Feld deklarieren, machen Sie Folgendes:
<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>
Anschließend erstellen Sie das Modell [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')
);
}
}
}
Dadurch wird verhindert, dass der Wert gespeichert wird, falls er falsch ist, und oben auf der Seite wird eine Fehlermeldung angezeigt.
Sie können ein funktionierendes Beispiel für den Bereich sehen base_url
aus app/code/core/Mage/Core/etc/system.xml
das das Backend-Modell verwendet adminhtml/system_config_backend_baseurl
was in die Klasse übersetzt wird Mage_Adminhtml_Model_System_Config_Backend_Baseurl