0

I want to override block Magento\Customer\Block\Address\Book in my custom module. And need to add a new function which returns module status enable/disable, which is defined in the model class. I need to call my custom function in book.phtml which is copied to my module.

Thanks.

  • Do you already have the module created ? – nikin Dec 13 '18 at 5:46
  • yes i have already created module and copied "book.phtml" – Utsav Gupta Dec 13 '18 at 5:46
1

Include the below line in your custom modules di.xml

<preference for="Magento\Customer\Block\Address\Book" type="<<Your Custom Modules Block File Path" />

And, in your custom modules block php file, override the desired method.

<?php

    namespace <<Custom Module Block Path>>;

    class CustomModuleBlockName extends \Magento\Customer\Block\Address\Book
    {
        public function <<methodname()
        {
            // Do your stuff here
        }
    }
  • can we override from layout xml file? – Utsav Gupta Dec 13 '18 at 5:54
  • Along with the block changes, we may need to do layout xml occassionally. Share you layout xml file – nikin Dec 13 '18 at 6:00
  • i have below code in layout customer_address_index.xml <referenceContainer name="content"> <block class="Name\Module\Block\Rewrite\Address\Book" name="address_book" template="Name_Module::address/book.phtml" cacheable="false"/> </referenceContainer> – Utsav Gupta Dec 13 '18 at 6:15
  • with di.xml it gives error Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Magento\Customer\Block\Address\Book Exception #1 (ReflectionException): Class Name\Module\Block\Rewite\Address\Book\Modulestatus does not exist – Utsav Gupta Dec 13 '18 at 6:19
  • Have you created the block ? – nikin Dec 13 '18 at 6:22
0
  1. Copy Book.php from Magento\Customer\Block\Address\Book to Vendor/ModuleName/Block/Address/Book Add your custom function inside book.php.

  2. In layout file, relate your template with the class file .

    <block class="Vendor/ModuleName/Block/Address/Book" name="customaddress" template="Vendor_ModuleName::your_layout_file.phtml"/>
    
  3. Within template file, call your custom function. $block->yourFunction()

0

I used layout xml to override phtml. i have blow code in my layout file for override.

We need to add all the dependencies in our module block file.

namespace Name\Module\Block\Rewrite\Address;

use Magento\Customer\Api\AddressRepositoryInterface; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Model\Address\Mapper;

class Book extends \Magento\Customer\Block\Address\Book { protected $currentCustomer; protected $customerRepository; protected $addressRepository; protected $_addressConfig; protected $addressMapper; protected $_moduleStatusConfigProvider;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    CustomerRepositoryInterface $customerRepository,
    AddressRepositoryInterface $addressRepository,
    \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
    \Magento\Customer\Model\Address\Config $addressConfig,
    Mapper $addressMapper,
    \Name\Module\Model\ModuleStatusConfigProvider $moduleStatusConfigProvider,
    array $data = []
) {
    $this->customerRepository = $customerRepository;
    $this->currentCustomer = $currentCustomer;
    $this->addressRepository = $addressRepository;
    $this->_addressConfig = $addressConfig;        
    $this->addressMapper = $addressMapper;
    $this->_moduleStatusConfigProvider = $moduleStatusConfigProvider;
    parent::__construct($context, $customerRepository,$addressRepository,$currentCustomer,$addressConfig,$addressMapper,$data);        
}

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.