3. Manipulating entities through forms
3.1. Add the routes
Section titled “3.1. Add the routes”-
Add the following use statements to
src/Entity/Event.php: -
Add the following to the
handlerssection of the attributes insrc/Entity/Event.php:
-
Add the following to the
linkssection of the attributes insrc/Entity/Event.php:The entire
Event.phpfile at this point -
Rebuild caches
Run
drush cache:rebuild -
Visit
/admin/content/events/addNote that a route exists and a Save button is shown, but no actual form fields are shown.
-
Visit
/admin/content/events/manage/2Note that a route exists and Save and Delete buttons are shown, but no actual form fields are shown.
3.2. Configure fields for display
Section titled “3.2. Configure fields for display”-
Add the following to the
$fields['title']section of thebaseFieldDefinitions()method of theEventclass before the semicolon: -
Add the following to the
$fields['date']section of thebaseFieldDefinitions()method of theEventclass before the semicolon: -
Add the following to the
$fields['description']section of thebaseFieldDefinitions()method of theEventclass before the semicolon: -
Add the following before the
returnstatement of thebaseFieldDefinitions()method of theEventclass:
The entire Event.php file at this point
-
Rebuild caches
Run
drush cache:rebuild -
Add an event in the user interface
Visit
/admin/content/events/addNote that the form fields are displayed.
Enter a title, date and description and press Save.
Verify that the event was saved by checking that a new row was created in the
{event}table.Note that no message is displayed and no redirect is performed.
3.3. Add a specialized form
Section titled “3.3. Add a specialized form”-
Add a
src/Formdirectory -
Add a
src/Form/EventForm.phpfile with the following: -
Add the following use statement to
src/Entity/Event.phpand replace the value of the
addandeditannotation keys in the form handlers section of the annotation insrc/Entity/Event.phpwithEventForm::class. -
Rebuild caches
Run
drush cache:rebuild -
Edit an event in the user interface
Visit
/admin/content/events/manage/3Note that a route exists and form fields are displayed including proper default values.
Modify the title, date and description and published status and press Save.
Note that a message is displayed and a redirect is performed.
Verify that the values in the respective row in the
{event}table have been updated. Also note that the default values of the form fields are correct on the reloaded page. -
Delete an event in the user interface
Visit
/admin/content/events/manage/3/deleteNote that a route exists and a confirmation form is shown.
Press Delete.
Note that a message is shown and you are redirected to the front page.
Verify that the respective row in the
{event}table has been deleted.