Configurable dashboard widget implementation 

New in version 14.0

Widget authors can implement configurable widgets by using to the renderer interface \TYPO3\CMS\Dashboard\Widgets\WidgetRendererInterface which allows to defining settings in their widget renderer.

Settings are automatically validated and processed using the Settings API. All types that are available for site settings definition are available: Definition types.

Example: A configurable widget implementation 

EXT:my_extension/Classes/Widgets/ConfigurableWidget.php
<?php

use TYPO3\CMS\Dashboard\Widgets\WidgetContext;
use TYPO3\CMS\Dashboard\Widgets\WidgetRendererInterface;
use TYPO3\CMS\Dashboard\Widgets\WidgetResult;
use TYPO3\CMS\Core\Settings\SettingDefinition;

class ConfigurableWidget implements WidgetRendererInterface
{
    public function getSettingsDefinitions(): array
    {
        return [
            new SettingDefinition(
                key: 'title',
                type: 'string',
                default: 'Default Title',
                label: 'LLL:EXT:my_extension/Resources/Private/Language/locallang_my_widget.xlf:settings.label',
                description: 'LLL:EXT:my_extension/Resources/Private/Language/locallang_my_widget.xlf:settings.description.label',
            ),
            new SettingDefinition(
                key: 'limit',
                type: 'int',
                default: 10,
                label: 'LLL:EXT:my_extension/Resources/Private/Language/locallang_my_widget.xlf:settings.limit',
                description: 'LLL:EXT:my_extension/Resources/Private/Language/locallang_my_widget.xlf:settings.description.limit',
            ),
        ];
    }

    public function renderWidget(WidgetContext $context): WidgetResult
    {
        $settings = $context->settings;
        $title = $settings->get('title');
        $limit = $settings->get('limit');

        // Use settings to customize widget output
        return new WidgetResult(
            content: '<!-- widget content -->',
            label: $title,
            refreshable: true
        );
    }
}
Copied!