4

I have created a simple widget which displays dummy data. The problem is my front end code doesn't show up in my page ? I 'm working on Magento 2.2.0.

Here is my widget code what i want to display :

{{widget type="Widget\CustomWidget\Block\Widget\ContactInformations" fullname="Adrien" gender="mal" age="24"}}

widget/contact_informations code :

<?php
$fullname = $this->getData('fullname');
$age = $this->getData('age');
$gender = $this->getData('gender');
?>
<ul>
    <?php if($fullname !=''){?>
    <li><?php echo $fullname; ?></li>
    <?php } ?>
    <?php if($age !=''){?>
    <li><?php echo $age; ?></li>
    <?php } ?>
    <?php if($gender!=''){?>
    <li>

    </li>
    <?php } ?>
</ul>

etc/module.xml code :

<?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="Widget_CustomWidget" setup_version="1.0.0">
    </module>
</config>

etc/widget.xml code :

<?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="widget_customwidget" class="Widget\CustomWidget\Block\Widget\ContactInformations">
        <label translate="true">Contact Informations Widget</label>
        <description>Widget in Magento2</description>
        <parameters>
            <parameter name="fullname" xsi:type="text"  visible="true" sort_order="0" >
                <label translate="true">Full Name</label>
            </parameter>
            <parameter name="age" xsi:type="text"  visible="true" sort_order="10" >
                <label translate="true">Age</label>
            </parameter>
            <parameter name="gender" xsi:type="select" source_model="Widget\CustomWidget\Model\Config\Source\Gender" visible="true" sort_order="10" >
                <label translate="true">Gender</label>
            </parameter>
        </parameters>
    </widget>
</widgets>

Widget/CustomWidget/Block/Widget/ContactInformations code :

<?php
namespace Widget\CustomWidget\Block\Widget; 
class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
    public function _toHtml()
    {
        parent::_construct();
        $this->setTemplate('widget/contact_informations.phtml');
    }
}

Any idea ?

11
  • You should post your custom widget code here? Dec 1, 2016 at 9:08
  • I updated my question. Dec 1, 2016 at 9:19
  • can you show the code for Widget\CustomWidget\Block\Widget\ContactInformations ? is it extending \Magento\Framework\View\Element\Template or other widget block? Dec 1, 2016 at 9:29
  • Question updated @MiroslavPetroff Dec 1, 2016 at 9:38
  • @AdrienCastagliola instead of using _toHtml() , Use _construct function it will work - try this code - protected function _construct() { parent::_construct(); $this->setTemplate('widget/contact_informations.phtml'); } Dec 1, 2016 at 9:39

2 Answers 2

3

Your widget block Widget\CustomWidget\Block\Widget\ContactInformations should extend \Magento\Framework\View\Element\Template otherwise it can't be displayed.

Everything is ok in your configuration code. However your Block code is wrong. You shouldn't override _tomHtml() in this case but define your template as protected $_template = 'widget/contact_informations.phtml'. Your template must be stored to this path, at the root of your module: /view/frontend/widget/contact_informations.phtml

<?php 
namespace Widget\CustomWidget\Block\Widget;

class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface {

protected $_template = 'widget/contact_informations.phtml';

}
8
  • It already does. Dec 1, 2016 at 9:44
  • 1
    <?php namespace Widget\CustomWidget\Block\Widget; class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface { $_template = 'widget/contact_informations.phtml'; }
    – Mr.K
    Dec 1, 2016 at 9:51
  • Try to replace your code by the code above
    – Mr.K
    Dec 1, 2016 at 9:52
  • My code expects a function i think. Dec 1, 2016 at 9:56
  • Actually your method '_toHtml' is completely wrong.. Please do not override _toHtml() and override the member class $_template with your template value.
    – Mr.K
    Dec 1, 2016 at 10:00
1

Your widget class should be like this:

<?php
namespace Widget\CustomWidget\Block\Widget; 
class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    public function _toHtml()
    {
        $this->setTemplate("widget/contact_informations.phtml");
        return parent::_toHtml();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.