0

I have attempted several steps to create a custom block to display custom copyright text below the existing default copyright in the frontend.

In my module root directory (app/code/Ciptaloka/HelloWorld/), I have these files added:

  • Block\Page\Footer\Copyright.php - Block file

    <?php
    
    namespace Ciptaloka\HelloWorld\Block\Page\Footer;
    
    use Magento\Framework\View\Element\Template;
    use Magento\Framework\View\Element\Template\Context;
    
    class Copyright extends Template
    {
        public function __construct(
            Context $context,
            array $data = []
        ) {
            parent::__construct($context, $data);
        }
    
        public function getCopyrightTxt()
        {
            return __('All products displayed on this website are manufactured by Ciptaloka.com');
        }
    }
    
  • view\frontend\templates\html\copyright.phtml - Template file

    <small class="copyright">
        <span><?= $this->getCopyrightTxt(); ?></span>
    </small>
    
  • view\frontend\layout\page_footer_copyright.xml - Layout configuration 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>
            <referenceContainer name="copyright">
                <block
                        name="ciptaloka_helloworld.footer.copyright"
                        class="Ciptaloka\HelloWorld\Block\Page\Footer\Copyright"
                        after="-"
                        template="html/copyright.phtml"/>
            </referenceContainer>
        </body>
    </page>
    

After creating those files, I tested it, but unfortunately it doesn't get displayed.

I want the frontend copyright footer to change from this:

<small class="copyright">
  <span>Copyright © 2017 Magento, Inc. All rights reserved.</span>
</small>

to become this:

<small class="copyright">
  <span>Copyright © 2017 Magento, Inc. All rights reserved.</span>
</small>
<small class="copyright">
  <span>All products displayed on this website are manufactured by Ciptaloka.com</span>
</small>

My question is, can it be established by creating custom block on a custom module? If it can, and my codes got something wrong, can you point me out?

Thanks.


EDIT: My layout now looks like this, other files remain unchanged:

<?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>
        <referenceContainer name="before.body.end">
            <block
                    name="ciptaloka_helloworld.footer.copyright"
                    class="Ciptaloka\HelloWorld\Block\Page\Footer\Copyright"
                    after="copyright"
                    template="html/copyright.phtml"/>
        </referenceContainer>
    </body>
</page>

But the result is not what I expected: my custom copyright is placed above the existing copyright message.

My custom copyright is placed above the existing copyright message

1
  • I cannot believe that for add a simple link into the copyright footer you have to do such a work!!!
    – Crispy
    Commented May 23, 2019 at 8:24

3 Answers 3

4

Your close, the reason your template is not rendering is because you're using <referenceContainer name="copyright">. There are three issues here:

  • copyright is a block, not a container. Check vendor/magento/module-theme/view/frontend/layout/default.xml line 121. Change referenceContainer to referenceBlock.

  • If you want your change to apply on all pages you need to rename your layout XML file to be default.xml

  • Because copyright is a block you need to call your block inside it (only containers automatically render child blocks). To do this you'll need to overwrite this template - vendor/magento/module-theme/view/frontend/templates/html/copyright.phtml so it looks like so:

 

<small class="copyright">
    <span><?php /* @escapeNotVerified */ echo $block->getCopyright() ?></span>
</small>

<?php echo $this->getChildHtml('ciptaloka_helloworld.footer.copyright'); ?>

Alternative method

An alternative method would be to render your template via the before.body.end container (for Luma only), as this is a container it will automatically render your template.

<referenceContainer name="before.body.end">
    <block 
        name="ciptaloka_helloworld.footer.copyright"
        class="Ciptaloka\HelloWorld\Block\Page\Footer\Copyright"
        after="copyright"
        template="html/copyright.phtml"/>
</referenceContainer>
11
  • Does this mean that we cannot purely make modification from custom module codes alone and we must make changes in Magento vendor to achieve this? Commented May 17, 2017 at 1:45
  • And I think that my custom layout configuration file (page_footer_copyright.xml) is not loaded, how to make it be loaded? Commented May 17, 2017 at 2:08
  • Nooooo never edit files in vendor, you can create the file in your theme and it will overwrite it or if you're doing it in a module you can set the template to use your own - see this answer for how to do that. If you want your change to render on every page I think you can rename your layout XML file to be default.xml and it will render on every page. I haven't tried this before as I only work in themes not modules.
    – Ben Crook
    Commented May 17, 2017 at 6:37
  • Sorry I misunderstood that "overwrite" statement that you wrote, I get it now. I've changed referenceContainer to referenceBlock and renamed my layout file to default.xml but nothing happened. And the answer link you gave me means to change the template of block but what I want is to extend the existing copyright block with my custom block. Commented May 17, 2017 at 7:47
  • If you want to add your template after the existing breadcrumbs without altering the existing breadcrumb template you will need to use <referenceContainer name="footer"> and add after="breadcrumbs" to your block. At the moment you have added your template but it isn't being rendered anywhere because you haven't called your template file or set the reference to a container. As footer is a container it should automatically be rendered now. Also make sure you clear cache after making XML changes.
    – Ben Crook
    Commented May 17, 2017 at 8:20
2

For achieving your task you just need to create a static block for your custom HTML or text and override copyright template file in your theme

Copy copyright.phtml file in your theme

vendor/magento/module-theme/view/frontend/templates/html/copyright.phtml

to

app/design/frontend/Vendor/theme/Magento_Theme/templates/html/copyright.phtml

Now call your static block in copyright.phtml file to add custom text below copyright

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<small class="copyright">
    <span><?php /* @escapeNotVerified */ echo $block->getCopyright() ?></span>
</small>

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
2
  • Is there any way to achieve this without overriding the template file in Magento module vendor? Commented May 17, 2017 at 1:50
  • 1
    @RizkiPratama You should accept an answer to help other developers to find their solutions. Commented Jun 11, 2017 at 14:12
0

You can change using the admin panel on this route below:

Content > Design > Configuration > SELECT YOUR THEME > Other Settings > Footer > Copyright.

Footer Copyright Magento 2

So you can add your code like that and customize using the CSS below:

<span>All products displayed on this website are manufactured by Ciptaloka.com</span>

.copyright span{
    display:block;
    width:100%;
    margin:10px 0;
}
3
  • As I stated in my title, I want to add it by module, NOT by admin configuration from backend page, so when user install this module, the copyright is extended by my custom copyright message. Commented May 18, 2017 at 4:29
  • @Rafael Corrêa Gomes, can one insert PHP code to display the year inside "Miscellaneous HTML" text area? Commented May 7, 2020 at 18:56
  • No, it's not possible Commented May 8, 2020 at 14:21

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.