3

I have a module that is installed in other sites. In one site it doesn't show up when inserted into CMS Pages.

{{widget type="Company\Modulename\Block\Widget\Test" template="widget/newsletter/test.phtml"}}

app/code/Company/Modulename/Block/Widget/Test.php:

namespace Company\Modulename\Block\Widget; 
class Test extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{ 
 protected function _construct()
 {
 parent::_construct();
 } 
}

app/code/Company/Modulename/view/frontend/templates/widget/newsletter/test.phtml:

<?php
echo 1;

app/code/Company/Modulename/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="test" class="Company\Modulename\Block\Widget\Buttons">
 <label translate="true">some text</label>
 <description>some text</description>
 <parameters>
 ...

It will show just empty string, no matter what I try. I enabled developer mode but no info about that is inside system.log. Any idea what could be wrong or at least how I can debug this problem?

asked Nov 23, 2016 at 17:05
2
  • do you have etc/widget.xml in your module with declaration of your widget? Commented Nov 23, 2016 at 17:22
  • Yes I have. It works in other stores, but in this one it won't without any error inside system.log, it will just show empty string where widget should appear in frontend. Commented Nov 23, 2016 at 17:51

2 Answers 2

0

Not necessary an answer, but I resolved the problem. The code was obfuscated. Once I uploaded code without obfuscation it started to work. I am not sure why only on this environment, it worked properly exact the same code in two other sites. I guess there were some errors masked by server.

What I tried is to copy/paste sample widget code from other extension, renamed class names, removed all unnecessary part of code so it was left only echo 'test'; inside phtml and only class declaration and class inherit statements. Then I put back line by line. I realized it stopped working just at the last step when I tried to use obfuscated code.

answered Nov 25, 2016 at 15:27
0

Your widgets should be defined in etc/widget.xml and then you can insert them with the WYSIWYG editor from the "insert widget" button or create new widget from Content -> Widgets in admin.

app/code/Company/Modulename/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="company_widget_test" class="Company\Modulename\Block\Widget\Test">
 <label translate="true">Test widget</label>
 <description translate="true"></description>
 <parameters>
 <parameter name="title" xsi:type="text" visible="true" sort_order="10">
 <label translate="true">Title</label>
 </parameter>
 </parameters>
</widget>

You can add many parameters to your widget even define template filename. Examples: vendor/magento/module-catalog/etc/widget.xml and any other widget.xml in other Magento modules

Other way to set template filename to the widget

app/code/Company/Modulename/Block/Widget/Test.php:

namespace Company\Modulename\Block\Widget; 
class Test extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{ 
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 array $data = []
 ) {
 $this->setTemplate('widget/newsletter/test.phtml');
 parent::__construct($context, $data);
 } 
}
Marius
199k55 gold badges431 silver badges837 bronze badges
answered Nov 23, 2016 at 17:42
7
  • widget.xml is already there, I didn't wrote that because this is obviously. It works in other stores, but in this one it won't and before I go into deep researching of core code I wanted to ask if anybody have any idea what could be the reason, if there can be some permission problem or anything. Commented Nov 23, 2016 at 17:48
  • can you find the widget in visual editor or you are adding it with the code manually? Or can you create new instance of the widget from Content -> Widgets? Commented Nov 23, 2016 at 17:56
  • Yes I find the widget in visual editor, I am also able to create new instance of the widget. Commented Nov 23, 2016 at 19:02
  • Can you try to set the template with Company_Modulename::widget/newsletter/test.phtml on CMS page and on widget.xml as parameter Commented Nov 23, 2016 at 22:11
  • Still no luck. It is weird that it won't show any error inside system.log. Commented Nov 23, 2016 at 23:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.