MVC Developers Part 8 – system configuration in depth

  • magento implicit grouped products

Last time we talked about Magento’s System Configuration System. If you missed it, you want to go back and catch up. So, we glossed over a lot of tags you can use within a separate field.

<fields>
    <!-- ... --->
    <fieldname translate="label">
        <label>Field Name</label>
        <frontend_type>text</frontend_type>
        <sort_order>2</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>0</show_in_store>
    </fieldname>
    <!-- ... --->
</fields>

Remember the rest of this article a thorough de-glossing.

<label/> and <comment/>

These tags can be set against the end-user entries for your system configuration area.

A label appears on the left of your form field, while a response is displayed directly below the field.

A label appears on the left of your form field, while a response is displayed directly below the field.

<show_in_default/>, <show_in_website/>, <show_in_store/>

These are boolean fields with a value of 1 or 0. They indicate if the field will appear when viewing the system configuration in the default view, see the website or store view.

The implicit assumption is that they also check what options are configurable at each level. However, there is nothing on the backend to maintain this assumption. This is one field can be configured with the following

<show_in_store> 0 </show_in_store>

but it will still be possible (though not recommended) to determine the value of this area and to get at the store level through means programmed quantities.

<sort_order/>

The <sort_order/> is a numerical value to the order of the fields to determine who within their group container. The higher the number the better the field will have on the screen.

<frontend_type/>

<frontend_type/> determines the type of field that will be used to collect the configuration value. Values Supported in Magento Community Edition 1.4

1. allow specifying
2. export
3. image
4. import
5. label
6. Multi Select
7. dark
8. password
9. choose
10. text
11. textarea
12. time

The value of this field is used in a factory-style pattern to instantiate a class with a size of

Varien_Data_Form_Element_Type

where “type” is the value of <frontend_type/> of text node. This occurs in the method of the addField Varien_Data_Form_Element_Abstract class, as follows

class Varien_Data_Form_Abstract extends Varien_Object
{
    //...
    public function addField($elementId, $type, $config, $after=false)
    {
        if (isset($this->_types[$type])) {
            $className = $this->_types[$type];
        }
        else {
            $className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
        }

        $element = new $className($config);
        $element->setId($elementId);
        if ($element->getRequired()) {
            $element->addClass('required-entry');
        }
        $this->addElement($element, $after);
        return $element;
    }
}

<frontend_class/>

Do not be fooled, a class is grouped <frontend_class/> name. <frontend_class/> The tag can be used for the class attribute of the form element tag is generated to change your territory. That is, this field allows you to configure a Cascading Style Sheet class (ie front-Class) added to the generated form element.

For example, a field configured with the following

<frontend_type>select</frontend_type>
<frontend_class>free-method</frontend_class>

will use as

<select class="free-method">

<validate/>

At first glance, <validate/> confusing configuration options. It seems a CSS class to be added to the generated code. A configuration like this

<validate>validate-email</validate>

provides a form element like this

<input type="text" class="validate-email" />

Like many of Magento, there’s more going on here. The presence of this CSS-class, the system is client-side validation routines. For example, the code above result in an email validation javascript must be implemented in the field. If the field is not the validation test, the end user will not be able to send the configuration form.

You can take a look at the available validation routines

js/prototype/validation.js

And here is the above e-mail validation routine in the same file

['validate-email', 'Please enter a valid email address. For example [email protected]', function (v) {
            return Validation.get('IsEmpty').test(v) || /^[a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]+(\.[a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})/i.test(v)
        }],

that looks like a monstrous, but useful, regular expressions.

<can_be_empty/>

<can_be_empty/> The tag is used with Multiselect fields. The short version is, if you like a state “no selection” to a valid configuration, use this field.

What happens behind the scenes as can_be_empty true, the system creates a hidden field on the system configuration page

File: lib/Varien/Data/Form/Element/Multiselect.php

if ($this->getCanBeEmpty()) {
    $html .= '<input type="hidden" name="' . parent::getName() . '" value="" />';
}

