How to create widgets yourself?
To implement a very simple functionality, I tried to create a simple widget myself. Unfortunately, I failed at the very first attempt.
Today I tried again and wrote a widget that was supposed to do nothing more than output the text ‘Hello, world!’
I followed the procedure described in the Hubzilla help (doc/Widgets.md).
It says that you can create a widget using two principles:
1. class-based according to the scheme
<?php
namespace Zotlabs\Widget;
class Slugfish {
function widget($args) {
... widget code goes here....
... The function returns a string which is the HTML content of the widget....
... $args is a named array which is passed any [var] variables from the layout editor...
... For instance [widget=slugfish][var=count]3[/var][/widget] will populate $args with...
... [ ‘count’ => 3 ]
...
}
or 2. as a simple function according to the scheme
<?php
function widget_slugfish($args) {..
... widget code goes here. See above information for class-based widgets for details.
}
So I created two test widgets:
Testwidget.php:
<?php
namespace Zotlabs\Widget;
class Testwidget {
function widget($args) {
return ‘Hello, world!’;
}
Testwidget2.php:
<?php
function Testwidget2($args) {
return ‘Hello, world!’;
}
According to the ‘instructions’ in the help section, the widget file should then be stored in
Zotlabs/Widget/
. So now there are two files
Zotlabs/Widget/Testwidget.php
and
Zotlabs/Widget/Testwidget2.php
.
In the PDL editor, the widgets can now also be found under ITEMS. But if I place them in any module in the sidebar, a 500 error occurs when I access the corresponding page:
This site is not working
klacker.org is currently unable to handle this request.
HTTP ERROR 500
Only removing the widget sorts out the page so that it works again.
This is frustrating because my ambitions are completely thwarted at the simplest point of entry. I suspect the instructions are out of date. Is that possible?
How can I create a simple widget that outputs a small snippet of html code? I really couldn't find anything up to date in the old help (in the sources, because not all documents from doc are offered in the Hubzilla help).
I think that's the main problem with Hubzilla. Nobody can contribute because nobody, except for the trained developers, knows how it works.