public function onFetchStatisticsWidgetClassname( string $widget ) : string

Fires while instantiating a third-party widget for the dashboard and statistics pages.


Description

The plugins MUST include here all the resources needed to the widget, otherwise it wouldn't be possible to instantiate the returned classes.

The event should return the PHP class name related to the requested widget.

In case the specified class doesn't exist, an exception will be thrown. The class MUST extend the VREStatisticsWidget abstract class.

NOTE: the file in which the widget class is located MUST be explicitly loaded.


Parameters

$widget

(string)  The name of the widget to include, which is equals to the base name of the file (without .php).

Return Value

String. The classname that will be used to instantiate the widget object.


Example

The example provides the instantiation of the "test" widget located within the VikRestaurants - E4J plugin.

/plugins/vikrestaurants/e4j/widgets/test.php
/**
 * Trigger filter to let the plugins include external widgets.
 * The plugins MUST include the resources needed, otherwise
 * it wouldn't be possible to instantiate the returned classes.
 *
 * @param   string  $widget  The name of the widget to include.
 *
 * @return  string  The classname of the widget.
 */
public function onFetchStatisticsWidgetClassname($widget)
{
    // define list of supported widgets
    $supported = [
        'test',
    ];

    if (in_array($widget, $supported)) {
        // include widget class handler
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'widgets' . DIRECTORY_SEPARATOR . $widget . '.php';

        // set widget class name
        return 'VREStatisticsWidget' . ucfirst($widget);
    }
}

Changelog

Version Description
1.8 Introduced.
Last Update: 2023-12-29 14:15
Helpful?