6. Adding additional fields
Now that our basic implementation of Events is functional and usable from the user interface, we can add some more fields. This is both to recap the above chapters and to show some additional features that are often used for content entities.
6.1. Add the field definitions
Section titled “6.1. Add the field definitions”-
Add the following use statements to
src/Entity/Event.php: -
Add
, EntityChangedInterfaceto theimplementspart of the class declaration of theEventclass -
Add
, EntityChangedTraitto theusepart inside of theEventclass -
Add the following to the
baseFieldDefinitions()method insrc/Entity/Event.phpabove thereturnstatement:
More information on the above
-
Multiple-value fields:
Fields can be allowed to have multiple values by changing the cardinality of the field definition. If an unlimited amount of field values should be possible, the constant
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITEDshould be used as the cardinality value. -
Changed time tracking:
When an entity that supports changed time tracking is being saved, Drupal checks whether the entity has been updated by someone else in the meantime so that the changes do not get overwritten.
Note that the and changed field is not exposed on the form.
6.2. Add additional field methods
Section titled “6.2. Add additional field methods”-
Add the following to the use statements at the top of
src/Entity/Event.php: -
Add the following to the
Eventclass insrc/Entity/Event.php:More information on the above
-
Field item lists:
-
Object traversal:
-
6.3. Add validation constraints
Section titled “6.3. Add validation constraints”-
Add a
src/Plugindirectory -
Add a
src/Plugin/Validationdirectory -
Add a
src/Plugin/Validation/Constraintdirectory -
Add a
src/Plugin/Validation/Constraint/AttendeeCountConstraint.phpwith the following: -
Add a
src/Plugin/Validation/Constraint/AttendeeCountConstraintValidator.phpwith the following: -
Add a
src/Plugin/Validation/Constraint/UniqueAttendeesConstraint.phpwith the following: -
Add a
src/Plugin/Validation/Constraint/UniqueAttendeesConstraintValidator.phpwith the following: -
Add the following to the
$fields['attendees']section of thebaseFieldDefinitions()method of theEventclass before the semicolon:
6.4. Implement the computed field
Section titled “6.4. Implement the computed field”-
Add a
src/Fielddirectory -
Add a
src/Field/RemainingFieldItemList.phpwith the following: -
Add the following use statements to
src/Entity/Event.php: -
Add the following to the
$fields['remaining']section of thebaseFieldDefinitions()method of theEventclass before the semicolon:
The entire Event.php file at this point
6.5. Install the fields
Section titled “6.5. Install the fields”-
Run
drush entity:updates -
Note that the
{event__attendees}table was created andmaximumandchangedfields have been created in the{event}table.
Note that there is no remaining column. There is no path column because
path aliases are stored separately in the {url_alias} table.
-
Try out the new methods
Run the following PHP code:
-
Try out the new fields in the user interface
Visit
/admin/content/events/manage/4and add a path and an attendee.Verify that the path alias gets created correctly and that the attendees are displayed correctly.
-
Verify that the remaining number of attendees is displayed and updated correctly.
-
Verify that the constraints prevent the creation of invalid events via the user interface.
The event entities are feature complete for our purposes as of now.