Невозможно расширить страницу редактирования адреса клиента в Magento 2

magento.stackexchange https://magento.stackexchange.com//questions/91472

Вопрос

Я пытаюсь продлить Адрес клиента Редактировать страницу , но она бросает ошибку.Для этого я написал код, как показано ниже.

<Сильный> 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\\": ""
        }
    }
}
.

<Сильные> Регистрация .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__
);
.

<Сильные> Модуль.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>
.

ОШИБКА: Object Domdocument должен быть создан

Это правильная процедура для расширения страницы редактирования адреса клиента?

Это было полезно?

Решение

<Сильный> 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\\": ""
        }
    }
}
.

<Сильные> Регистрация .php

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

.

<Сильные> Модуль.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();
    }

}
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top