Widget is part of Astroid's Element system and is used to create display elements in Layout Builder or Article Builder. The structure of a widget includes an xml file and a php file.
The XML file is used to declare fields like a Joomla Extension
The PHP file is used to query, execute and display results
To create or override a widget you need to create a folder {WIDGET_NAME} in the JROOT/media/templates/site/{YOUR_TEMPLATE_NAME}/astroid/elements/ folder with an xml file and a php file
{WIDGET_NAME}.xml
{WIDGET_NAME}.php
The XML structure
I am taking an example of a simple xml structure of Icons Widget below
title (required): Declare the name of the Widget you want to create.
description: Declare the description of the Widget
In PHP file you simply display what you want with widget. You can use $param->get() variable to call out the widget params defined in XML file. Let's see the example below.
<?php// No direct access.defined('_JEXEC')ordie;extract($displayData);$hello = $params->get('hello_statement','Hello World!');echo $hello;
Use Widget
To use the your widget, go to Administrator > Template Manager > select your Template > Template Options > Layout Builder
Create a block and Add Element. In the Element selection section, you will see your Widget as shown in the example image below. I created a Test Widget.