public function onFetchStatisticsWidgetClassname(string $widget) : string

Fires while instantiating a third-party widget for the Dashboard and Analytics 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 VAPStatisticsWidget 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 VikAppointments - E4J plugin.

/plugins/vikappointments/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 = array(
        'foo_test',
    );

    if (in_array($widget, $supported))
    {
        // include widget class handler
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'widgets' . DIRECTORY_SEPARATOR . $widget . '.php';
	
        // convert widget name in Pascal Case: "foo_test" becomes "FooTest"
        $widget = str_replace(' ', '', ucwords(str_replace('_', ' ', $widget)));

        // return widget class name (e.g. "VAPStatisticsWidgetFooTest")
        return 'VAPStatisticsWidget' . $widget;
    }
}

Changelog

VersionDescription
1.7 Introduced.
Last Update: 2021-10-06 17:22
Helpful?
This site uses cookies. By continuing to browse you accept their use. Further information