Jump to content

The hook "Header" is deprecated, please use "displayHeader"


watou

Recommended Posts

Hello, I've upgraded to PS 8.0.1 and my Apache error log is flooded with messages like

PHP message: PHP Deprecated:  The hook "Header" is deprecated, please use "displayHeader" instead in module "severalmodules". in /var/www/html.com/public_html/classes/Hook.php on line 850

What is the correct solution? I've tried to relocate the hook for some modules, but some modules do not let me do so via Design -> Positions in the Back Office. For some modules, displayHeader is not listed as a choicce. For others, I did change the hook to displayHeader but the deprecation warning is still being logged. 

I'd even be willing to simply suppress all deprecated messages for this production site, but obviously fixing the root cause is preferable.

Many thanks for any guidance!

Link to comment
Share on other sites

  • 3 weeks later...

Hi,

you can find everything in administration => Design => Positions

In the search field "Search for a hook" write header and check the option "Display non-positionable hooks"

Find Header in the list and you will see modules that use the old Header hook.

obrazek.thumb.png.9d1d481e504cd43b4ef60748bd6a1f80.png

obrazek.thumb.png.85bcfb41712b6893c491207ec88651f1.png

 

In the main module file (folder name = module name + .php) you can find the Header hook and change it to displayHeader, including the function.
After this change, the module hook will need to be registered again.

You can do it again in Design => Position ...

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

35 minutes ago, ps8moduly.cz said:

Hi,

you can find everything in administration => Design => Positions

In the search field "Search for a hook" write header and check the option "Display non-positionable hooks"

Find Header in the list and you will see modules that use the old Header hook.

obrazek.thumb.png.9d1d481e504cd43b4ef60748bd6a1f80.png

obrazek.thumb.png.85bcfb41712b6893c491207ec88651f1.png

 

In the main module file (folder name = module name + .php) you can find the Header hook and change it to displayHeader, including the function.
After this change, the module hook will need to be registered again.

You can do it again in Design => Position ...

Thanks for your help, I found the file in the module and it appears to already be using displayHeader, yet it does show up as using header in the display non-positionable hooks.

I also cant see the option to re register the hook.

here is the ps_shopping_cart.php file it appears to be correct to me.

<?php
/**
 * 2007-2020 PrestaShop and Contributors
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * @author    PrestaShop SA <[email protected]>
 * @copyright 2007-2020 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 * International Registered Trademark & Property of PrestaShop SA
 */

