Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.

Update extending pages (EventSubscriberInterface) #172

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions source/includes/_plugin_extending_pages.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
### Extending Landing Pages

Make sure you reigster the event listener in your `config.php`:

```php
<?php
// plugins/HelloWorldBundle/config.php

use MauticPlugin\HelloWorldBundle\EventListener\PageSubscriber;

return [
'services' => [
'events' => [
'plugin.helloworld.page.subscriber' => array(
'class' => PageSubscriber::class,
'arguments' => [
'mautic.helper.templating'
]
)
],
],
];

```
```php
<?php
// plugins/HelloWorldBundle/EventListener/PageSubscriber.php

namespace MauticPlugin\HelloWorldBundle\EventListener;

use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\CoreBundle\Helper\TemplatingHelper;
use Mautic\PageBundle\PageEvents;
use Mautic\PageBundle\Event\PageBuilderEvent;
use Mautic\PageBundle\Event\PageSendEvent;
use Mautic\PageBundle\Event\PageDisplayEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Class PageSubscriber
*/
class PageSubscriber extends CommonSubscriber
class PageSubscriber implements EventSubscriberInterface
{
/**
* @var TemplatingHelper
*/
private $templating;

public function __construct(TemplatingHelper $templating)
{
$this->templating = $templating;
}

/**
* @return array
Expand All @@ -36,8 +68,9 @@ class PageSubscriber extends CommonSubscriber
public function onPageBuild(PageBuilderEvent $event)
{
// Add page tokens
$content = $this->templating->render('HelloWorldBundle:SubscribedEvents\PageToken:token.html.php');
$event->addTokenSection('helloworld.token', 'plugin.helloworld.header', $content);
Copy link
Member Author

@dennisameling dennisameling Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was deleted in Mautic 3.0.0, without any hint as to what should be used instead. $event->addToken() seems to do the job in my testing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($event->tokensRequested('{myToken}')) {
$event->addToken('{myToken}', 'My Token');
}

// Add AB Test Winner Criteria
$event->addAbTestWinnerCriteria(
Expand All @@ -58,15 +91,18 @@ class PageSubscriber extends CommonSubscriber
/**
* Search and replace tokens with content
*
* @param PageSendEvent $event
* @param PageDisplayEvent $event
*/
public function onPageDisplay(PageSendEvent $event)
public function onPageDisplay(PageDisplayEvent $event)
{
// Get content
$content = $event->getContent();

// Search and replace tokens
$content = str_replace('{hello}', 'world!', $content);
$html = $this->templating->getTemplating()->render(
'HelloWorldBundle:SubscribedEvents\PageToken:token.html.php'
);
$content = str_replace('{myToken}', $html, $content);

// Set updated content
$event->setContent($content);
Expand All @@ -82,4 +118,4 @@ Page tokens are handled exactly the same as [Email Tokens](#page-tokens).

#### Page A/B Test Winner Criteria

Custom landing page A/B test winner criteria is handled exactly the same as [page A/B test winner criteria](#page-a/b-test-winner-criteria) with the only differences being that the `callback` function is passed `Mautic\PageBundle\Entity\Page $page` and `Mautic\PageBundle\Entity\Page $parent` instead. Of course `$children` is an ArrayCollection of Page entities as well.
Custom landing page A/B test winner criteria is handled exactly the same as [page A/B test winner criteria](#page-a/b-test-winner-criteria) with the only differences being that the `callback` function is passed `Mautic\PageBundle\Entity\Page $page` and `Mautic\PageBundle\Entity\Page $parent` instead. Of course `$children` is an ArrayCollection of Page entities as well.