Skip to content

Commit ccc3f56

Browse files
committed
Add template event listener prioritizing docs.
Requires phpbb/phpbb#6770 to get merged.
1 parent 3256d04 commit ccc3f56

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

development/extensions/tutorial_events.rst

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,68 @@ 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+
---------------------------------------
87+
88+
In rare cases, some extensions could cause a conflict when template listeners
89+
from different extensions are subscribed to the same template event. In such cases
90+
phpBB allows to assign the priority to template event listeners, which allows
91+
to determine the order template event listeners will be compiled.
92+
This can be accomplished using PHP core event listener subscribed to the
93+
``core.twig_event_tokenparser_constructor`` core event, which should use
94+
``template_event_priority_array`` array variable to assign the template event listener priority.
95+
``template_event_priority_array`` array has the following format:
96+
97+
::
98+
99+
'<author>_<extension_name>' => [
100+
'event/<template_event_name>' => <priority_number>,
101+
],
102+
103+
Example:
104+
105+
.. code-block:: php
106+
107+
<?php
108+
109+
namespace acme\demo\event;
110+
111+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
112+
113+
class main_listener implements EventSubscriberInterface
114+
{
115+
/**
116+
* Assign functions defined in this class to event listeners in the core
117+
*
118+
* @return array
119+
*/
120+
static public function getSubscribedEvents()
121+
{
122+
return [
123+
'core.twig_event_tokenparser_constructor' => 'set_template_event_priority',
124+
];
125+
}
91126
127+
/**
128+
* Assign priority to template event listener
129+
*
130+
* @param \phpbb\event\data $event The event object
131+
*/
132+
public function set_template_event_priority($event)
133+
{
134+
$template_event_priority_array = $event['template_event_priority_array'];
135+
$template_event_priority_array['acme_demo'] = [
136+
'event/navbar_header_quick_links_after' => $priority,
137+
];
138+
$event['template_event_priority_array'] = $template_event_priority_array;
139+
}
140+
}
141+
142+
In this example, ``$priority`` is an integer, the value of which defaults to 0.
143+
Setting this integer to higher values equals more importance and therefore that
144+
template event listener will be compiled earlier than others subscribed to the same template event.
145+
In case of equal priority values, template event listeners will be compiled in the order
146+
they have been read from their locations.
92147

93148
PHP Core Events & Listeners
94149
===========================

0 commit comments

Comments
 (0)