Domanda

Ho Magento CE 1.9.2.4 installato nel mio sistema.

Ora voglio convalidare la configurazione del sistema del mio modulo quando si fa clic sul pulsante "Salva configurazione".

Di seguito è il mio codice osservatore:

<?php
class Mycompany_StoreLocator_Model_Observer extends Varien_Event_Observer
{
    public function validateDefaultCountry($observer) {
        echo "<pre/>";print_r($observer->getEvent());die;
    }
}
.

sotto è il mio 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>
.

Come posso ottenere il campo di configurazione del sistema che viene salvato quando viene attivato l'osservatore?

Proprio come il preventivo da Checkout Event $observer->getEvent()->getQuote()

È stato utile?

Soluzione

non è necessario aggiungere un osservatore per quello.
Per sistemi-> valori di configurazione è possibile utilizzare un modello di backend.
In system.xml dove dichiari il tuo campo:

<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>
.

Quindi si crea il modello [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')
            );
        }

    }
}
.

Ciò impedirà al valore di salvare nel caso in cui sia sbagliato e verrà visualizzato un messaggio di errore nella parte superiore della pagina.
È possibile visualizzare un esempio di funzionamento per il campo base_url da app/code/core/Mage/Core/etc/system.xml che utilizza il modello di backend adminhtml/system_config_backend_baseurl che si traduce in classe Mage_Adminhtml_Model_System_Config_Backend_Baseurl

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top