Skip to content

Commit 3457b31

Browse files
authored
Merge pull request #307 from rxu/template-events-priority
Add template events prioritizing docs
2 parents 3256d04 + a30511f commit 3457b31

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

development/extensions/tutorial_events.rst

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,55 @@ text ``DEMO_PAGE``. We will fix the link text in the next section.
8282
``config.php`` file, which will force the template engine to always
8383
look for template listeners when a page is being rendered.
8484

85-
It's important to understand that when phpBB compiles the templates,
86-
there is no current system for determining the priority in which template
87-
listeners from different extensions subscribed to the same event are
88-
compiled. In rare cases some extensions could cause a conflict, in which case
89-
the recommendation is for the extension authors to work together on a solution for their
90-
conflicting template listeners.
85+
Prioritising template event listeners (optional)
86+
------------------------------------------------
9187

88+
In rare cases, conflicts may occur when multiple extensions subscribe to the same template
89+
event using template listeners. To resolve such conflicts, phpBB allows you to control the
90+
order in which template event listeners are compiled by assigning them priorities.
91+
92+
This is done by subscribing a PHP event listener to the
93+
``core.twig_event_tokenparser_constructor`` event and using the
94+
``template_event_priority_array`` variable to define listener priorities.
95+
96+
Example:
97+
98+
.. code-block:: php
99+
100+
<?php
101+
102+
namespace acme\demo\event;
103+
104+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
105+
106+
class main_listener implements EventSubscriberInterface
107+
{
108+
// Subscribe an event listener function to the core.twig_event_tokenparser_constructor
109+
static public function getSubscribedEvents()
110+
{
111+
return [
112+
'core.twig_event_tokenparser_constructor' => 'set_template_event_priority',
113+
];
114+
}
115+
116+
// Give your extension a high priority when rendering the navbar_header_quick_links_after template event.
117+
public function set_template_event_priority($event)
118+
{
119+
$template_event_priority_array = $event['template_event_priority_array'];
120+
$template_event_priority_array['acme_demo'] = [
121+
'event/navbar_header_quick_links_after' => 100,
122+
];
123+
$event['template_event_priority_array'] = $template_event_priority_array;
124+
}
125+
}
126+
127+
In this example, ``100`` is an integer implying the template event priority. Higher values
128+
indicate greater importance, meaning the corresponding template event listener
129+
will be compiled earlier than others listening to the same event.
130+
For example, the content of template event listener which has a priority value of ``100``
131+
will be rendered above/before the same template event listener which has a priority value of ``99``.
132+
If multiple listeners share the same priority value, they will be rendered in the order they were read
133+
from their respective filesystem locations. If no priority value set, it defaults to ``0``.
92134

93135
PHP Core Events & Listeners
94136
===========================

0 commit comments

Comments
 (0)