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?
2 Answers 2
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.
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);
}
}
-
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.user1324762– user13247622016年11月23日 17:48:36 +00:00Commented 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?Miroslav Petroff– Miroslav Petroff2016年11月23日 17:56:47 +00:00Commented 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.user1324762– user13247622016年11月23日 19:02:26 +00:00Commented Nov 23, 2016 at 19:02
-
Can you try to set the template with
Company_Modulename::widget/newsletter/test.phtmlon CMS page and on widget.xml as parameterMiroslav Petroff– Miroslav Petroff2016年11月23日 22:11:08 +00:00Commented Nov 23, 2016 at 22:11 -
Still no luck. It is weird that it won't show any error inside system.log.user1324762– user13247622016年11月23日 23:04:27 +00:00Commented Nov 23, 2016 at 23:04
etc/widget.xmlin your module with declaration of your widget?