8

I want to customize Magento\Theme\Block\Html\Footer class in magento2.

how can i change some text and style of footer copyright text with custom module..

12

You don't need to create custom module. You can change the copyright text in Admin.

  1. Go to Content >> Configuration.
  2. Edit the active theme
  3. Click on Footer tab
  4. Edit the copyright Text enter image description here

OR

You can override Magento\Theme\Block\Html\Footer class in your custom module with getCopyright function like this:

<?php
namespace Vendor\Theme\Block\Html;

class Footer extends Magento\Theme\Block\Html\Footer
{
    /*
     * Retrieve copyright information
     *
     * @return string
     */
    public function getCopyright()
    {
        if (!$this->_copyright) {
            $this->_copyright = $this->_scopeConfig->getValue(
                'design/footer/copyright',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
        }
        return __($this->_copyright);
    }

}

and update the getCopyright function as per your requirement.

If you can specify the exact requirement as to what needs to be changed I can help you in better way.

2
  • I know this method..but my concern is from Magento\Theme\Block\Html\Footer this class with custom module – Chandra Kumar Aug 14 '17 at 9:25
  • please check my answer – Sejal Shah Aug 16 '17 at 8:53
0

If you want to customize Magento\Theme\Block\Html\Footer you need:

Create your own module.

Create YourVendor/YourModule/etc/frontend/di.xml file like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Theme\Block\Html\Footer" type="YourVendor\YourModule\Block\Html\Footer" />

Create your block like:

<?php
declare(strict_types=1);

namespace YourVendor\YourModule\Block\Html;

/**
 * Html page footer block
 */
class Footer extends \Magento\Theme\Block\Html\Footer
{
   /**
    * @inheritdoc
    */
    public function getCopyright(): string
    {
        $copyright = parent::getCopyright();

        return '| ' . $copyright . ' |';
    }
}
0
  1. Create di.xml file in <vendor_name>/<module_name>/etc/frontend (i.e Lokesh/Custom/etc/frontend/di.xml)

     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
         <preference for="Magento\Theme\Block\Html\Footer" type="Lokesh\Custom\Block\Html\Footer" />
     </config>
    
  2. Create Footer.php in <vendor_name>/<module_name>/Block/Html (i.e Lokesh/Custom/Block/Html/Footer.php) and add below code

A. Without Template:

<?php
namespace Lokesh\Custom\Block\Html;

class Footer extends \Magento\Theme\Block\Html\Footer
{
     public function getCopyright()
    {
        if (!$this->_copyright) {
            $this->_copyright = "Your Copyright Content goes here";
        }
        return __($this->_copyright);
    }
}

B. With Template:

<?php
namespace Lokesh\Custom\Block\Html;

class Footer extends \Magento\Theme\Block\Html\Footer
{
     public function getCopyright()
    {
        if (!$this->_copyright) {
            $this->_copyright =  $this->getLayout()
          ->createBlock('Lokesh\Custom\Block\Html\Footer')
          ->setTemplate('Lokesh_Custom::footer.phtml')
          ->toHtml();

        }
        return __($this->_copyright);
    }

    public function getYear()
    {
        return date('Y');
    }
}

Template File: Lokesh/Custom/view/template/footer.phtml

<?php
$year = $block->getYear();
?>
<p class="copyright col-m-6">Copyright © <?= $year; ?> Company and Content</p>
   

NOTE: Execute php bin/magento cache:flush and php bin/magento setup:static-content:deploy -f in cli to see the changes on storefront

New contributor
Lokesh Vinay Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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