0

I want to know how to add custom link to the header panel that should be manageable from back-end. so that the person trying to change the value can easily change it without any coding knowledge

I have tried it with module and it successfully worked using following code:-

<referenceBlock name="header.links">

 <block class="Learning\Module\Block\Link" name="custom-header-link">
     
            <arguments>
            <argument name="label" xsi:type="string" translate="true">Customer Service: 888-371-4942</argument>
            <argument name="path" xsi:type="string" translate="true">support</argument>
            </arguments>

      </block> 

      
 </referenceBlock>

but i dont know how to manage it from backend. thank you

5
  • you can create static block in admin and call in xml file. Commented Nov 11, 2019 at 11:38
  • You need to create helper file and add both function which I mentioned in answer. Commented Nov 11, 2019 at 11:53
  • Did you get output or still you face issue? Commented Nov 11, 2019 at 12:30
  • no am still looking for ans
    – Pramod
    Commented Nov 11, 2019 at 12:32
  • Did you created helper file and add function? Commented Nov 11, 2019 at 12:32

2 Answers 2

2

You can set xsi:type="helper" and get dynamic value from admin.For that, Follow this below steps for that :

Replace this code in your xml file

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="header.links">
           <block class="Learning\Module\Block\Link" name="custom-header-link">
              <arguments>
                 <argument name="label" xsi:type="helper" helper="Learning\Module\Helper\Data::yourFunctionLabel" translate="true"/>
                 <argument name="path" xsi:type="helper" helper="Learning\Module\Helper\Data::yourFunctionPath" translate="true"/>
              </arguments>
           </block>
        </referenceBlock>
    </body>
</page>

Now, create yourFunctionLabel() and yourFunctionPath() in helper file for set dynamic label and path value.

9
  • give me a min will give try again will let you know within a min.
    – Pramod
    Commented Nov 11, 2019 at 12:33
  • i have tried it but its not working anyways thanks @rohan
    – Pramod
    Commented Nov 11, 2019 at 12:41
  • Can you please post code which you have tried in xml and helper? I tested and then, upload and it's working so. Commented Nov 11, 2019 at 12:42
  • Any error or log printed in var folder? Commented Nov 11, 2019 at 12:52
  • just tell me one thing how to manage and update number from backend.
    – Pramod
    Commented Nov 11, 2019 at 12:53
0

You just need to add 2 methods in Link.php file

Learning\Module\Block\Link.php

and add the method below:

public function getLabel()
{
    return "Customer Service: 888-371-4942"; // You can set configuration field data dynamically here
}

public function getPath()
{
    return "Support"; // You can set configuration field data dynamically here
}

your layout file code should be

<referenceBlock name="header.links">
    <block class="Learning\Module\Block\Link" name="custom-header-link" />
 </referenceBlock>

Hope this will help you!

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.