core.posting_modify_template_vars correction

These requests for events in core phpBB have been merged into 3.1/Ascraeus and will be available with the next release.
joelk
Registered User
Posts: 7
Joined: Tue Mar 11, 2014 8:00 pm

core.posting_modify_template_vars correction

Post by joelk »

For some reason I cannot login to jira, so I'm posting this here.

in posting.php the event dispatcher is called directly triggering the core.posting_modify_template_vars event, but the event doesn't have any data, so it's impossible to modify the template array within the event listener.

Instead the array should be assigned to a variable and then the event should be triggered with the variable.

Around line 1477 in the beta:
Replace:

Code: Select all

$template->assign_vars(array( ... ));
$phpbb_dispatcher->dispatch('core.posting_modify_template_vars');
With:

Code: Select all

$template_array = array(...);
$vars = array('template_array');
extract($phpbb_dispatcher->trigger_event('core.posting_modify_template_vars', compact($vars)));
$template->assign_vars(array( ... ));
Not shown, but if possible please send the current forum and topic info to the event handler. Basically, I need to know what the current forum is, and whether or not the template is displaying a new post form, or edit the first post of a topic (not a reply).

Thanks!

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: core.posting_modify_template_vars correction

Post by DavidIQ »

Tracker goes off of your account on phpbb.com, not Area 51. It doesn't appear that you are registered on phpbb.com, at least not with the same email address you have here.
Image

joelk
Registered User
Posts: 7
Joined: Tue Mar 11, 2014 8:00 pm

Re: core.posting_modify_template_vars correction

Post by joelk »

Good to know!

Thanks.

User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: core.posting_modify_template_vars correction

Post by rfdy »

This Jira for this can be found here: https://tracker.phpbb.com/browse/PHPBB3-12391

But it looks like the current resolution doesn't address the template variable issue.

User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: core.posting_modify_template_vars correction

Post by rfdy »

A part of our extension adds additional fields to the post field, the options are retrieved in the extension backend and need to be assigned to the template variable so that they are available to the html template on display.

The core.posting_modify_template_vars event seems to be appropriately named for us to make those changes.

aleha
Registered User
Posts: 143
Joined: Tue Mar 26, 2013 2:19 am

Re: core.posting_modify_template_vars correction

Post by aleha »

As nickvergessen pointed out there is no need to pass the whole array. When this specific event fires, assuming that you have injected template code with a template event, you can render this with an event listener. E.g.

Code: Select all

static public function getSubscribedEvents()
{
	return array(
		'core.posting_modify_template_vars'	=> 'render_my_vars',
	);
}
[...]
public function render_my_vars($event)
{
global $template;

$template->assign_vars(array(
    'MY_VAR' => 'my value',
));
}
For a similar simple example of the above have a look at an extension by VSEphpbb.

User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: core.posting_modify_template_vars correction

Post by rfdy »

We would really rather not rely on global variables. The event name implies making changes to the template array, if this is really so undesirable, then you might as well rename the event.

But realistically, we don't see why passing the template array into the event before it gets assigned to the template so bad.

aleha
Registered User
Posts: 143
Joined: Tue Mar 26, 2013 2:19 am

Re: core.posting_modify_template_vars correction

Post by aleha »

But if you inject template code and you wish to render it, you must use $template which is global. I don't think you can avoid this global (maybe with twig? I am not sure atm).

As it was pointed out to me at the irc, just pass it as a service in services.yml of your extension, use a constructor in your class that renders the variables, and use it in the same class as a protected variable. So same job can be done without globals. As VSE+ pointed out, have a look at the following two links.

https://github.com/nickvergessen/phpbb- ... es.yml#L45
https://github.com/nickvergessen/phpbb- ... er.php#L45
Last edited by aleha on Thu May 01, 2014 5:07 pm, edited 1 time in total.

User avatar
PayBas
Registered User
Posts: 305
Joined: Tue Jul 29, 2008 6:08 pm
Contact:

Re: core.posting_modify_template_vars correction

Post by PayBas »

One of the key points of the new extension system is to avoid having to use globals. If you cannot modify the output using the event, but have to rely on a reassignment of template vars, then the event is basically not doing it's job or not powerful enough, and needs to be changed. I think overruling already assigned template vars should be a last resort, not common practice.

User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: core.posting_modify_template_vars correction

Post by rfdy »

We don't want to change the $template itself. We just want to assign more variables to it, so that our template event handler has access to them. Isn't that the point of the event, to modify the assigned variables before they get assigned?

Post Reply