Skip to content

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
Where block must be located

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>BlockThemeHeaderLang" 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>BlockThemeHeader;

class Lang extends MagentoFrameworkViewElementTemplate {

    public function __construct(MagentoFrameworkViewElementTemplateContext $context)
    {
        parent::__construct($context);
    }

    public function render() {

        $label = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoFrameworkAppConfigScopeConfigInterface')->getValue('mysetting/header_lang/hl_label');
        $s_data = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoFrameworkAppConfigScopeConfigInterface')->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>BlockThemeHeaderLang $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: Bitnami Docker
  • PHP: 8.1.9
  • MySQL: 8.0.30