Contao implements a variety of events using the Symfony Event Dispatcher.
These events are often used internally but also provide you with ways to customise
specific flows of the application, in addition to Hooks.
contao.backend_menu_build
This event is dispatched, when the Contao back end menu is built. The event contains
references to the menu factory as well as the menu tree object of the
KnpMenuBundle. This can be used to alter the back end menu to your
needs. An example of this can also be found in the Back End Routes
guide.
This event is dispatched after Contao generated all its necessary symlinks (e.g.
from web/files/ to all the public folders in files/). The event object returns
a list of custom symlinks to be built and offers the possibility to add your own
custom symlink to the list.
This event is dispatched when all the available image sizes for selection in the
back end are collected. The event allows to return all the currently set image sizes
and lets you define a new set of image sizes. It also returns back end user, for
which these image sizes are collected. This allows you to customize the list of
offered image sizes.
This event is triggered when a preview URL for the front end within the back end
is created. The event provides the set ID, key and query and allows you to set or
override the query parameter.
When a request to the preview controller is made, this event can be used to redirect
the user to a specific front end URL within the preview mode. Otherwise a redirect
to the root page is made. Contao uses this to translate a request to the the preview
controller into a front end request URL.
This event is triggered when the /robots.txt route is called. The event allows you
to retrieve the webignition\RobotsTxt\File\File object of the dynamically generated
robots.txt, which allows you to add your own custom records programatically. See
the README of webignition/robots-txt-file for more
details.
// src/EventListener/RobotsTxtListener.php
namespaceApp\EventListener;useContao\CoreBundle\Event\ContaoCoreEvents;useContao\CoreBundle\Event\RobotsTxtEvent;useSymfony\Component\EventDispatcher\Attribute\AsEventListener;usewebignition\RobotsTxt\Directive\Directive;usewebignition\RobotsTxt\Directive\UserAgentDirective;usewebignition\RobotsTxt\Inspector\Inspector;usewebignition\RobotsTxt\Record\Record;#[AsEventListener(ContaoCoreEvents::ROBOTS_TXT)]
classRobotsTxtListener{publicfunction__invoke(RobotsTxtEvent$event):void{$file=$event->getFile();$inspector=newInspector($file);// Check if a "User-Agent: *" directive is not already present
if(0===$inspector->getDirectives()->getLength()){$record=newRecord();$userAgentDirectiveList=$record->getUserAgentDirectiveList();$userAgentDirectiveList->add(newUserAgentDirective('*'));$file->addRecord($record);}foreach($file->getRecords()as$record){$directiveList=$record->getDirectiveList();$directiveList->add(newDirective('Disallow','/foo/'));}}}
contao.slug_valid_characters
This event event is triggered when the valid slug characters options in the back
end are generated. The event allows you to set custom options.
This event event is triggered when the available page types are collected in the
PageTypeOptionsListener for the type select of tl_page. The event allows you
to add or remove options.
// src/EventListener/FilterPageTypeListener.php
namespaceApp\EventListener;useContao\CoreBundle\Event\FilterPageTypeEvent;useSymfony\Component\EventDispatcher\Attribute\AsEventListener;#[AsEventListener]
classFilterPageTypeListener{publicfunction__invoke(FilterPageTypeEvent$event):void{// Removes the "redirect" page type from the available page types
$event->removeOption('redirect');}}
contao.sitemap
This event is triggered in Contao’s SitemapController when the sitemap is built. The event allows you to modify the XML DOM of the
sitemap. The event also stores for which website roots this sitemap was created.
This event is triggered for each newsletter mail and allows you to customize
the newsletter text and HTML content, prevent the submission or perform other
related actions to the newsletter (e.g. logging).
The event is triggered when sending a newsletter preview as well as when sending
a newsletter to real recipients.
// src/EventListener/SendNewsletterListener.php
namespaceApp\EventListener;useContao\NewsletterBundle\Event\SendNewsletterEvent;useSymfony\Component\EventDispatcher\Attribute\AsEventListener;#[AsEventListener]
classSendNewsletterListener{publicfunction__invoke(SendNewsletterEvent$event):void{// Skip sending under certain conditions
if(!preg_match('/@contao\.org/i',$event->getRecipientAddress())){$event->setSkipSending(true);}if($event->isHtmlAllowed()){// Get the parsed HTML content of the newsletter
$html=$event->getHtml();// Use the original, unparsed HTML content of the newsletter
$html=$event->getNewsletterValue('content');// Get any data from the newsletter
$id=$event->getNewsletterValue('id');// Get any data from the recipient
$greeting=$event->getRecipientValue('greeting');// Overwrite the HTML with custom content
$event->setHtml($html);}}}
FetchArticlesForFeedEvent
This event is dispatched when a news feed is created and is used to collect the news articles before adding them to the
feed. The event holds references to the feed, the page and the request and allows news articles to be added to it. This
event is also used by the news bundle itself, so if you want to alter the collection of news articles (before they are
added to the feed) make sure that your event listener has a lower priority.
This event also allows you to alter the feed directly.
// src/EventListener/FetchArticlesForFeedEventListener.php
namespaceApp\EventListener;useContao\NewsBundle\Event\FetchArticlesForFeedEvent;useSymfony\Component\EventDispatcher\Attribute\AsEventListener;#[AsEventListener]
classFetchArticlesForFeedEventListener{publicfunction__invoke(FetchArticlesForFeedEvent$event):void{// Add additional news articles to the feed
$articles=NewsModel::findBy(…);$event->addArticles($articles->getModels());// Set a logo for the feed
$event->getFeed()->setLogo(…);}}
TransformArticleForFeedEvent
This event is dispatched when a news article is transformed to a news feed item node. The event holds a reference to the
news archive, feed, page, request and the base URL. The news bundle uses this event to create a feed item based on a
news record and sets this item in the event. You can use this event to further alter the feed item (if your event
listener has a lower priority).
// src/EventListener/TransformArticleForFeedEventListener.php
namespaceApp\EventListener;useContao\NewsBundle\Event\TransformArticleForFeedEvent;useSymfony\Component\EventDispatcher\Attribute\AsEventListener;#[AsEventListener(priority: -100)]
classTransformArticleForFeedEventListener{publicfunction__invoke(TransformArticleForFeedEvent$event):void{if(!($item=$event->getItem())){return;}// Set a summary
$item->setSummary(…);}}