1 # Part 2: Event and Template Listeners
3 In the [first part](part_1.md) of this tutorial series, we have created the base structure of our people management package.
4 In further parts, we will use the package of the first part as a basis to directly add new features.
5 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.
7 The goal of the small plugin that will be created in this part is to add the birthday of the managed people.
8 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.
11 ## Package Functionality
13 The package should provide the following possibilities/functions:
15 - List person’s birthday (if set) in people list in the ACP
16 - Sort people list by birthday in the ACP
17 - Add or remove birthday when adding or editing person
18 - List person’s birthday (if set) in people list in the front end
19 - Sort people list by birthday in the front end
24 We will use the following package installation plugins:
26 - [database package installation plugin](../../package/pip/database.md),
27 - [eventListener package installation plugin](../../package/pip/event-listener.md),
28 - [file package installation plugin](../../package/pip/file.md),
29 - [language package installation plugin](../../package/pip/language.md),
30 - [template package installation plugin](../../package/pip/template.md),
31 - [templateListener package installation plugin](../../package/pip/template-listener.md).
33 For more information about the event system, please refer to the [dedicated page on events](../../php/api/events.md).
38 The package will have the following file structure:
45 │ │ └── install_com.woltlab.wcf.people.birthday.php
50 │ ├── BirthdayPersonAddFormListener.class.php
51 │ └── BirthdaySortFieldPersonListPageListener.class.php
56 ├── templateListener.xml
58 ├── __personListBirthday.tpl
59 └── __personListBirthdaySortField.tpl
63 ## Extending Person Model
65 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).
66 To add the birthday to the model, we need to create an additional database table column using the [`database` package installation plugin](../../package/pip/database.md):
69 --8<-- "tutorial/tutorial-series/part-2/files/acp/database/install_com.woltlab.wcf.people.birthday.php"
72 If we have a [`Person` object](part_1.md#person), 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`.
75 ## Setting Birthday in ACP
77 To set the birthday of a person, we only have to add another form field with an event listener:
80 --8<-- "tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdayPersonAddFormListener.class.php"
86 <eventlistener name="createForm@wcf\acp\form\PersonAddForm">
87 <environment>admin</environment>
88 <eventclassname>wcf\acp\form\PersonAddForm</eventclassname>
89 <eventname>createForm</eventname>
90 <listenerclassname>wcf\system\event\listener\BirthdayPersonAddFormListener</listenerclassname>
95 in `eventListener.xml`, [see below](#eventlistenerxml).
97 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.
98 It is important to set `<inherit>1</inherit>` so that the event listener is also executed for `PersonEditForm`, which extends `PersonAddForm`.
100 The language item `wcf.person.birthday` used in the label is the only new one for this package:
103 --8<-- "tutorial/tutorial-series/part-2/language/de.xml"
107 --8<-- "tutorial/tutorial-series/part-2/language/en.xml"
111 ## Adding Birthday Table Column in ACP
113 To add a birthday column to the person list page in the ACP, we need three parts:
115 1. an event listener that makes the `birthday` database table column a valid sort field,
116 1. a template listener that adds the birthday column to the table’s head, and
117 1. a template listener that adds the birthday column to the table’s rows.
119 The first part is a very simple class:
122 --8<-- "tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdaySortFieldPersonListPageListener.class.php"
125 !!! info "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."
127 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](#templatelistenerxml).
128 The code for the table head is similar to the other `th` elements:
131 <th class="columnDate columnBirthday{if $sortField == 'birthday'} active {@$sortOrder}{/if}"><a href="{link controller='PersonList'}pageNo={@$pageNo}&sortField=birthday&sortOrder={if $sortField == 'birthday' && $sortOrder == 'ASC'}DESC{else}ASC{/if}{/link}">{lang}wcf.person.birthday{/lang}</a></th>
134 For the table body’s column, we need to make sure that the birthday is only show if it is actually set:
137 <td class="columnDate columnBirthday">{if $person->birthday}{@$person->birthday|strtotime|date}{/if}</td>
141 ## Adding Birthday in Front End
143 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”.
145 To add the birthday as a valid sort field, we use `BirthdaySortFieldPersonListPageListener` just as in the ACP.
146 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:
149 --8<-- "tutorial/tutorial-series/part-2/templates/__personListBirthdaySortField.tpl"
152 !!! info "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."
154 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.
156 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:
159 --8<-- "tutorial/tutorial-series/part-2/templates/__personListBirthday.tpl"
163 ## `templateListener.xml`
165 The following code shows the `templateListener.xml` file used to install all mentioned template listeners:
168 --8<-- "tutorial/tutorial-series/part-2/templateListener.xml"
171 In cases where a template is used, we simply use the `include` syntax to load the template.
174 ## `eventListener.xml`
176 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.
179 --8<-- "tutorial/tutorial-series/part-2/eventListener.xml"
185 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>`):
188 --8<-- "tutorial/tutorial-series/part-2/package.xml"
193 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.
195 The complete source code of this part can be found on [GitHub]({jinja{ config.repo_url }}tree/{jinja{ config.edit_uri.split("/")[1] }}/snippets/tutorial/tutorial-series/part-2).