Skip to main content

Customising widgets

Widgets can be added in Photon by creating a controller class, registering the widget in the JaduHomepageWidgets database table and defining a service tagged as photon.widget and the id of the widget in the CMS database.

The controller class for a widget is a standard Symfony controller that takes a request and returns a response. The controller must be an invokable class to be compatible with the widget system.

The widget service must be tagged with photon.widget and given an id. The id is used when the CMS identifies the controller to invoke, where a matching controller can not be found, the default widget controller is reverted to.

note

When creating the service, the parent photon.controller.widget can be used to simplify the definition. This will set all of the dependencies that are required by the Widget base class.

Defining the controller:

<?php

namespace Photon\CmsProject\Widget;


use Photon\CmsEngine\Model\PublishingContent\Homepage\HomepageWidget;
use Photon\Core\Controller\Widget;
use Symfony\Component\HttpFoundation\Response;

class ContentWidget extends Widget
{
/**
* @param HomepageWidget $widget
* @param bool $isPreview
*
* @return Response
*/
public function __invoke(HomepageWidget $widget, $isPreview = false)
{
if ($isPreview && empty($widget->getSettingValue('title')) && empty($widget->getSettingValue('content'))) {
return new Response('Please apply some settings to this widget.');
}
return $this->render('widgets/content.html.twig', [
'title' => $widget->getSettingValue('title'),
'content' => $widget->getSettingValue('content'),
]);
}
}

Defining the service:

photon_cms_project.widget.website_statistics:
class: Photon\CmsProject\Widget\WebsiteStatisticsWidget
parent: photon.controller.widget
arguments:
- '@photon_cms_engine.repository.website_statistics'
tags:
- { name: photon.widget, widget_id: 31 }