use PrestaShop\PrestaShop\Adapter\Cart\CartPresenter;

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

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class Ps_Shoppingcart extends Module implements WidgetInterface
{
    /**
     * @var string Name of the module running on PS 1.6.x. Used for data migration.
     */
    const PS_16_EQUIVALENT_MODULE = 'blockcart';

    public function __construct()
    {
        $this->name = 'ps_shoppingcart';
        $this->tab = 'front_office_features';
        $this->version = '2.0.6';
        $this->author = 'PrestaShop';
        $this->need_instance = 0;

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->trans('Shopping cart', [], 'Modules.Shoppingcart.Admin');
        $this->description = $this->trans('Display a shopping cart icon on your pages and the number of items it contains.', [], 'Modules.Shoppingcart.Admin');
        $this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
        $this->controllers = ['ajax'];
    }

    /**
     * @return void
     */
    public function hookDisplayHeader()
    {
        if (Configuration::isCatalogMode()) {
            return;
        }

        if (Configuration::get('PS_BLOCK_CART_AJAX')) {
            $this->context->controller->registerJavascript('modules-shoppingcart', 'modules/' . $this->name . '/ps_shoppingcart.js', ['position' => 'bottom', 'priority' => 150]);
        }
    }

    /**
     * @return string
     */
    private function getCartSummaryURL()
    {
        return $this->context->link->getPageLink(
            'cart',
            null,
            $this->context->language->id,
            [
                'action' => 'show',
            ],
            false,
            null,
            true
        );
    }

    /**
     * @param string|null $hookName
     * @param array<string,mixed> $params
     *
     * @return array<string,mixed>
     */
    public function getWidgetVariables($hookName, array $params)
    {
        $cart_url = $this->getCartSummaryURL();

        return [
            'cart' => (new CartPresenter())->present(isset($params['cart']) ? $params['cart'] : $this->context->cart),
            'refresh_url' => $this->context->link->getModuleLink('ps_shoppingcart', 'ajax', [], null, null, null, true),
            'cart_url' => $cart_url,
        ];
    }

    /**
     * @param string|null $hookName
     * @param array<string,mixed> $params
     *
     * @return string
     */
    public function renderWidget($hookName, array $params)
    {
        if (Configuration::isCatalogMode()) {
            return '';
        }

        $this->smarty->assign($this->getWidgetVariables($hookName, $params));

        return $this->fetch('module:ps_shoppingcart/ps_shoppingcart.tpl');
    }

    /**
     * @param Cart $cart
     * @param int $id_product
     * @param int $id_product_attribute
     * @param int $id_customization
     *
     * @return string
     *
     * @throws Exception
     */
    public function renderModal(Cart $cart, $id_product, $id_product_attribute, $id_customization)
    {
        $data = (new CartPresenter())->present($cart);
        $product = null;
        foreach ($data['products'] as $p) {
            if ((int) $p['id_product'] == $id_product &&
                (int) $p['id_product_attribute'] == $id_product_attribute &&
                (int) $p['id_customization'] == $id_customization) {
                $product = $p;
                break;
            }
        }

        $this->smarty->assign([
            'product' => $product,
            'cart' => $data,
            'cart_url' => $this->getCartSummaryURL(),
        ]);

        return $this->fetch('module:ps_shoppingcart/modal.tpl');
    }

    /**
     * @return string
     */
    public function getContent()
    {
        $output = '';
        if (Tools::isSubmit('submitBlockCart')) {
            $ajax = Tools::getValue('PS_BLOCK_CART_AJAX');
            if ($ajax != 0 && $ajax != 1) {
                $output .= $this->displayError($this->trans('Ajax: Invalid choice.', [], 'Modules.Shoppingcart.Admin'));
            } else {
                Configuration::updateValue('PS_BLOCK_CART_AJAX', (int) ($ajax));
                Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->name . '&conf=6');
            }
        }

        return $output . $this->renderForm();
    }

    /**
     * @return bool
     */
    public function install()
    {
        $this->uninstallPrestaShop16Module();

        return
            parent::install()
                && $this->registerHook('displayHeader')
                && $this->registerHook('displayNav2')
                && Configuration::updateValue('PS_BLOCK_CART_AJAX', 1);
    }

    /**
     * Migrate data from 1.6 equivalent module (if applicable), then uninstall
     *
     * @return bool
     */
    public function uninstallPrestaShop16Module()
    {
        if (!Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) {
            return false;
        }
        $oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE);
        if ($oldModule) {
            // This closure calls the parent class to prevent data to be erased
            // It allows the new module to be configured without migration
            $parentUninstallClosure = function () {
                return parent::uninstall();
            };
            $parentUninstallClosure = $parentUninstallClosure->bindTo($oldModule, get_class($oldModule));
            $parentUninstallClosure();
        }

        return true;
    }

    /**
     * @return string
     */
    public function renderForm()
    {
        $fields_form = [
            'form' => [
                'legend' => [
                    'title' => $this->trans('Settings', [], 'Admin.Global'),
                    'icon' => 'icon-cogs',
                ],
                'input' => [
                    [
                        'type' => 'switch',
                        'label' => $this->trans('Ajax cart', [], 'Modules.Shoppingcart.Admin'),
                        'name' => 'PS_BLOCK_CART_AJAX',
                        'is_bool' => true,
                        'desc' => $this->trans('Activate Ajax mode for the cart (compatible with the default theme).', [], 'Modules.Shoppingcart.Admin'),
                        'values' => [
                            [
                                'id' => 'active_on',
                                'value' => 1,
                                'label' => $this->trans('Yes', [], 'Admin.Global'),
                            ],
                            [
                                'id' => 'active_off',
                                'value' => 0,
                                'label' => $this->trans('No', [], 'Admin.Global'),
                            ],
                        ],
                    ],
                ],
                'submit' => [
                    'title' => $this->trans('Save', [], 'Admin.Actions'),
                ],
            ],
        ];

        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $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;

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitBlockCart';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab
        . '&module_name=' . $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->tpl_vars = [
            'fields_value' => $this->getConfigFieldsValues(),
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        ];

        return $helper->generateForm([$fields_form]);
    }

    /**
     * @return bool[]
     */
    public function getConfigFieldsValues()
    {
        return [
            'PS_BLOCK_CART_AJAX' => (bool) Tools::getValue('PS_BLOCK_CART_AJAX', Configuration::get('PS_BLOCK_CART_AJAX')),
        ];
    }
}

 

