Php Prestashop 1.6-将自定义字段添加到类别

Php Prestashop 1.6-将自定义字段添加到类别,php,prestashop,prestashop-1.6,Php,Prestashop,Prestashop 1.6,我想知道如何将自定义字段添加到类别中,以及如何在后台进行编辑(在“说明”字段下)。 我想添加的字段是namedescription\u long 字段类型为TEXT 我已经覆盖了前台,我的字段显示得很好 override\classes\Category.php <?php class Category extends CategoryCore { public $description_long; /** * @see ObjectModel::$def

我想知道如何将自定义字段添加到类别中,以及如何在后台进行编辑(在“说明”字段下)。 我想添加的字段是name
description\u long

字段类型为
TEXT

我已经覆盖了前台,我的字段显示得很好

override\classes\Category.php

<?php 

class Category extends CategoryCore
{

    public $description_long;

    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'category',
        'primary' => 'id_category',
        'multilang' => true,
        'multilang_shop' => true,
        'fields' => array(
            'nleft' =>              array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
            'nright' =>             array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
            'level_depth' =>        array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
            'active' =>             array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
            'id_parent' =>          array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
            'id_shop_default' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'is_root_category' =>   array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'position' =>           array('type' => self::TYPE_INT),
            'date_add' =>           array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'date_upd' =>           array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            // Lang fields
            'name' =>               array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
            'link_rewrite' =>       array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
            'description' =>        array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
            'description_long' =>   array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'), // CUSTOM FIELD
            'meta_title' =>         array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
            'meta_description' =>   array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
            'meta_keywords' =>      array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
        ),
    );
}

要在后台添加字段,您需要覆盖AdminCategoriesController,确切地说是函数renderForm(),并在其中添加新字段。
要实现这一点,请在/override/controllers/admin/下创建一个新文件AdminCategoriesController,然后在其中声明原始控制器的扩展名,并在renderForm函数中从原始核心文件(完全)复制

class AdminCategoriesController extends AdminCategoriesControllerCore
{

    public function renderForm()
    {
        ...
    }
}
现在我们必须在几个地方编辑它,首先我们需要在描述下添加新字段,因此在renderForm()中搜索声明'name'=>'description',您将看到它是一个数组列表,每个数组都在描述一个表单字段。在描述数组之后添加新字段:

 array(
                    'type' => 'textarea',
                    'label' => $this->l('Description long'),
                    'name' => 'description_long',
                    'lang' => true,
                    'autoload_rte' => true,
                    'hint' => $this->l('Invalid characters:').' <>;=#{}',
                ),
数组(
'type'=>'textarea',
'label'=>$this->l('Description long'),
“名称”=>“说明”,
“lang”=>对,
“自动加载”=>正确,
'hint'=>$this->l('无效字符:')。;={},
),
此声明要求Prestashop使用以下规范创建新字段:

  • 文本区域字段
  • 多语言领域
  • 要编辑的javascript插件

  • 名称“description_long”

通过以这种方式声明一个字段,我们将允许prestashop像处理任何其他类属性一样处理它,因此不需要我们在数据库中添加和更新字段


现在在我们的renderForm()函数上还有最后一件事要做,现在最后一条指令是
parent::renderForm()
,它在原始类中调用AdminController要求它呈现表单,但是现在,由于我们正在扩展这个类,该指令正在调用我们的父类AdminCategoriesControllerCore,覆盖我们的所有工作并显示默认表单。为了避免这种情况,请将
parent::renderForm
更改为
AdminController::renderForm()
,以明确对感兴趣的类的调用。

在重写类的
\u构造中添加此行

public function __construct($id_category = null, $id_lang = null, $id_shop = null)
    {

self::$definition['fields']['description_long'] = array('type' => self::TYPE_HTML, 'lang' => true);

parent::__construct($id_category, $id_lang, $id_shop);

}
其中
description\u long
是您的新字段名。

对于在这里挣扎的任何人,都有一个完整的答案:

要向Prestashop类别中的类别添加新的描述字段,需要执行3个步骤:

  • 更新数据库
  • 将名为description_long的字段添加到category_lang表中,可以模拟description列的特征

  • 覆盖类别类
  • 使用以下代码在此处/override/classes/Category.php创建一个文件:

    class Category extends CategoryCore
    {
    
        public $description_long;
    
        public function __construct($id_category = null, $id_lang = null, $id_shop = null){
            self::$definition['fields']['description_long'] = array('type' => self::TYPE_HTML, 'lang' => true);
            parent::__construct($id_category, $id_lang, $id_shop);
        }
    
    }
    
  • 覆盖AdminCategoriesController核心类
  • 使用以下代码在这里/override/controllers/admin/AdminCategoriesController.php创建一个文件:

    class AdminCategoriesController extends AdminCategoriesControllerCore{
    
    
        public function renderForm()
        {
            $this->fields_form_override =array(
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Description long'),
                    'name' => 'description_long',
                    'lang' => true,
                    'autoload_rte' => true,
                    'hint' => $this->l('Invalid characters:').' <>;=#{}',
                ),
            );
    
            return parent::renderForm();
        }
    }
    
    类AdminCategoriesController扩展AdminCategoriesController核心{
    公共函数renderForm()
    {
    $this->fields\u form\u override=array(
    排列(
    'type'=>'textarea',
    'label'=>$this->l('Description long'),
    “名称”=>“说明”,
    “lang”=>对,
    “自动加载”=>正确,
    'hint'=>$this->l('无效字符:')。;={},
    ),
    );
    返回父::renderForm();
    }
    }
    
    工作起来很有魅力!谢谢这是非常误导任何人以外的OP!答案的三分之一在这里,另三分之一在OP中,最后一部分(db更新)留给任何人猜测,这比公认的答案更有帮助。谢谢!回答得很好。我不知道字段\u form\u override是从AdminController父类继承的,所以我总是重写整个方法。。好!!非常感谢。我注意到这会在页面末尾添加新字段。如果有一种方法可以对字段进行重新排序,或者换句话说,将新字段放在某个特定的位置,我会做得更好。我在prestashop 1.7中也做过同样的事情。一切都很好,就像向数据库添加值一样!工作正常。只是问题是编辑案例时字段中没有显示值!