I'm trying to create an extension for phpBB3.1 to use on my forum. This extension is meant to replace the username that is displayed on the template pages with the custom profile field "display_name".
The yml and the composer.json files are in place and the extension is enabled in ACP.
I'm having trouble understanding when the listener will fire this event (before, during, after the core function is used), and what exactly should I be modifying in the $event?
I tried modifying the $username_string and saving it back into the $event, but that strips away all the link functionality and styling of the $username_string that the event returns.
The current listener code:
Code: Select all
<?php
namespace company\displayname\event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class main_listener implements EventSubscriberInterface
{
protected $pfm;
public function __construct(\phpbb\profilefields\manager $pfm, \phpbb\template\template $template)
{
$this->pfm = $pfm;
$this->template = $template;
}
/**
* Assign functions defined in this class to event listeners in the core
*
* @return array
*/
static public function getSubscribedEvents()
{
return array(
'core.modify_username_string' => 'replace_username_with_display_name',
);
}
public function replace_username_with_display_name($event)
{
$user_id = $event['user_id'];
//retrieve the display name profile field for the user
if(!empty($user_id))
{
$userProfileFields = $this->pfm->grab_profile_fields_data($user_id);
if(!empty($userProfileFields[$user_id]['display_name']['value']))
{
$username_string = $userProfileFields[$user_id]['display_name']['value'];
$this->template->assign_var('USERNAME', $username_string);
}
}
}
}
What are your suggestions on how to handle this sort of extension functionality? I'd also appreciate some tips on how to figure out what the $event and $template variables contain. I've tried printing them out to file, but they lock up the system and give a memory allocation error. I tried looking at the phpbb "core" files, but they seem to mostly define the functions, not the data.