Link to comment
Share on other sites

7 minutes ago, ps8moduly.cz said:

And how is it in the positions?

After the upgrade, it is necessary to find out the positions in which hooks the module is registered.
If it uses displayHeader and is also in the header, the header can be removed.

 

I got it to upgrade to 2.0.7, i discovered distribution api client wasnt enabled, so did all ps updates after enabling, but it still displays in header section of positions, see attached pic

 

position-hooks.png

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

Contact the module developers of all theese modules asking them to update their module removing header hook.
I did this and discovered that there's a lot of developers unware that header hook has been deprecated and replaced by displayHeader.

Link to comment
Share on other sites

  • 3 weeks later...
On 20/3/2023 at 8:03, dragon_sa said:

Gracias por su ayuda, encontré el archivo en el módulo y parece que ya está usando displayHeader, pero aparece como usando el encabezado en los ganchos no posicionables de la pantalla.

Tampoco puedo ver la opción para volver a registrar el gancho.

aquí está el archivo ps_shopping_cart.php que me parece correcto.



 

  
    


 

     

    
     

     
         
 
          
    

    
     
    
          
            
        

                   
        
    

    
     
    
        
            
            
            
                  
            
            
            
            
        
    

    
     
    

         
                 
                   
             
        
    

    
     
    
          
             
        


        
    

    
     
       
           
              
                 
                  
                
            
        

             
             
             
        

        
    

    
     
     
           
                   
              
                  
                    
            
        

        
    

    
     
    

        
                
                
                  
    

    
     
    
          
             
         
          
            
               
                
            
        

         
    

    
     
     
              
                  
                       
                      
                
                  
                    
                          
                           
                          
                          
                           
                          
                            
                                  
                                  
                                   
                            
                            
                                  
                                  
                                   
                            
                        
                    
                
                  
                       
                
            
        
           
           
             
             
             
        

        
    

    
     
    
         
                
        
    

 

Hello, In the end, how do I solve this error in the Prestashop Checkout? I have the same error on PS 8.0.4 with PS Checkout v8.3.2.0. I managed to fix several such errors related to a custom theme. But in this payment module I don't know what to play. I'd appreciate your help.

Link to comment
Share on other sites

  • 4 months later...
On 6/9/2023 at 3:59 PM, rubiotel said:

Hello, In the end, how do I solve this error in the Prestashop Checkout? I have the same error on PS 8.0.4 with PS Checkout v8.3.2.0. I managed to fix several such errors related to a custom theme. But in this payment module I don't know what to play. I'd appreciate your help.

Hello,

You need to identify the modules that contain the deprecated hook. You can then replace it with its new version in the module's PHP file. The hook changes from 'Header' to 'displayHeader.' You can use ctrl + f to find occurrences of the hook in the file. Once it's correctly modified following the conventions, you should reset the module for the hook change to take effect.

I hope I've been clear. Have a great day.

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...