在Magento 2中如何覆盖phtml或布局核心文件?

Nik*_*iks 4 php magento magento2

我在Magento 2中开发了"Hello world"扩展.

我想覆盖联系我们的核心文件形式.什么是覆盖的正确方法联系我们Magento 2中的表单文件.

请帮我.任何帮助,将不胜感激.

Hen*_*nry 12

与之前的两个答案不同,我选择从布局中删除原始块,并使用我自己的模板添加一个新.

我们将创建一个新模块,VendorName_ModuleName我们需要为其创建以下文件:

  1. /app/code/VendorName/ModuleName/view/frontend/layout/contact_index_index.xml
  2. /app/code/VendorName/ModuleName/view/frontend/templates/form.phtml
  3. /app/code/VendorName/ModuleName/etc/module.xml
  4. /app/code/VendorName/ModuleName/composer.json
  5. /app/code/VendorName/ModuleName/registration.php

Magento 2中的每个模块都有唯一的名称,由两部分组成.第一部分是描述构建扩展的公司,个人或团体的词.这有时称为" 供应商 "命名空间.模块名称的第二部分是描述模块功能的单词.

Alan Storm,在他的教程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>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我删除了原始表单Block并通过在referenceContainer内容中添加我自己的表单来替换它.

注意 :

contact_index_index.xml,代码template="My_Module::form.phtml"是指您的自定义联系表单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>
Run Code Online (Sandbox Code Playgroud)

为registration.php

只需VendorName_ModuleName用您自己的替换.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_ModuleName',
    __DIR__
);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

composer.json

当然,如果您想让新模块正常工作,请不要忘记添加 composer.json.

 {
"name": "VendorName/ModuleName",
"autoload": {
    "psr-4": { "VendorName\\ModuleName\\": "" },
    "files": [ "registration.php" ]
} }
Run Code Online (Sandbox Code Playgroud)

进一步参考

  1. composer.json的 Magento 2文档
  2. 使用autoload 在composer.json中调用registration.php
  3. 探索的代码示例模块通过Magento的Github上.