0

I'm trying to extend customer address edit page, but it is throwing Error. For these I have written code as below.

composer.json

{
  "name": "learning/module-contact",
  "description": "Contact",
  "require": {
      "php": "~5.5.0|~5.6.0|~7.0.0",
      "magento/framework": "100.0.*",
      "magento/module-ui": "100.0.*",
      "magento/module-config": "100.0.*"
  },
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
  "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Learning\\Contact\\": ""
        }
    }
}

registration.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Learning_Contact',
    __DIR__
);

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Learning_Contact" setup_version="1.0.0">
    </module>
</config>

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">
    <!--<type name="Magento\Customer\Block\Address\Edit">
        <plugin name="contact-custom-module" type="Learning\Contact\Block\Address\Edit" sortOrder="0"/>
    </type>-->
    <preference for="Magento\Customer\Block\Address\Edit" type="Learning\Contact\Block\Address\Edit"/>
</config>

Edit.php

<?php

namespace Learning\Contact\Block\Address;

class Edit extends \Magento\Customer\Block\Address\Edit{


    /**
     * Registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_registry;


    public function __construct(\Magento\Framework\Registry $registry){
        $this->_registry = $registry;
        parent::__construct();
    }


    public function _toHtml() {

        if ($this->getTemplate() == 'customer/address/edit.phtml') {

                $htmlToInsertAfter = '<h2 class="legend">'. __('Address') .'</h2>';
                $addressCandidatesHtml = $this->getLayout()->createBlock('contact/candidates')->setTemplate('custom.phtml');
                $editHtml = parent::_toHtml();
                return substr_replace($editHtml, $addressCandidatesHtml, strpos($editHtml, $htmlToInsertAfter)+strlen($htmlToInsertAfter),0);
        }

        return parent::_toHtml();
    }


}

custom.phtml

<h1>Hi I'm here</h1>

Error : Object DOMDocument should be created

Is it correct procedure to extend customer address edit page?

  • plz upload your module.xml file and registration.php file to solve out your issue.your issue arrives in xml file. – Rakesh Jesadiya Nov 25 '15 at 5:54
  • @RBJesadiya, I have written above code after my module activating. But I'm not mentioning composer.json and registration.php code in my post. see my thread updated. – Bojjaiah Nov 25 '15 at 5:57
  • you must need composer.json and registration.php in your root of module. – Rakesh Jesadiya Nov 25 '15 at 5:58
  • plz show your module.xml file – Rakesh Jesadiya Nov 25 '15 at 6:00
  • @RBJesadiya, now updated. even though I have ran php bin/magento cache:clean and php bin/magento setup:upgrade. – Bojjaiah Nov 25 '15 at 6:02
1

composer.json

{
    "name": "learning/module-contact",
    "description": "contact",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/framework": "100.0.*",
        "magento/module-ui": "100.0.*",
        "magento/module-config": "100.0.*",
        "magento/module-contact": "100.0.*"
    },
    "type": "magento2-module",
    "version": "100.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "Learning\\Contact\\": ""
        }
    }
}

registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Learning_Contact',
    __DIR__
);

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Learning_Contact" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Contact"/>
        </sequence>
    </module>
</config>

Edit.php

namespace Learning\Contact\Block\Address;

class Edit extends \Magento\Customer\Block\Address\Edit
{
    /**
     * Registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_registry;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Directory\Helper\Data $directoryHelper,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\App\Cache\Type\Config $configCacheType,
        \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
        \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
        \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
        \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
        \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
        \Magento\Framework\Registry $registry,
        array $data = []
    ){
        $this->_registry = $registry;
        parent::__construct($context, $directoryHelper, $jsonEncoder, $configCacheType, $regionCollectionFactory, $countryCollectionFactory, $customerSession, $addressRepository, $addressDataFactory, $currentCustomer, $dataObjectHelper, $data);
    }


    public function _toHtml()
    {

        if ($this->getTemplate() == 'address/edit.phtml')
        {
            $this->setTemplate('Magento_Customer::address/edit.phtml');
            $editHtml = $this->fetchView($this->getTemplateFile());
            $htmlToInsertAfter = ''. __('Address') .'';
            $this->setTemplate('Learning_Contact::custom.phtml');
            $addressCandidatesHtml = $this->fetchView($this->getTemplateFile());
            return $editHtml.$addressCandidatesHtml;
            //return substr_replace($editHtml, $addressCandidatesHtml, strpos($editHtml, $htmlToInsertAfter)+strlen($htmlToInsertAfter),0);
        }

        return parent::_toHtml();
    }

}
| improve this answer | |
  • Yes now rendering my custom.phtml file but empty page is shown. Is last line is correct my code i.e., substr_replace() – Bojjaiah Nov 25 '15 at 8:53
  • substr_replace() -> here has problem. Just comment out this line and return $editHtml. Then you can get actual output. – Sohel Rana Nov 25 '15 at 9:03
  • I want display combined both my custom.phtml and edit.phtml. how? – Bojjaiah Nov 25 '15 at 9:06
  • check last updated code – Sohel Rana Nov 25 '15 at 10:40

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.