Magento – Invalid template file override Magento 2 block

blocksmagento2override-block

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

Best Answer

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>
Related Topic