Adding button to Widget

In order to add a button to a widget, a new dependency to an ButtonProviderInterface can be added.

Template

The output itself is done inside of the Fluid template, for example Resources/Private/Templates/Widget/RssWidget.html:

<f:if condition="{button}">
   <a href="{button.link}" target="{button.target}" class="widget-cta">
      {f:translate(id: button.title, default: button.title)}
   </a>
</f:if>

Configuration

The configuration is done through an configured Instance of the dependency, for example Services.yaml:

services:
  # …

  dashboard.buttons.t3news:
    class: 'TYPO3\CMS\Dashboard\Widgets\Provider\ButtonProvider'
    arguments:
      $title: 'LLL:EXT:dashboard/Resources/Private/Language/locallang.xlf:widgets.t3news.moreItems'
      $link: 'https://typo3.org/project/news'
      $target: '_blank'

  dashboard.widget.t3news:
    class: 'TYPO3\CMS\Dashboard\Widgets\RssWidget'
    arguments:
      # …
      $buttonProvider: '@dashboard.buttons.t3news'
      # …
$title

The title used for the button. E.g. an LLL:EXT: reference like LLL:EXT:dashboard/Resources/Private/Language/locallang.xlf:widgets.t3news.moreItems.

The link to use for the button. Clicking the button will open the link.

$target

The target of the link, e.g. _blank. LLL:EXT:dashboard/Resources/Private/Language/locallang.xlf:widgets.t3news.moreItems.

Implementation

An example implementation could look like this:

Classes/Widgets/RssWidget.php:

class RssWidget implements WidgetInterface
{
    /**
     * @var ButtonProviderInterface|null
     */
    private $buttonProvider;

    public function __construct(
        // …
        ButtonProviderInterface $buttonProvider = null,
        // …
    ) {
        $this->buttonProvider = $buttonProvider;
    }

    public function renderWidgetContent(): string
    {
        $this->view->setTemplate('Widget/RssWidget');
        $this->view->assignMultiple([
            // …
            'button' => $this->buttonProvider,
            // …
        ]);
        return $this->view->render();
    }
}