Jump to content

[SOLVED] List of hooks for admin category edit page? Prestashop 1.7.6


Recommended Posts

Since in 1.7.6 category and category edit page was one of the pages migrated to Symfony I can no longer add custom fields the way I used to in 1.6 without touching the core files (at least not to my knowledge). So I figured I'll budge and use modules and hooks, but I can't find any info on which hooks are actually used on the category edit page. The documentation doesn't mention any and debug only shows the default ones that show everywhere.

Compare that with hooks on product page where we have like 10 hooks to work with and this seems a bit off. Which leads me to the question, did anyone have any luck with finding what, if any, hooks are called on the category edit page that we can use to add our own custom fields etc.?

Edited by direwald (see edit history)
Link to comment
Share on other sites

So I did manage to locate the hooks and add my own custom field. In case someone else is having the same issue, here's my module file that creates a new field on admin category page. The field itself doesn't save or do anything yet, as I haven't gotten around to it yet, but you can use this as a framework I guess.

The hooks that you can use to modify the category page are: 

actionCategoryFormBuilderModifier
actionAfterCreateCategoryFormHandler
actionAfterUpdateCategoryFormHandler

 

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class CustomCategoryFields extends Module {

    public function __construct()
    {
        $this->name = 'customcategoryfields';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'direwald';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
            'min' => '1.7',
            'max' => _PS_VERSION_
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Custom Admin Category Fields');
        $this->description = $this->l('Adds custom fields to admin category page');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

        if (!Configuration::get('customcategoryfields')) {
            $this->warning = $this->l('No name provided');
        }
    }

    public function install()
    {
        return parent::install()
            && $this->_installSql()
            && $this->registerHook('actionCategoryFormBuilderModifier')
            && $this->registerHook('actionAfterCreateCategoryFormHandler')
            && $this->registerHook('actionAfterUpdateCategoryFormHandler');
    }

    public function uninstall()
    {
        if (!parent::uninstall() 
        || !$this->_unInstallSql()
        || !Configuration::deleteByName('customcategoryfields')
        ) {
            return false;
        }

        return true;
    }

    protected function _installSql()
    { 
        $sqlInstallLang = "CREATE TABLE "._DB_PREFIX_."category_custom_fields (
                            id_category int(10) NULL,
                            image varchar(255) NULL)";
 
        $returnSqlLang = Db::getInstance()->execute($sqlInstallLang);
 
        return $returnSqlLang; 
    }


    protected function _unInstallSql()
    { 
         $sqlInstallLang = "DROP TABLE "._DB_PREFIX_."category_custom_fields";
  
         $returnSqlLang = Db::getInstance()->execute($sqlInstallLang);
  
         return $returnSqlLang; 
    }
	
  	/**
     * Modifies category page form
     * @param array $params
     */
    public function hookActionCategoryFormBuilderModifier($params) 
    {
        $formBuilder = $params['form_builder'];
        $formBuilder->add('image', 
            FileType::class, [
            'label' => $this->getTranslator()->trans('Custom image', [], 'Modules.customcategoryfields'),
            'required' => false,
        ]);
    }
	
  	/**
     * Actions after a new category is created
     * @param array $params
     */
    public function hookActionAfterCreateCategoryFormHandler($params)
    {
        $this->updateCategoryFields($params);
    }

	/**
     * Actions after category update
     * @param array $params
     */
    public function hookActionAfterUpdateCategoryFormHandler($params)
    {
        $this->updateCategoryFields($params);
    }
	
  	/**
     * Custom function that handles saving fields into database and whatever else you want to fields to do
     * @param array $params
     */
    public function updateCategoryFields($params)
    {
    }
    
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...