0

In Magento 2, I can retrieve the logo image url by using this:

public function __construct(\Magento\Theme\Block\Html\Header\Logo $_logo){
  $logUrl = $_logo->getLogoSrc();
}

Is there a way to retrieve logo full directory image path ?

1 Answer 1

0

Step 1: Create a module with the required files. Dolphin_HelloWorld

Step 2: Create block app/code/Dolphin/HelloWorld/Block/HelloWorld.php

You will use a block class of the module Dolphin_HelloWorld, then possibly inject the object of Logo class in the constructor of the module’s block class.

<?php
namespace Dolphin\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_logo;    
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Theme\Block\Html\Header\Logo $logo,
        array $data = []
    )
    {        
        $this->_logo = $logo;
        parent::__construct($context, $data);
    }
    
    /**
     * Get logo image URL
     *
     * @return string
     */
    public function getLogoSrc()
    {    
        return $this->_logo->getLogoSrc();
    }
    
    /**
     * Get logo text
     *
     * @return string
     */
    public function getLogoAlt()
    {    
        return $this->_logo->getLogoAlt();
    }
    
    /**
     * Get logo width
     *
     * @return int
     */
    public function getLogoWidth()
    {    
        return $this->_logo->getLogoWidth();
    }
    
    /**
     * Get logo height
     *
     * @return int
     */
    public function getLogoHeight()
    {    
        return $this->_logo->getLogoHeight();
    }    
}
?>

Step 2: Get Logo data in template (.phtml) file

Add the below function in your template (.phtml) file

echo $block->getLogoSrc() . '<br />';
echo $block->getLogoAlt() . '<br />';
echo $block->getLogoWidth() . '<br />';
echo $block->getLogoHeight() . '<br />';

Thanks,

2
  • At least add reference from where you took the code!
    – Pawan
    Oct 29, 2020 at 4:26
  • ok @pawan.Thanks Oct 29, 2020 at 4:28

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.