Page 1 of 1

Dependency injection

Posted: Wed May 08, 2019 5:15 pm
by MarkDHamill
There is an event in /incluces/acp/acp_email.php that interests me:

Code: Select all

		/**
		* Modify custom email template data before we display the form
		*
		* @event core.acp_email_display
		* @var	array	template_data		Array with template data assigned to email template
		* @var	array	exclude				Array with groups which are excluded from group selection
		* @var	array	usernames			Usernames which will be displayed in form
		*
		* @since 3.1.4-RC1
		*/
		$vars = array('template_data', 'exclude', 'usernames');
		extract($phpbb_dispatcher->trigger_event('core.acp_email_display', compact($vars)));

		$template->assign_vars($template_data);
My question is how to modify my services.yml page to carry over this data so I can use the event.

Re: Dependency injection

Posted: Wed May 08, 2019 5:39 pm
by david63
Where do you want to use the event? Events can only be used in listeners so if you want that data otside of the listener you will need to save it in the listener and retrieve it at the place where you are going to use it.

Re: Dependency injection

Posted: Wed May 08, 2019 7:07 pm
by MarkDHamill
I am using it in /event/main_listener.php. I've added my function to the subscribed events and tried various approaches to adding these parameters to my services.yml file, currently adding:

Code: Select all

            - '%template_data%'
            - '%exclude%'
            - '%usernames%'
and trying to hook them in:

Code: Select all

	public function __construct(\phpbb\language\language $language, \phpbb\controller\helper $helper, \phpbb\template\template $template, $php_ext, $template_data, $exclude, $usernames)
	{
		$this->language = $language;
		$this->helper   = $helper;
		$this->template = $template;
		$this->php_ext  = $php_ext;
		$this->template_data = $template_data;
		$this->exclude = $exclude;
		$this->usernames = $usernames;
	}
getting an error like this:
Exception: The service "phpbbservices.selectivemassemails.listener" has a dependency on a non-existent parameter "template_data". Did you mean this: "tables.styles_template_data"?

Re: Dependency injection

Posted: Wed May 08, 2019 7:52 pm
by Paul
You don't inject it into the constructor, it is part of the array for the variable that gets included in the method call to the method you provided for that event.

Re: Dependency injection

Posted: Wed May 08, 2019 8:26 pm
by MarkDHamill
Thank you. Getting there.

I notice in /includes/acp/acp_email.php the event is triggered like this:

Code: Select all

	$vars = array('template_data', 'exclude', 'usernames');
	extract($phpbb_dispatcher->trigger_event('core.acp_email_display', compact($vars)));
So my listener is coded as follows:

Code: Select all

	public function acp_email_display($template_data, $exclude, $usernames)
	{

		$template_data['S_GROUP_OPTIONS'][] = '<option value="-1">' . $this->language->lang('SELECTIVEMASSEMAILS_SELECTED') . '</option>';;

	}
I get this:
[phpBB Debug] PHP Notice: in file [ROOT]/ext/phpbbservices/selectivemassemails/event/main_listener.php on line 128: Indirect modification of overloaded element of phpbb/event/data has no effect
Because $template_data basically doesn't exist and no data is being passed.

I was thinking maybe I needed to add the $event object as a parameter

Code: Select all

	public function acp_email_display($event, $template_data, $exclude, $usernames)
or just pass $vars and extract that but none of this works.

I'm probably missing something basic about how events work.

Re: Dependency injection

Posted: Wed May 08, 2019 8:53 pm
by Paul
You only get one parameter, which is an array that contains as keys the fields from the event, and as values the actual values of those variables.
I suggest you read the extension development documentation, which can be found at area51

Re: Dependency injection

Posted: Wed May 08, 2019 10:47 pm
by MarkDHamill
Thank you. It looks like for core events you get the $event object and that's it so you must assert it and only it as a parameter.