cancel
Showing results for 
Search instead for 
Did you mean: 

Custom block is not showed in header.panel

Custom block is not showed in header.panel

Stumbled on this issue:
I need to add a block which switches between Web-pages in a DropDown form.

I am using a custom theme here. Block must be located at this side LD0Qy

But the problem is, that when I trying to add a custom block to the container, it's not showing up.

<referenceContainer name="header.panel">
    <block class="<Vendor>\<Module>\Block\Theme\Header\Lang" name="lang_switch" before="header.links" template="<Vendor>_<Module>::language.phtml"/>
</referenceContainer>

What I tried:

  1. Add this to app/design/frontend/<Vendor>/<Module>/Magento_Theme/layout/defalt.xml
  2. Add this to app/code/<Vendor>/<Module>/view/frontend/layout/default.xml

For knowing what content of blocks:

app/code/<Vendor>/<Module>/Block/Theme/Header/Lang.php:

<?php

namespace <Vendor>\<Module>\Block\Theme\Header;

class Lang extends \Magento\Framework\View\Element\Template {

    public function __construct(\Magento\Framework\View\Element\Template\Context $context)
    {
        parent::__construct($context);
    }

    public function render() {

        $label = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('mysetting/header_lang/hl_label');
        $s_data = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('mysetting/header_lang/hl_data');

        $data = array_map(function($el){
            $d = explode("|",$el);
            return array("order"=>intval($d[0]),"label"=>$d[1],"link"=>$d[2]);
        },explode("\n",$s_data));

        usort($data, fn($a,$b) => $a->order - $b->order );

        $html = '<div class="example-dropdown-2"><span class="action toggle" data-mage-init=\'{"dropdown":{}}\' data-toggle="dropdown" aria-haspopup="true"><span>'.$label.'</span></span><ul class="dropdown">';

        foreach ($data as $d){
            $html .= '<li> <a href="'.$d->link.'">'.$d->label.'</a></li>';
        }
        $html .= '</ul></div>';

        return $html;
    }
}

app/code/<Vendor>/<Module>/view/frontend/templates/language.phtml:

<?php
/**
 * @var \<Vendor>\<Module>\Block\Theme\Header\Lang $block
 */
echo $block->render();

Data structure of configs:

hl_label: text
  example.com

hl_data: textarea
  0|ExampleNET|https://example.net
  1|ExampleORG|https://example.org
  2|ExampleIT|https://example.it

Any help with it?
Using:

  • Magento: 2.4.4
  • Server: CentOS
  • Installation: docker\bitnami\magento Docker
  • PHP: 8.1.9
  • MySQL: 8.0.30
1 REPLY 1

Re: Custom block is not showed in header.panel

Hello @mykolakike6a30 

 

Please follow the steps below:

  • Open default.phtml file. (Path: app/design/frontend/<Vendor>/<Module>/Magento_Theme/layout/defalt.xml)
  • Please add the following code before <body> end:
    <move element="lang_switch" destination="header.panel" after="-"/>

 

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9