How to move top link into container magento2 năm 2024

I know this is old, but I landed while searching for the same issue, so figured my solution might help someone else. This is for 2.4.1.

In our custom theme, I had to move top.search into the custom header block (in app/design/frontend/[theme_name]/default/Magento_Theme/layout/default.xml):

That custom.header block had it's own .phtml template file (app/design/frontend/[theme_name]/default/Magento_Theme/templates/html/custom-header.phtml) where I added this code to get it to show up in the right place. Note that we have to use topSearch here, and not top.search (I have no idea why the name changes from top.search):

This article features a step-by-step illustration of how a real-life layout customization task is performed. Namely, it illustrates how to change the layout of customer account links in a store page header.


In their Orange theme, ExampleCorp wants to transform the header links block to a drop-down, the way it is done in the Luma theme:

How to move top link into container magento2 năm 2024

To do this, they need to wrap the list of header links with a container and add a greeting with a drop-down arrow before the list.

The Orange theme inherits from Blank, so by default the rendered header links look like following:

Copied to your clipboard

...
    
    
  • ...
  •     
  • ...
  •     
    
        
    
    
    
    ...
    

    The markup required for the drop-down is the following:

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    Step 1: Define the layout blocks

    ExampleCorp applies the Luma theme. Using the approach described in Locate templates, layouts, and styles they find out that the original block responsible for displaying the header links is defined in

    /view/frontend/layout/default.xml:

    Copied to your clipboard

    ...
    
    
    
        
    
            header links
    
        
    
    
    

    (See on GitHub).

    Other modules use this block to add their specific links to the header using the instruction. For example, see how links are added in the Customer module:

    The Luma theme the

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    0 block to the new

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    1 block in the extending layout file.

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    2

    Copied to your clipboard

    
    
    ...
    

    ...

    ...

    The links that should be in header, but outside the drop-down menu are added in the new

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    3 block (

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    4):

    Copied to your clipboard

    
    
        
    
            header links
    
        
    
    
    

    Step 2: Define the templates

    Similar to the way they defined the layout on the previous step, ExampleCorp defines the template which is used as the drop-down container :

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    5.

    Copied to your clipboard

    customerLoggedIn()): ?>

  •     
              role="link"
    
              tabindex="0"
    
              data-mage-init='{"dropdown":{}}'
    
              data-toggle="dropdown"
    
              data-trigger-keypress-button="true"
    
              data-bind="scope: 'customer'">
    
            
    
            
                    class="action switch"
    
                    tabindex="-1"
    
                    data-action="customer-menu-toggle">
    
                
    
            
    
        
    
        
    
            getChildHtml()):?>
    
            
                getChildHtml();?>
    
            
    
            
    
        
    
    
    
    See app/code/Magento/Customer/view/frontend/templates/account/customer.phtml.

    Step 3: Extend the base layout to add a block

    ExampleCorp needs to create a new block, say,

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    3, in the

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    7 container, to move the links there. As the links can be added to this list by different modules, it is better to add this block to the

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    8 page configuration of the

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    9 module.

    So the following extending layout is added in the Orange theme:

    Copied to your clipboard

    ...
    
    
    
        
    
            header links
    
        
    
    
    

    0

    Copied to your clipboard

    
    
        
    
            
    
                
    
                    header links
    
                
    
            
    
        
    
    
    

    To move the links to the

    Copied to your clipboard

    ...
    
        
  • ...
  •     
  •         
                  role="link"
    
                  tabindex="0"
    
                  data-mage-init='{"dropdown":{}}'
    
                  data-toggle="dropdown"
    
                  data-trigger-keypress-button="true"
    
                  data-bind="scope: 'customer'">
    
                
    
                
                        class="action switch"
    
                        tabindex="-1"
    
                        data-action="customer-menu-toggle">
    
                    Change
    
                
    
            
    
            
                
                    
  • ...
  •                 
    
                    
    
                
    
            
        
    
        
    
    
    
    ....
    

    3 block, ExampleCorp adds an extending layout:

    Copied to your clipboard

    ...
    
    
    
        
    
            header links
    
        
    
    
    

    2

    Copied to your clipboard

    
    
        
    
            
    
            
    
        
    
        
    
        
    
        
    
    
    

    Now the customer links look like following:

      
    
          ![](https://https://i0.wp.com/developer.adobe.com/commerce/frontend-core/static/e590cfe7106124bb27d3cda7d5335086/c852c/layout_screen221.png)
        
    
    
    Clicking the Change button toggles the

    Copied to your clipboard

    ...
    
    
    
        
    
            header links
    
        
    
    
    

    3 CSS class:

    To add quick basic styling and visual behavior to the "dropdown" menu, ExampleCorp added _extend.less to their theme with the following customizations:

      ### How do I add a top link in Magento 2?
    
      Step 1: Add/ modify default.xml file. Find the path app/code/Mageplaza/HelloWorld/view/frontend/layout , then add/ modify default.xml file by: ... .
    
    Step 2: Add Link.php file. Continue with the path app/code/Mageplaza/HelloWorld/Block and you will add the link.php file: ... .

    Step 3: Flush cache and test your Top link result..

    
      ### How do I change the URL in Magento 2?
    
      Configure Your Base URLs in the Magento 2 Admin.
    
    From the top navigation bar, select Stores -> Configuration :.

    From the configuration menu, select General -> Web :.

    Open the secure and unsecure dropdown sections from the page:.

    Now, insert the correct URL configuration in the base_url input box and click on Save Config..

    
      ### How do I edit the footer in Magento?
    
      How to Edit the Footer in Magento 1.6.
    
    Log into your Magento Admin Backend..

    Go to System > Configuration..

    In the left menu pane under “General” select “Design”..

    Click on the “Footer” section in the center of your browser screen to open it..

    Under the “Copyright” section type in your new custom copyright information..

    
      ### How do I add a hyperlink in Magento?
    
      How to add link in Category Menu in Magento 2.
    

    .

    .

    .