Development BlogLearning the key functions in writing an extension

Post Reply
User avatar
Topic Starter
Restless Rancor
Great Britain

Site Admin
New Member
Posts: 48
Joined: October 15th, 2018, 3:14 pm
Age: 25
Location: Hampshire, England
Contact:

Learning the key functions in writing an extension

#1

Post by Restless Rancor » October 19th, 2018, 2:24 pm

I've used phpBB for almost ten years now, but only recently decided to learn the language that holds it together. I've picked up bits of HTML and CSS over the years, customizing forum installations to make them unique and have made it my goal to to write extensions for phpBB at an intermediate level.

User's post count anywhere is a simple snippet, so I decided to start there.

That snippet adds to the global template variables (in includes/functions.php) to:
  1. fetch the logged in member's post count, and
  2. fetch that member's user ID

Code: Select all

'S_USER_ID'             => $user->data['user_id'],
'USER_POST_COUNT' 	    => $user->data['user_posts'], 
(We need to fetch the user ID to make the post count clickable and to match the behavior of the profile page and postbit.)

When we've done that, it's as simple as placing it in the template where you want it:

Code: Select all

<!-- EVENT index_body_markforums_before -->
<!-- IF U_MARK_FORUMS -->
	<div class="action-bar compact">
		<a href="{U_MARK_FORUMS}" class="mark-read rightside" accesskey="m" data-ajax="mark_forums_read">{L_MARK_FORUMS_READ}</a>
		{% if S_REGISTERED_USER %}
			{% if USER_POST_COUNT == '1' %}
				<a href="search.php?author_id={S_USER_ID}&sr=posts" class="mark-read leftside" title="Search your posts">You have {USER_POST_COUNT} post.</a>
				{% else %}
				<a href="search.php?author_id={S_USER_ID}&sr=posts" class="mark-read leftside" title="Search your posts">You have {USER_POST_COUNT} posts!</a>
			{% endif %}
		{% endif %}
	</div>
<!-- ENDIF -->
<!-- EVENT index_body_markforums_after -->
I surprised myself yesterday.

Taking a slightly educated guess, I downloaded some extensions that have similar functions to had a look at what they were doing in the listener and to figure out why they were doing it.

It's simple enough to get an extension to display something in a template (you literally make the html file to put into the ext/styles/event folder), but I was struggling to get the code to fetch the post count and user ID to work- the extension would show "You have Posts!"- without an actual post count and the link was for search.php, without the user ID.

I've been chatting to someone who recently helped me get some code to work and they guided me in the right direction, to the point where I had a working extension. I was constantly testing different things and a few times they would say "you need this for that to work" and I'd have already started to work out how to do this, but the key point was updating the services.yml file: that would have taken a while for me to figure out.

I was heading in the right direction to get the user ID and post count added to the global templates, but was struggling with it returning an error of "this is not an array"- the formatting was wrong. When I was given the correct formatting of how the listener file should be, I could see exactly how $this->user->data was working and just why the public function __construct was important.

Code: Select all

public function __construct(user $user, twig $template)
{
    $this->user = $user;
    $this->template = $template;
}

Code: Select all

public function pcoi_header()
{				
	$this->template->assign_vars([
		'S_USER_ID'		=> $this->user->data['user_id'],
		'USER_POST_COUNT'	=> $this->user->data['user_posts'],
	]);
}
The most important part was updating services.yml to tell it to use the @user and @template arguments. It would have taken much longer for me to realize this, so when I was told that file needed updating, I had a working extension.

Code: Select all

services:
    restlessrancor.postcountonindex.listener:
        class: restlessrancor\postcountonindex\event\main_listener
        arguments:
            - '@user'
            - '@template'
        tags:
            - { name: event.listener }
What next?
The extension works, but isn't ready. There's still a few things I'd like to improve and add, each of which will be a learning step:
  • ACP options to display where the information is displayed,
  • Language files (it's currently plain text in the template file),
  • Place the event within some core HTML in the template:
The HTML event is currently index_body_markforums_before.html, so displays before 'Mark Forums Read'. I need to work out how to include it within the <div class="action-bar compact"> which contains the Mark Forums Read text, so it may be directly opposite it and not above and opposite.

But first, I have to get GitHub figured out. Once I have I'll upload the first development version and get a [DEV] topic created on the phpBB forum and in the new subforum on restlessrancor.com: phpBB Extensions.


These aren't the droids you're looking for.

Post Reply

Topic Subscribers

1 user subscribed to this topic: Restless Rancor