providing for the form processing code will accept a blank check.

<depends/>

<depends/> you can specify that your configuration area should only be displayed when another confgiruation field in the same group a specific value.

For example, the Paypal Express System configuration has the following options defined.

<specificcountry translate="label">
    <label>Countries Payment Applicable From</label>
    <frontend_type>multiselect</frontend_type>
    <sort_order>110</sort_order>
    <source_model>adminhtml/system_config_source_country</source_model>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>0</show_in_store>
    <depends><allowspecific>1</allowspecific></depends>
</specificcountry>

You, the disclosure of the all

<depends><allowspecific>1</allowspecific></depends>

This is the system to tell if the <allowspecific> area (defined with the following)

<allowspecific translate="label">
   <label>Payment Applicable From</label>
   <frontend_type>select</frontend_type>
   <sort_order>100</sort_order>    <source_model>adminhtml/system_config_source_payment_allspecificcountries</source_model>
   <show_in_default>1</show_in_default>
   <show_in_website>1</show_in_website>
   <show_in_store>0</show_in_store>
</allowspecific>

has a value, a1, AU, then the <specificcountry> field should appear. This is done directly via some JavaScript behind the scenes.

While it appears to work in all fields that have been sent onchange event, the Magento core system only uses this function when the parent is a selected area. If you are paranoid about forward compatibility, I apply the same restriction AOD to your System Configuration fields, such as AOS hard to tell where the core of Magento team can function in the future.

<source_model/>

The tag provides a <source_model/> Model class (in URI / Batch Class Name) to a field populate aos standard options. From Magento Community Edition 1.4, it works with multi-select and select. In addition to the standard Magento Batch Class Name, an extended syntax

module/modelname::methodName

supported. The system will instantiate a model getModel (‘module / model name “) and its call methodName method to an array of value out / tag Paris to use the resource. If the method name has remained, the method will be called toOptionArray standard.

<frontend_model/>

By default, Magento Form Elements made with Block-Class

Mage_Adminhtml_Block_System_Config_Form_Field

However, if you want a custom renderer to use for your system configuration area, you can block another class (in URI / Clustered Name class form) with the <frontend_model/> tag.

For example, the field in the group last_update adminnotification

<last_update translate="label">
    <label>Last update</label>
    <frontend_type>label</frontend_type>
    <frontend_model>adminhtml/system_config_form_field_notification</frontend_model>
    <sort_order>3</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>0</show_in_website>
    <show_in_store>0</show_in_store>
</last_update> 

indicates that one of the renderer admin html / system_config_form_field_notification should be used. This translates into the following class

File: app/code/core/Mage/Adminhtml/Block/System/Config/Form/Field/Notification.php
class Mage_Adminhtml_Block_System_Config_Form_Field_Notification extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $element->setValue(Mage::app()->loadCache('admin_notifications_lastcheck'));
        $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
        return Mage::app()->getLocale()->date(intval($element->getValue()))->toString($format);
    }
}

The core team is imperative _getElementHtml way to ensure that any date entered will be displayed in the same format.

<backend_model/>

Once a form is sent to Magento, its values must be saved. For the system configuration fields, this is normally handeled with the Model class

Mage_Core_Model_Config_Data

However, there may be times where you want your system configuration to another backend model. <backend_model/> The tag can be a different model class (using the URI / Grouped class name format to be specified).

Usually this is because you want to save AOT when the data are stored, but because you want some extra action when a field is stored to perform. By extending the class Mage_Core_Model_Config_Data your own model and defining a _beforeSave and / or _afterSave method on your model, you can at any time to take additional measures the value of a configuration file has changed.

Think of the entry field in the group table rate

<import translate="label">
    <label>Import</label>
    <frontend_type>import</frontend_type>
    <backend_model>adminhtml/system_config_backend_shipping_tablerate</backend_model>
    <sort_order>6</sort_order>
    <show_in_default>0</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>0</show_in_store>
