Introduction

This article describes how developer can create a complex setting class in X-Cart instead of using predefined ones from the <X-Cart>/classes/XLite/View/FormField/ folder. For the sake of example, we will create a new page in admin area where we will display our own select-box with custom values.

Table of Contents

Implementation

We start with creating a module with developer ID Tony and module ID FormFieldDemo. Then, we create a page target=form_field in admin area. During this process, we create:

  • empty controller class \XLite\Module\Tony\FormFieldDemo\Controller\Admin\FormField;
  • viewer class \XLite\Module\Tony\FormFieldDemo\View\Page\Admin\FormField with the following content:

    <?php
    // vim: set ts=4 sw=4 sts=4 et:
    
    namespace XLite\Module\Tony\FormFieldDemo\View\Page\Admin;
    
    /**
     * Form field page view
     *
     * @ListChild (list="admin.center", zone="admin")
     */
    class FormField extends \XLite\View\AView
    {
        /**
         * Return list of allowed targets
         *
         * @return array
         */
        public static function getAllowedTargets()
        {
            return array_merge(parent::getAllowedTargets(), array('form_field'));
        }
    
        /**
         * Return widget default template
         *
         * @return string
         */
        protected function getDefaultTemplate()
        {
            return 'modules/Tony/FormFieldDemo/page/form_field/body.tpl';
        }
    }
    
  • empty template <X-Cart>/skins/admin/en/modules/Tony/FormField/page/form_field/body.tpl.

Next step is to create a select-box class. We create the <X-Cart>/classes/XLite/Module/Tony/FormField/View/FormField/Select/CustomSelect.php file with the following content: 

<?php

namespace XLite\Module\Tony\FormFieldDemo\View\FormField\Select;

class CustomSelect extends \XLite\View\FormField\Select\Regular
{
    protected function getDefaultOptions()
    {
        return array(
            'First value'  => static::t('First value'),
            'Second value' => static::t('Second value'),
            'Third value'  => static::t('Third value'),
        );
    }
}

This is an implementation of simple select-box class. We just define options of this select-box in the getDefaultOptions() method. Our select-box will have three options: First value, Second value and Third value.

In order to display this setting widget on our page, we go to the <X-Cart>/skins/admin/en/modules/Tony/FormField/page/form_field/body.tpl template and add the following code there: 

<widget class="\XLite\Module\Tony\FormFieldDemo\View\FormField\Select\CustomSelect" />

If we leave the code like this, X-Cart will display this setting with missed label: 

Instead, we can either define this setting to be displayed without label: 

<widget class="\XLite\Module\Tony\FormFieldDemo\View\FormField\Select\CustomSelect" fieldOnly="true" />

or define some label for it: 

<widget class="\XLite\Module\Tony\FormFieldDemo\View\FormField\Select\CustomSelect" label="Some label" />

Our module pack will contain both variants.

Result

Now we re-deploy the store and once it is finished we go to admin.php?target=form_field page in order to check the result. You should something like this: 

Module pack

You can download an example of this module from here: https://dl.dropboxusercontent.com/u/23858825/Tony-FormFieldDemo-v5_1_0.tar

Attachments: