我在 Magento 2 中开发了“Hello world”扩展。
我想覆盖核心文件的联系我们形式。在 Magento 2 中覆盖联系我们表单文件的正确方法是什么。
请帮我。任何帮助,将不胜感激。
与前两个答案不同,我选择从布局中删除原始块并使用我自己的模板添加一个新块。
我们将创建一个新模块,VendorName_ModuleName
我们需要为其创建以下文件:
/app/code/VendorName/ModuleName/view/frontend/layout/contact_index_index.xml
/app/code/VendorName/ModuleName/view/frontend/templates/form.phtml
/app/code/VendorName/ModuleName/etc/module.xml
/app/code/VendorName/ModuleName/composer.json
/app/code/VendorName/ModuleName/registration.php
Magento 2 中的每个模块都有由两部分组成的唯一名称。第一部分是描述构建扩展的公司、个人或团体的词。这有时被称为“<strong>vendor”命名空间。模块名称的第二部分是描述模块功能的词。
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<!-- Remove the original Contact Form -->
<referenceBlock name="contactForm" remove="true"/>
<!-- Add a custom Contact Form -->
<referenceContainer name="content">
<block class="Magento\Contact\Block\ContactForm" name="customContactForm" template="My_Module::form.phtml" />
</referenceContainer>
</body>
</page>
在上面的代码中,我删除了原始表单块并通过在referenceContainer
内容中添加我自己的表单来替换它。
笔记 :
在
contact_index_index.xml
中,代码template="My_Module::form.phtml"
是指您的自定义联系表单的phtml
.
现在,您需要制作自定义表单模板。您可以复制原始文件并对此文件进行修改。
<form class="form contact"
action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>"
id="contact-form"
method="post"
data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Write Us') ?></span></legend><br />
<div class="field note no-label"><?php /* @escapeNotVerified */ echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div>
<div class="field name required">
<label class="label" for="name"><span><?php /* @escapeNotVerified */ echo __('Name') ?></span></label>
<div class="control">
<input name="name" id="name" title="<?php /* @escapeNotVerified */ echo __('Name') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field email required">
<label class="label" for="email"><span><?php /* @escapeNotVerified */ echo __('Email') ?></span></label>
<div class="control">
<input name="email" id="email" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field telephone">
<label class="label" for="telephone"><span><?php /* @escapeNotVerified */ echo __('Phone Number') ?></span></label>
<div class="control">
<input name="telephone" id="telephone" title="<?php /* @escapeNotVerified */ echo __('Phone Number') ?>" value="" class="input-text" type="text" />
</div>
</div>
<div class="field comment required">
<label class="label" for="comment"><span><?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?></span></label>
<div class="control">
<textarea name="comment" id="comment" title="<?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"></textarea>
</div>
</div>
<?php echo $block->getChildHtml('form.additional.info'); ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>" class="action submit primary">
<span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
只需VendorName_ModuleName
用您自己的替换。
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
替换VendorName_ModuleName
为您自己0.0.1
的版本,并使用您的自定义模块的版本作为安装版本。
<?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="VendorName_ModuleName" setup_version="0.0.1" />
</config>
当然,如果你想让你的新模块工作,别忘了添加 composer.json
.
{
"name": "VendorName/ModuleName",
"autoload": {
"psr-4": { "VendorName\\ModuleName\\": "" },
"files": [ "registration.php" ]
} }
我解决了这个问题。如果您想覆盖任何核心文件,您只需使用引用名称并将此引用名称传递给 referenceBlock name="passit"。
对于 conatct us 文件覆盖,首先您获取 contactus form.phtml 的原始文件,然后找到它的布局文件 contact_index_index.xml 并获取参考名称,如"contactForm"。
系统/核心 contact_index_index.xml 文件。
<referenceContainer name="content">
<block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
这个“contactForm”引用名称传递给我们在referenceBlock标签中的扩展布局文件。请显示以下代码。
我们的扩展布局 contact_index_index.xml 文件
<referenceBlock name="contactForm">
<action method="setTemplate">
<argument name="template"xsi:type="string">Test_Overide::form.phtml</argument>
</action>
</referenceBlock>
在此之后,系统联系form.phtml没有被调用,我们的扩展form.phtml文件被调用。您可以使用 Developer 前端调试工具进行检查。
您可以使用插件来完成。
首先,您必须重写该块并调用如下beforeToHtml
方法:
public function beforeToHtml(\Magento\Catalog\Block\Product\View\Description $originalBlock)
{
$originalBlock->setTemplate('Vendorname_Modulename::description.phtml');
}
嗨,最简单的覆盖核心模板文件的方法:-
module-contact/view/frontend/templates/form.phtml
转到您的主题,请app/design/frontend/vendor/your_theme/
按照以下步骤操作:
form.phtm
或复制您form.phtml
的,然后对其进行编辑。为此,您需要创建一个扩展(自定义模块)。
从 app/magento创建一个block
,etc
和文件夹。view
在etc
文件夹中创建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="Xyz_Contact" setup_version="0.0.1"></module>
</config>
在view
文件夹中创建一个layout
文件夹并将下面的代码放在一个名为的文件中contact_index_index.xml
:
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<title>Contact Us</title>
</head>
<body>
<referenceContainer name="content">
<block class="Xyz\Contact\Block\ContactForm" name="contactForm" template="Xyz_Contact::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
</body>
</page>
创建templates
文件夹并将下面的代码放入form.phtml
:
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<form class="form contact"
action="<?php echo $block->getFormAction(); ?>"
id="contact-form"
method="post"
data-hasrequired="<?php echo __('* Required Fields') ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?php echo __('Write Us') ?></span></legend><br />
<div class="field note no-label"><?php echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div>
<div class="field name required">
<label class="label" for="name"><span><?php echo __('Name') ?></span></label>
<div class="control">
<input name="name" id="name" title="<?php echo __('Name') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field email required">
<label class="label" for="email"><span><?php echo __('Email') ?></span></label>
<div class="control">
<input name="email" id="email" title="<?php echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field telephone">
<label class="label" for="telephone"><span><?php echo __('Phone Number') ?></span></label>
<div class="control">
<input name="telephone" id="telephone" title="<?php echo __('Phone Number') ?>" value="" class="input-text" type="text" />
</div>
</div>
<div class="field comment required">
<label class="label" for="comment"><span><?php echo __('What’s on your mind?') ?></span></label>
<div class="control">
<textarea name="comment" id="comment" title="<?php echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"></textarea>
</div>
</div>
<?php echo $block->getChildHtml('form.additional.info'); ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?php echo __('Submit') ?>" class="action submit primary">
<span><?php echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
在Block
文件夹中,创建一个名为ContactForm.php
并使用以下代码的文件:
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Xyz\Contact\Block;
use Magento\Framework\View\Element\Template;
/**
* Main contact form block
*/
class ContactForm extends Template
{
/**
* @param Template\Context $context
* @param array $data
*/
public function __construct(Template\Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
}
请不要忘记在 中注册您的模块app/etc/config.php
,或者从命令行使用 Magento 二进制工具:php -f bin/magento module:enable Xyz_Contact
.
这Xyz
是公司(供应商)的名称,Contact
是模块名称。
如果您有任何问题,请告诉我。