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):
70 "tutorial/tutorial-series/part-2/files/acp/database/install_com.woltlab.wcf.people.birthday.php",
71 "files/acp/database/install_com.woltlab.wcf.people.birthday.php"
74 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`.
77 ## Setting Birthday in ACP
79 To set the birthday of a person, we only have to add another form field with an event listener:
83 "tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdayPersonAddFormListener.class.php",
84 "files/lib/system/event/listener/BirthdayPersonAddFormListener.class.php"
90 <eventlistener name="createForm@wcf\acp\form\PersonAddForm">
91 <environment>admin</environment>
92 <eventclassname>wcf\acp\form\PersonAddForm</eventclassname>
93 <eventname>createForm</eventname>
94 <listenerclassname>wcf\system\event\listener\BirthdayPersonAddFormListener</listenerclassname>
99 in `eventListener.xml`, [see below](#eventlistenerxml).
101 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.
102 It is important to set `<inherit>1</inherit>` so that the event listener is also executed for `PersonEditForm`, which extends `PersonAddForm`.
104 The language item `wcf.person.birthday` used in the label is the only new one for this package:
108 "tutorial/tutorial-series/part-2/language/de.xml",
114 "tutorial/tutorial-series/part-2/language/en.xml",
119 ## Adding Birthday Table Column in ACP
121 To add a birthday column to the person list page in the ACP, we need three parts:
123 1. an event listener that makes the `birthday` database table column a valid sort field,
124 1. a template listener that adds the birthday column to the table’s head, and
125 1. a template listener that adds the birthday column to the table’s rows.
127 The first part is a very simple class:
131 "tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdaySortFieldPersonListPageListener.class.php",
132 "files/lib/system/event/listener/BirthdaySortFieldPersonListPageListener.class.php"
135 !!! 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."
137 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).
138 The code for the table head is similar to the other `th` elements:
141 <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>
144 For the table body’s column, we need to make sure that the birthday is only show if it is actually set:
147 <td class="columnDate columnBirthday">{if $person->birthday}{@$person->birthday|strtotime|date}{/if}</td>
151 ## Adding Birthday in Front End
153 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”.
155 To add the birthday as a valid sort field, we use `BirthdaySortFieldPersonListPageListener` just as in the ACP.
156 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:
160 "tutorial/tutorial-series/part-2/templates/__personListBirthdaySortField.tpl",
161 "templates/__personListBirthdaySortField.tpl"
164 !!! 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."
166 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.
168 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:
172 "tutorial/tutorial-series/part-2/templates/__personListBirthday.tpl",
173 "templates/__personListBirthday.tpl"
177 ## `templateListener.xml`
179 The following code shows the `templateListener.xml` file used to install all mentioned template listeners:
183 "tutorial/tutorial-series/part-2/templateListener.xml",
184 "templateListener.xml"
187 In cases where a template is used, we simply use the `include` syntax to load the template.
190 ## `eventListener.xml`
192 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.
196 "tutorial/tutorial-series/part-2/eventListener.xml",
203 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>`):
207 "tutorial/tutorial-series/part-2/package.xml",
213 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.
215 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).