在帮助表单 Prestashop 中动态添加表单字段

Adding Form field dynamically in helper form Prestashop

我有一个后台管理控制器,我在其中使用 renderform() 中的辅助表单来生成一个表单,它正在工作,但我想要一个功能来动态地在该表单中添加一个字段。

我已经通过 helper list 完成了这个,同时渲染表单我也返回了一个渲染列表,检查 renderAdditionalOptionsList() 用法

public function renderForm() {
    $id_citydelivery = (int) Tools::getValue('id_citydelivery');
    $citydeliveryObj = new CityPortModel($id_citydelivery);
    if (Validate::isLoadedObject($citydeliveryObj)) {
        $_legends = $citydeliveryObj->legend;
        $_legend_array = explode(',', $_legends);
        foreach ($_legend_array as $_legend) {
            $this->fields_value['legend_' . $_legend] = $_legend;
        }
    }
    $temp = $this->fields_value;
    $fields_form_1 = array(
        'form' => array(
            'legend' => array('title' => $this->l('Price and Delivery Destination Manager'), 'icon' => 'icon-cogs'),
            'input' => array(
                array(
                    'type' => 'select',
                    'label' => $this->l('Country'),
                    'name' => 'id_country',
                    'required' => false,
                    'lang' => false,
                    'options' => array(
                        'query' => $this->getCountries(),
                        'id' => 'value',
                        'name' => 'name'
                    )),
                array(
                    'type' => 'text',
                    'label' => $this->l('Portname'),
                    'name' => 'portname',
                    'size' => 255),
                array(
                    'type' => 'hidden',
                    'name' => 'id_citydelivery'
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Per m3'),
                    'name' => 'perm3',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Service charges'),
                    'name' => 'servicecharges',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Insurance'),
                    'name' => 'insurance',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Inspection'),
                    'name' => 'inspection',
                    'size' => 255),
                array(
                    'type' => 'text',
                    'label' => $this->l('Certificate'),
                    'name' => 'certificate',
                    'size' => 255),
                array(
                    'type' => 'checkbox',
                    'label' => $this->l('Service legend'),
                    'name' => 'legend',
                    'required' => false,
                    'lang' => false,
                    'values' => array(
                        'query' => $this->getLegends(),
                        'id' => 'value',
                        'name' => 'name'
                    )),
                array(
                    'type' => 'switch',
                    'label' => $this->l('Active'),
                    'name' => 'active',
                    'required' => false,
                    'is_bool' => true,
                    'values' => array(array(
                            'id' => 'active_on',
                            'value' => 1,
                            'label' => $this->l('Active')), array(
                            'id' => 'active_off',
                            'value' => 0,
                            'label' => $this->l('Inactive')))),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'type' => 'submit'
            ),
            'buttons' => array(
                array(
                    'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite($this->name),
                    'title' => $this->l('Cancle'),
                    'icon' => 'process-icon-cancel'
                )
            )
        ),
    );
    $helper = new HelperForm();
    $helper->show_toolbar = false;
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->toolbar_scroll = true;
    $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $this->fields_form = array();
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitAddcitydelivery';
    $helper->currentIndex = AdminController::$currentIndex;
    $helper->token = Tools::getAdminTokenLite($this->name);
    $helper->tpl_vars = array(
        'fields_value' => $this->getFormValues($citydeliveryObj, $temp),
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id
    );
    $_1 = $helper->generateForm(array($fields_form_1));
    $return = $_1;
    if ($this->display != 'add') {
        $return .= $this->renderAdditionalOptionsList($id_citydelivery);
    }
    return $return;
}

public function initPageHeaderToolbar() {
    if ($this->display == 'edit' || $this->display == 'add') {
        $this->page_header_toolbar_btn['back_to_list'] = array(
            'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true),
            'desc' => $this->l('Back to list', null, null, false),
            'icon' => 'process-icon-back'
        );
    }
    parent::initPageHeaderToolbar();
}

function getFormValues($citydeliveryObj, $temp) {
    $arrTwo = array(
        'id_citydelivery' => Tools::getValue('id_citydelivery', $citydeliveryObj->id_citydelivery),
        'id_country' => Tools::getValue('id_country', $citydeliveryObj->id_country),
        'portname' => Tools::getValue('portname', $citydeliveryObj->portname),
        'perm3' => Tools::getValue('perm3', $citydeliveryObj->perm3),
        'servicecharges' => Tools::getValue('servicecharges', $citydeliveryObj->servicecharges),
        'insurance' => Tools::getValue('insurance', $citydeliveryObj->insurance),
        'inspection' => Tools::getValue('inspection', $citydeliveryObj->inspection),
        'certificate' => Tools::getValue('certificate', $citydeliveryObj->certificate),
        'active' => Tools::getValue('active', $citydeliveryObj->active),
    );
    return array_merge($temp, $arrTwo);
}

protected function renderAdditionalOptionsList($id_citydelivery) {
    $this->fields_list = array(
        'service_name' => array(
            'width' => 'auto',
            'orderby' => false,
            'title' => $this->l('Service Name'),
            'type' => 'text',
            'search' => false,
        ),
        'service_desc' => array(
            'type' => 'text',
            'title' => $this->l('Service Description'),
            'search' => false,
            'orderby' => false,
        ),
        'charge' => array(
            'title' => $this->l('Service Charge'),
            'type' => 'text',
            'search' => false,
            'orderby' => false,
        ),
        'active' => array(
            'title' => $this->l('Status'),
            'active' => 'status',
            'type' => 'bool',
            'search' => false,
            'orderby' => false
        )
    );
    $linkToAdditionServiceController = Context::getContext()->link->getAdminLink('AdminAdditionalService', false);
    $helperList = new HelperList();
    $helperList->table = 'additional_service';
    $helperList->shopLinkType = '';
    $helperList->simple_header = false; //For showing add and refresh button
    $helperList->identifier = 'id_additional_service';
    $helperList->actions = array('edit', 'delete');
    $helperList->show_toolbar = false;
    $helperList->toolbar_btn['new'] = array(
        'href' => $linkToAdditionServiceController . '&addadditional_service&id_citydelivery=' . $id_citydelivery . '&token=' . Tools::getAdminTokenLite('AdminAdditionalService'),
        'desc' => $this->l('Add new')
    );
    $helperList->title = "Additional Option Manager";
    $helperList->currentIndex = $linkToAdditionServiceController;
    $helperList->token = Tools::getAdminTokenLite('AdminAdditionalService');
    $content = $this->getListContent($id_citydelivery);
    $helperList->listTotal = count($content);
    return $helperList->generateList($content, $this->fields_list);
}