4

I want to overrid magento 2 address_book block, but I got an error like this:

Invalid template file: 'address/book.phtml' in module: 'Namespace_Module' block's name: 'address_book'

di.xml:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Block\Address\Book" type="Namespace\Module\Block\Address\Book"/>
</config>

Namespace\Module\Block\Address\Book.php :

namespace Lime\Courier\Block\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
{
    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,
        array $data = []
    ) {
        $this->customerRepository = $customerRepository;
        $this->currentCustomer = $currentCustomer;
        $this->addressRepository = $addressRepository;
        $this->_addressConfig = $addressConfig;
        $this->addressMapper = $addressMapper;
        parent::__construct($context, $customerRepository,$addressRepository,$currentCustomer,$addressConfig,$addressMapper, $data);
      }

      public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $address = null)
      {
          if ($address !== null) {
              /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
              $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
              return $renderer->renderArray($this->addressMapper->toFlatArray($address));
          }
          return '';
      }

}

I think the problem is they search for template file in my module, but I just need to override the block file not the phtml file

2

In the file customer_address_index.xml you can see this:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceBlock name="head.components">
            <block class="Magento\Framework\View\Element\Js\Components" name="customer_address_head_components" template="Magento_Customer::js/components.phtml"/>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Address\Book" name="address_book" template="address/book.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

The template for the block Magento\Customer\Block\Address\Book is address/book.phtml. When you override this block, Magento look for the template address/book.phtml in your custom module Namespace\Module. You can do 2 way to solve this.

Copy the template address/book.phtml in Namespace/Module/view/frontend/templates/address/book.phtml

or

Create a file customer_address_index.xml in Namespace/Module/view/frontend/layout/ specifying the module to load the template.

<?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="address_book">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Customer::address/book.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
-1

i don't see any difference in your method getAddressHtml. You should clear block_html or all cache

php bin/magento cache:clean

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.