Jump to content

Edit History

Paul C

Paul C


fixed typos

On 3/24/2024 at 11:47 AM, Hoda245 said:

same problem here... 

Any proper solution for it???

Depends on how you define "proper" :D

Here's an example module that can rename the registration field labels and make them translatable at the same time.

<root>/modules/formlabelmangler.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 [];
    }
}

This has been tested with 8.1.4 but should be fine for 1.7.0+ php7+

Paul C

Paul C

On 3/24/2024 at 11:47 AM, Hoda245 said:

same problem here... 

Any proper solution for it???

Depends on how you define "proper" :D

Here's an example module that can rename the registration field labels and make them translatable at the same time.

<root>/modules/formlabelmangler.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.', array(), 'Modules:Ecsbrands.Admin');
        $this->description = $this->trans('Modify field names on customer registration form.', array(), 'Modules.Ecsbrands.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 of FormField objects in $arguments['fields']
     * @return array The additional field definitions to be rendered (if any)
     * 
     */
    public function hookAdditionalCustomerFormFields($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...