In the first part of this tutorial series, we have created the base structure of our people management package.
In further parts, we will use the package of the first part as a basis to directly add new features.
In order to explain how event listeners and template works, however, we will not directly adding a new feature to the package by altering it in this part, but we will assume that somebody else created the package and that we want to extend it the “correct” way by creating a plugin.
The goal of the small plugin that will be created in this part is to add the birthday of the managed people.
As in the first part, we will not bother with careful validation of the entered date but just make sure that it is a valid date.
The existing model of a person only contains the person’s first name and their last name (in additional to the id used to identify created people).
To add the birthday to the model, we need to create an additional database table column using the database package installation plugin:
If we have a Person object, this new property can be accessed the same way as the personID property, the firstName property, or the lastName property from the base package: $person->birthday.
As BirthdayPersonAddFormListener extends AbstractEventListener and as the name of relevant event is createForm, AbstractEventListener internally automatically calls onCreateForm() with the event object as the parameter.
It is important to set <inherit>1</inherit> so that the event listener is also executed for PersonEditForm, which extends PersonAddForm.
The language item wcf.person.birthday used in the label is the only new one for this package:
<?phpnamespacewcf\system\event\listener;usewcf\page\SortablePage;/** * Makes people's birthday a valid sort field in the ACP and the front end. * * @author Matthias Schmidt * @copyright 2001-2022 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\System\Event\Listener */finalclassBirthdaySortFieldPersonListPageListenerextendsAbstractEventListener{/** * @see SortablePage::validateSortField() */publicfunctiononValidateSortField(SortablePage$page):void{$page->validSortFields[]='birthday';}}
We use SortablePage as a type hint instead of wcf\acp\page\PersonListPage because we will be using the same event listener class in the front end to also allow sorting that list by birthday.
As the relevant template codes are only one line each, we will simply put them directly in the templateListener.xml file that will be shown later on.
The code for the table head is similar to the other th elements:
1
<th class="columnDate columnBirthday{if$sortField=='birthday'} active {$sortOrder}{/if}"><a href="{linkcontroller='PersonList'}pageNo={@$pageNo}&sortField=birthday&sortOrder={if$sortField=='birthday'&&$sortOrder=='ASC'}DESC{else}ASC{/if}{/link}">{lang}wcf.person.birthday{/lang}</a></th>
For the table body’s column, we need to make sure that the birthday is only show if it is actually set:
In the front end, we also want to make the list sortable by birthday and show the birthday as part of each person’s “statistics”.
To add the birthday as a valid sort field, we use BirthdaySortFieldPersonListPageListener just as in the ACP.
In the front end, we will now use a template (__personListBirthdaySortField.tpl) instead of a directly putting the template code in the templateListener.xml file:
You might have noticed the two underscores at the beginning of the template file. For templates that are included via template listeners, this is the naming convention we use.
Putting the template code into a file has the advantage that in the administrator is able to edit the code directly via a custom template group, even though in this case this might not be very probable.
To show the birthday, we use the following template code for the personStatistics template event, which again makes sure that the birthday is only shown if it is actually set:
There are two event listeners that make birthday a valid sort field in the ACP and the front end, respectively, and the third event listener takes care of setting the birthday.
The only relevant difference between the package.xml file of the base page from part 1 and the package.xml file of this package is that this package requires the base package com.woltlab.wcf.people (see <requiredpackages>):
This concludes the second part of our tutorial series after which you now have extended the base package using event listeners and template listeners that allow you to enter the birthday of the people.
The complete source code of this part can be found on GitHub.