</import>

The grouped Class Name admin html / system_config_backend_shipping_tablerate translates to the next class

class Mage_Adminhtml_Model_System_Config_Backend_Shipping_Tablerate extends Mage_Core_Model_Config_Data
{
    public function _afterSave()
    {
        Mage::getResourceModel('shipping/carrier_tablerate')->uploadAndImport($this);
    }
}

Here is the hook into the core team _afterSave method (which is named after a model is stored) to update the shipping / Model carrier_tablerate with information from the newly uploaded file.

<upload_dir/> and <base_url/>

Both tags are used in conjunction with the System Configuration fields <frontend_type> use images </ frontend_type> field with a <backend_model> admin html / system_config_backend_image </ backend_model> backend model. These provide both

1. When the image file is uploaded
2. The base URI path used when rendering a tag <img>

Let us first refer to the tag <upload_dir/>

<upload_dir config="system/filesystem/media" scope_info="1">sales/store/logo</upload_dir>

There are three things to be determined by the above.

1. The basic image upload path
2. The path from the base, where this particular field of view must be uploaded
3. Whether the current config scope should be added to the path

The basic image upload path is set using the config attribute (system / filesystem / media above). This specifies a System Configuration path. That is the basis, uploading images path is system / filesystem / media, but it is the value of the Magento System Configuration system / filesystem / media (who decides to (()) root_dir / media in a standard Community Edition install)

Once the base image upload path is found, we must sub-directory, this particular image must be uploaded to add. To get this we add the value of <upload_dir/> of text node

sales/store/logo

the value of the base image upload path that gives us something like this

/path/to/magento/install/media/sales/store/logo

Finally, if the scope attribute is set to “1”, the current configuration Scope into a path. If you upload an image with the scope set to default, you would like a path

/path/to/magento/install/media/sales/store/logo/default

However, if you upload an image with the scope set to a specific store, you’d end up with a path like

/path/to/magento/install/media/sales/store/logo/stores/5

where “5” is the ID of the store you are currently in scope.

When we upload an image, only the size and name of the image portion of the path to get stored in the config. This means that we need to tell the system what the base URL should be for our image.

<base_url type="media" scope_info="1">sales/store/logo</base_url>

Scope_info the text node and the same function as a s. <upload_dir/> “When <base_url/> differ is in determining the base URI of the image (that is, of course, contrary to the basic local file system).

As you probably intuitively, the baseline is set using the type attribute. The configured value will be passed into the method to the global getBaseUrl Mage object to the base URL for the image to set. The above example, that something would look like

Mage::getBaseUrl('media')

You can see the actual code that does this in Mage_Adminhtml_Block_System_Config_Form_Field_Image class.

class Mage_Adminhtml_Block_System_Config_Form_Field_Image extends Varien_Data_Form_Element_Image
{
    //...

    protected function _getUrl()
    {
        $url = parent::_getUrl();

        $config = $this->getFieldConfig();
        /* @var $config Varien_Simplexml_Element */
        if (!empty($config->base_url)) {
            $el = $config->descend('base_url');
            $urlType = empty($el['type']) ? 'link' : (string)$el['type'];
            $url = Mage::getBaseUrl($urlType) . (string)$config->base_url . '/' . $url;
        }

        return $url;
    }

    //...
}

Finally, it is important to note that this base URL is not used consistently in Magento to a URL for a final image is configured to create. From what I’ve seen this is only used to preview the image in the system configuration to create self-admin.

Wrapup

As you can see, the Magento admin Configuration System is a powerful system that is almost an application development environment itself. By using the functionality is built into the system you can quickly and reliably build new functionality tailored for your or your customer shops.

(Based on Alanstorm Turtorial)

I have been using this product for years. I have tried others but have not found anything that works better for me. Cheap viagra. Many rogue pharmacies are happy to bypass a doctor’s prescription in order to win your business.

Comments

Leave a Reply

Your email address will not be published.

two × two =

Security Code: