在 Magento 2 如何覆盖 phtml 或布局核心文件?
In Magento 2 How to Override phtml or layout core files?
我在 Magento 2 中开发了 "Hello world" 扩展。
我想覆盖核心文件的联系我们表格。在 Magento 2 中覆盖 Contact us 表单文件的正确方法是什么。
请帮助我。任何帮助将不胜感激。
为此您需要创建一个扩展(自定义模块)。
从 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
是模块名称。
如果您有任何问题,请告诉我。
您可以使用插件来完成。
首先你必须覆盖块并像这样调用 beforeToHtml
方法:
public function beforeToHtml(\Magento\Catalog\Block\Product\View\Description $originalBlock)
{
$originalBlock->setTemplate('Vendorname_Modulename::description.phtml');
}
与前两个答案不同,我选择从布局中删除原始块并使用我自己的模板添加新块 .
我们将创建一个新模块 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
Every module in Magento 2 has unique name that’s made up of two parts. The first part is a word that describes the company, individual, or group that built the extension. This is sometimes called the “vendor” namespace. The second part of a module’s name is a word that describes what the module does.
Alan Storm, in his tutorial Magento 2 Hello World Module
contact_index_index.xml
<?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>
在上面的代码中,我删除了原始表单 Block 并通过在 referenceContainer
内容中添加我自己的表单来替换它。
Note :
In contact_index_index.xml
, the code template="My_Module::form.phtml"
refers to your custom contact form's phtml
.
form.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>
registration.php
只需将 VendorName_ModuleName
替换为您自己的即可。
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
module.xml
将 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
当然,如果你想让你的新模块工作,不要忘记添加composer.json
。
{
"name": "VendorName/ModuleName",
"autoload": {
"psr-4": { "VendorName\ModuleName\": "" },
"files": [ "registration.php" ]
} }
进一步参考
- composer.json
的 Magento 2 文档
- 使用自动加载在 composer.json 中调用 registration.php
- 探索Sample Modules by Magento on Github的代码。
我解决了这个问题。如果你想覆盖任何核心文件,你只需使用引用名称并将此引用名称传递给 referenceBlock name="passit".
对于 conatct us 文件覆盖,首先你得到 contactus 的原始文件 form.phtml 然后找到它的布局文件 contact_index_index.xml 并得到参考名称 "contactForm"。
System/core 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>
在此之后,系统 contactus form.phtml 未被调用,我们的分机 form.phtml 文件被调用。可以使用Developer前端调试工具查看。
您好,覆盖核心模板文件的最简单方法:-
module-contact/view/frontend/templates/form.phtml
转到您的主题app/design/frontend/vendor/your_theme/
按照以下步骤操作:
- 创建 Magento_Contact 文件夹(将 module-contact 重命名为 Magento-Contact)
- 创建模板文件夹
- 创建
form.phtm
或从核心复制您的 form.phtml
,然后编辑它。
我在 Magento 2 中开发了 "Hello world" 扩展。
我想覆盖核心文件的联系我们表格。在 Magento 2 中覆盖 Contact us 表单文件的正确方法是什么。
请帮助我。任何帮助将不胜感激。
为此您需要创建一个扩展(自定义模块)。
从 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
是模块名称。
如果您有任何问题,请告诉我。
您可以使用插件来完成。
首先你必须覆盖块并像这样调用 beforeToHtml
方法:
public function beforeToHtml(\Magento\Catalog\Block\Product\View\Description $originalBlock)
{
$originalBlock->setTemplate('Vendorname_Modulename::description.phtml');
}
与前两个答案不同,我选择从布局中删除原始块并使用我自己的模板添加新块 .
我们将创建一个新模块 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
Every module in Magento 2 has unique name that’s made up of two parts. The first part is a word that describes the company, individual, or group that built the extension. This is sometimes called the “vendor” namespace. The second part of a module’s name is a word that describes what the module does.
Alan Storm, in his tutorial Magento 2 Hello World Module
contact_index_index.xml
<?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>
在上面的代码中,我删除了原始表单 Block 并通过在 referenceContainer
内容中添加我自己的表单来替换它。
Note :
In
contact_index_index.xml
, the codetemplate="My_Module::form.phtml"
refers to your custom contact form'sphtml
.
form.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>
registration.php
只需将 VendorName_ModuleName
替换为您自己的即可。
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
module.xml
将 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
当然,如果你想让你的新模块工作,不要忘记添加composer.json
。
{
"name": "VendorName/ModuleName",
"autoload": {
"psr-4": { "VendorName\ModuleName\": "" },
"files": [ "registration.php" ]
} }
进一步参考
- composer.json 的 Magento 2 文档
- 使用自动加载在 composer.json 中调用 registration.php
- 探索Sample Modules by Magento on Github的代码。
我解决了这个问题。如果你想覆盖任何核心文件,你只需使用引用名称并将此引用名称传递给 referenceBlock name="passit".
对于 conatct us 文件覆盖,首先你得到 contactus 的原始文件 form.phtml 然后找到它的布局文件 contact_index_index.xml 并得到参考名称 "contactForm"。
System/core 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>
在此之后,系统 contactus form.phtml 未被调用,我们的分机 form.phtml 文件被调用。可以使用Developer前端调试工具查看。
您好,覆盖核心模板文件的最简单方法:-
module-contact/view/frontend/templates/form.phtml
转到您的主题app/design/frontend/vendor/your_theme/
按照以下步骤操作:
- 创建 Magento_Contact 文件夹(将 module-contact 重命名为 Magento-Contact)
- 创建模板文件夹
- 创建
form.phtm
或从核心复制您的form.phtml
,然后编辑它。