4. Listing entities
4.1. Add a route
Section titled “4.1. Add a route”-
Add the following use statements to the
Eventclass: -
Add the following to the attributes of the
Eventclass: -
Add the following to the
handlerssection of the attributes of theEventclass: -
Add the following to the
linkssection of the attributes insrc/Entity/Event.php:
The entire Event.php file at this point
-
Rebuild caches
Run
drush cache:rebuild -
Visit
/admin/content/eventsNote that a route is provided and a list of entities is provided with Edit and Delete operation links for each entity.
By not showing at least the title of each event the list is not actually usable so we need to provide a specialized list builder.
4.2. Add a specialized list builder
Section titled “4.2. Add a specialized list builder”-
Create a
src/Controllerdirectory -
Add a
src/Controller/EventListBuilder.phpfile with the following:
More information on the above
-
Separate methods:
List builders build the table header and the table rows in separate methods.
-
Translation:
The base
EntityListBuilderclass, like many other base classes in Drupal, provides at()function that can be used to translate strings. -
Array merging:
Arrays with string keys can be merged in PHP by “adding” them. Because the base class provides the operations column we put our own part of the header first and add the part from the parent last.
-
Inline type hint:
Because
EntityListBuilderInterface, the interface for list builders, dictates that we type hint the$eventvariable withEntityInterfaceinstead of our more specificEventInterface, IDEs are not aware that the$eventvariable has the methodsgetTitle()andgetDate()in this case. To inform IDEs that these methods are in fact available an inline type hint can be added to the$eventvariable. -
Entity links:
Entities have a
toLink()method to generate links with a specified link text to a specified link relation of the entity. By default a link with the entity label as link text to thecanonicallink relation is generated which is precisely what we want here. -
Date formatting:
Because the
getDate()method returns a date object we can attain the formatted date by using itsformat()method. If the same date format is to be used in multiple places on the site, hardcoding it here can lead to duplication or worse, inconsistent user interfaces. To prevent this, Drupal associates PHP date formats with machine-readable names to form a Date format configuration entity. (More on configuration entities in general below.) That way the name, such asshort,mediumorlongcan be used without having to remember the associated PHP date format. This also allows changing the PHP date format later without having to update each place it is used. To utilize Drupal’s date format system thedate.formatterservice can be used. Unfortunately, Drupal’s date formatter cannot handle date objects but works with timestamps instead. It is not used above because it would be more verbose and introduce new concepts, such as services and dependency injection, even though it would be the preferred implementation. For reference, the respective parts ofEventListBuilder.phpwould then be:
-
Add the following use statement to the
Eventclass: -
Replace the value of the
list_builderkey in thehandlerssection of the attributes insrc/Entity/Event.phpwithEventListBuilder::class. -
Rebuild caches
Run
drush cache:rebuild -
Visit
/admin/content/eventsNote that the entity list now shows the event title and date.
-
Delete an event again
Visit
/admin/content/events/manage/2/deleteand press Delete.Note that this time you are redirected to the administrative event listing. The redirect to the front page that happened above is only a fallback in case no
collectionroute exists.
4.3. Add an administrative view
Section titled “4.3. Add an administrative view”While a specialized entity list builder has the benefit of being re-usable one can also take advantage of Drupal’s Views module to create an administrative listing of events.
-
Add the following use statement to the
Eventclass: -
Add the following to the
handlerssection of the annotation insrc/Entity/Event.php:Note that the views data that is provided by the default views data handler is partially incomplete so - in particular when dealing with date or entity reference fields - using Views for entity listings should be evaluated carefully.
-
Rebuild caches
Run
drush cache:rebuild -
Add an Events view to replace the list builder
-
Select Show: Event
-
Check Create a page
-
Use
admin/content/eventsas the PathThis will make Views replace the previously existing collection route.
Note that the path is entered without a leading slash in Views.
-
Select Display format: Table
-
Press Save and edit
-
Click on Settings in the Format section and check Sortable for the Title, Date and Published fields, as well as Enable Drupal style “sticky” table headers (Javascript) and Show the empty text in the table
-
Add an empty text field to the No results behavior area
-
Add Date and Published fields and select _Output format: ✔ / ✖ for the Published field
-
Add Link to edit Event and Link to delete Event fields and check the Exclude from display checkbox
-
Add a Dropbutton field for the operations and enable the edit and delete links
-
Views provides a number of features that increase the usability of administrative listings when compared to the stock entity list builder. These include:
-
Exposed filters
-
Using formatters for fields
This allows using Drupal’s date formatting system for the date field (as discussed above), for example, and using check (✔) and cross (✖) marks for the published field.
-
A click-sortable table header
-
An Ajax-enabled pager
-
A “sticky” table header
-
The ability to add a menu link or local task for the view from the user interface. (Note that adding a local task does not work in this case as it conflicts with the overriding of the list-builder route.)