Jump to content

Edit History

Paul C

Paul C


added link to docs

The hook additionalCustomerFormFields is sent the array of the field definitions, so you can manipulate them in there and never have to worry about files getting updated and having to mess with core changes and overrides. There's an equivalent for most forms.

Here's an example module that uses this process to change a field name (but you can do the same with the setRequired() method too, of course). This specific module is intended to target Prestashop 8.0.0 onwards and PHP 8.1. May need to be modified for earlier versions, but the principle is supported from 1.7 onwards. You may need to remove declare(strict_types=1) and the function return types if you're using an older version of PHP.

Documentation can be found here (PrestaShop 😎.

 

<?php
/** 
 * Module Name: formlabelmangler
 * Description: Example code to show how to modify field labels
 * Version: 8.0.0
 * @author: Paul Campbell [https://www.prestashop.com/forums/profile/11264-paul-c/] 
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
declare(strict_types=1);

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

class formlabelmangler extends Module
{   
    /** @var array */
    private $hooks;

    /**
     * @param string|null $name (Deprecated parameter)
     * @param Context|null $context
     * @return void
     */
    public function __construct()
    {
        $this->name = 'formlabelmangler';
        $this->tab = 'front_office_features';
        $this->author = 'PaulC';
        $this->version = '8.0.0';

        parent::__construct();

        $this->displayName = $this->trans('Fun with forms and hooks.', [], 'Modules.formlabelmangler.Admin');
        $this->description = $this->trans('Modify field names on customer registration form.', [], 'Modules.formlabelmangler.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);

        // Hooks to install to
        $this->hooks =  [
            'additionalCustomerFormFields'
        ];
    }

    /**
     * Install and hook module.
     * @return bool True on success, False if an error is encountered
     */
    public function install()
    {
        return 
            parent::install() &&
            $this->registerHook($this->hooks);
    }

    /**
     * Respond whether the module uses the new translation system.
     * 
     * @return bool
     */
    public function isUsingNewTranslationSystem(): bool
    {
        return true;
    }

    /**
     * Inject additional customer registration fields and/or manipulate existing
     * Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true)
     * @see https://github.com/PrestaShop/PrestaShop/blob/develop/classes/form/FormField.php
     * 
     * @param array FormField &$arguments['fields'] any additional data
     * @return array The field definitions to be rendered
     * 
     */
    public function hookAdditionalCustomerFormFields(array $arguments) : array
    {
        // The current fields which are set can be retrieved with the following
        // since the data is passed by reference.
        $fields = $arguments['fields'];

        // Uncomment the following if you want to record the current field details in the
        // PHP error log for reference.
        
        //error_log('We got ourselves some fields to play in: '. print_r($fields, true));

        // In this example we want to spruce up the 'email' field and call it "Email Address"
        // instead of just plain old "Email".
        foreach ($fields as $field) {
            if ($field->getName() == 'email') {
                $field->setLabel(
                    $this->trans(
                        'Email Address', [], 'Shop.Forms.Labels'
                    ));
            }
        }

        // This is where you would return any additional fields you want to create
        return [];
    }
}

 

Paul C

Paul C

The hook additionalCustomerFormFields is sent the array of the field definitions, so you can manipulate them in there and never have to worry about files getting updated and having to mess with core changes and overrides. There's an equivalent for most forms.

Here's an example module that uses this process to change a field name (but you can do the same with the setRequired() method too, of course). This specific module is intended to target Prestashop 8.0.0 onwards and PHP 8.1. May need to be modified for earlier versions, but the principle is supported from 1.7 onwards. You may need to remove declare(strict_types=1) and the function return types if you're using an older version of PHP.

 

<?php
/** 
 * Module Name: formlabelmangler
 * Description: Example code to show how to modify field labels
 * Version: 8.0.0
 * @author: Paul Campbell [https://www.prestashop.com/forums/profile/11264-paul-c/] 
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
declare(strict_types=1);

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

class formlabelmangler extends Module
{   
    /** @var array */
    private $hooks;

    /**
     * @param string|null $name (Deprecated parameter)
     * @param Context|null $context
     * @return void
     */
    public function __construct()
    {
        $this->name = 'formlabelmangler';
        $this->tab = 'front_office_features';
        $this->author = 'PaulC';
        $this->version = '8.0.0';

        parent::__construct();

        $this->displayName = $this->trans('Fun with forms and hooks.', [], 'Modules.formlabelmangler.Admin');
        $this->description = $this->trans('Modify field names on customer registration form.', [], 'Modules.formlabelmangler.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);

        // Hooks to install to
        $this->hooks =  [
            'additionalCustomerFormFields'
        ];
    }

    /**
     * Install and hook module.
     * @return bool True on success, False if an error is encountered
     */
    public function install()
    {
        return 
            parent::install() &&
            $this->registerHook($this->hooks);
    }

    /**
     * Respond whether the module uses the new translation system.
     * 
     * @return bool
     */
    public function isUsingNewTranslationSystem(): bool
    {
        return true;
    }

    /**
     * Inject additional customer registration fields and/or manipulate existing
     * Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true)
     * @see https://github.com/PrestaShop/PrestaShop/blob/develop/classes/form/FormField.php
     * 
     * @param array FormField &$arguments['fields'] any additional data
     * @return array The field definitions to be rendered
     * 
     */
    public function hookAdditionalCustomerFormFields(array $arguments) : array
    {
        // The current fields which are set can be retrieved with the following
        // since the data is passed by reference.
        $fields = $arguments['fields'];

        // Uncomment the following if you want to record the current field details in the
        // PHP error log for reference.
        
        //error_log('We got ourselves some fields to play in: '. print_r($fields, true));

        // In this example we want to spruce up the 'email' field and call it "Email Address"
        // instead of just plain old "Email".
        foreach ($fields as $field) {
            if ($field->getName() == 'email') {
                $field->setLabel(
                    $this->trans(
                        'Email Address', [], 'Shop.Forms.Labels'
                    ));
            }
        }

        // This is where you would return any additional fields you want to create
        return [];
    }
}

 

Paul C

Paul C

The hook additionalCustomerFormFields is sent the array of the field definitions, so you can manipulate them in there and never have to worry about files getting updated and having to mess with core changes and overrides. There's an equivalent for most forms.

Here's an example module that uses this process to change a field name (but you can do the same with the setRequired() method too, of course).

 

<?php
/** 
 * Module Name: formlabelmangler
 * Description: Example code to show how to modify field labels
 * Version: 8.0.0
 * @author: Paul Campbell [https://www.prestashop.com/forums/profile/11264-paul-c/] 
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
declare(strict_types=1);

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

class formlabelmangler extends Module
{   
    /** @var array */
    private $hooks;

    /**
     * @param string|null $name (Deprecated parameter)
     * @param Context|null $context
     * @return void
     */
    public function __construct()
    {
        $this->name = 'formlabelmangler';
        $this->tab = 'front_office_features';
        $this->author = 'PaulC';
        $this->version = '8.0.0';

        parent::__construct();

        $this->displayName = $this->trans('Fun with forms and hooks.', [], 'Modules.formlabelmangler.Admin');
        $this->description = $this->trans('Modify field names on customer registration form.', [], 'Modules.formlabelmangler.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);

        // Hooks to install to
        $this->hooks =  [
            'additionalCustomerFormFields'
        ];
    }

    /**
     * Install and hook module.
     * @return bool True on success, False if an error is encountered
     */
    public function install()
    {
        return 
            parent::install() &&
            $this->registerHook($this->hooks);
    }

    /**
     * Respond whether the module uses the new translation system.
     * 
     * @return bool
     */
    public function isUsingNewTranslationSystem(): bool
    {
        return true;
    }

    /**
     * Inject additional customer registration fields and/or manipulate existing
     * Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true)
     * @see https://github.com/PrestaShop/PrestaShop/blob/develop/classes/form/FormField.php
     * 
     * @param array FormField &$arguments['fields'] any additional data
     * @return array The field definitions to be rendered
     * 
     */
    public function hookAdditionalCustomerFormFields(array $arguments) : array
    {
        // The current fields which are set can be retrieved with the following
        // since the data is passed by reference.
        $fields = $arguments['fields'];

        // Uncomment the following if you want to record the current field details in the
        // PHP error log for reference.
        
        //error_log('We got ourselves some fields to play in: '. print_r($fields, true));

        // In this example we want to spruce up the 'email' field and call it "Email Address"
        // instead of just plain old "Email".
        foreach ($fields as $field) {
            if ($field->getName() == 'email') {
                $field->setLabel(
                    $this->trans(
                        'Email Address', [], 'Shop.Forms.Labels'
                    ));
            }
        }

        // This is where you would return any additional fields you want to create
        return [];
    }
}

 

×
×
  • Create New...