1

目录结构预览:

app/code/Silk/Test/etc/module.xml
app/code/Silk//Test/etc/widget.xml
app/code/Silk//Test/registration.php
app/code/Silk//Test/Block/Widget/Mwcontact.php
app/code/Silk//Test/view/frontend/templates/widget/contact_widget.phtml

1) etc/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="Silk_Test" setup_version="1.0.0.3" />
</config>

2) etc/widget.xml



<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="custom_widget" class="Silk\Test\Block\Widget\Mwcontact">
        <label translate="true">Silk Custom - Widget</label>
        <description>Silk Custom - Widget</description>
        <parameters>
            <parameter name="name" xsi:type="text" visible="true" required="true" sort_order="0" >
                <label translate="true">Name</label>
            </parameter>
            <parameter name="address" xsi:type="text" visible="true" sort_order="10">
                <label translate="true">Address</label>
            </parameter>
            <parameter name="var1" xsi:type="select" visible="true"  source_model="Magento\Config\Model\Config\Source\Yesno">
                <label translate="true">Yes/No</label>
            </parameter>
            <parameter name="website" xsi:type="text" visible="true" sort_order="30">
                <label translate="true">Website URL</label>
            </parameter>
        </parameters>
    </widget>
</widgets>

3) registration.php



<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Silk_Test',
    __DIR__
);

4) Block/Widget/Mwcontact.php



<?php
/**
 * Created by PhpStorm.
 * User: denson
 * Date: 16-9-19
 * Time: 下午15:48
 */
namespace Silk\Block\Block\Widget;

class Mwcontact extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('widget/contact_widget.phtml');
    }
}

5) view/frontend/templates/widget/contact_widget.phtml



<div class="contact-widget">
    <table>
        <tr>
            <td>Name</td>
            <td><?php echo $this->getData('name'); ?></td>
        </tr>
        <tr>
            <td>Address</td>
            <td><?php echo $this->getData('address'); ?></td>
        </tr>
        <tr>
            <td>Mobile</td>
            <td><?php echo $this->getData('mobile'); ?></td>
        </tr>
        <tr>
            <td>Website URL</td>
            <td><?php echo $this->getData('website'); ?></td>
        </tr>
    </table>
</div>

然后,打开页面,可以看到如下效果

图片描述


denson
366 声望57 粉丝