From 58ac24db8f0bfc3d5786f6a25f274c42cbde5e48 Mon Sep 17 00:00:00 2001 From: naomi Date: Thu, 12 Apr 2018 13:42:52 +0200 Subject: [PATCH] Added route and controller for editing hat So that the person id is in the URL. --- modules/zencrm_entities/src/Entity/Hat.php | 2 +- src/Controller/HatController.php | 31 ++++++++++++++++++---- zencrm.routing.yml | 8 ++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/modules/zencrm_entities/src/Entity/Hat.php b/modules/zencrm_entities/src/Entity/Hat.php index f53428f..f1e73e9 100644 --- a/modules/zencrm_entities/src/Entity/Hat.php +++ b/modules/zencrm_entities/src/Entity/Hat.php @@ -52,7 +52,7 @@ use Drupal\user\UserInterface; * "canonical" = "/zencrm/hat/{hat}", * "add-page" = "/zencrm/hat/add", * "add-form" = "/zencrm/hat/add/{hat_type}", - * "edit-form" = "/zencrm/hat/{person}/{hat}/edit", + * "edit-form" = "/zencrm/hat/{hat}/edit", * "delete-form" = "/zencrm/hat/{hat}/delete", * "collection" = "/zencrm/hat", * }, diff --git a/src/Controller/HatController.php b/src/Controller/HatController.php index 9440acf..cfdd66c 100644 --- a/src/Controller/HatController.php +++ b/src/Controller/HatController.php @@ -10,10 +10,10 @@ use Drupal\Core\Controller\ControllerBase; class HatController extends ControllerBase { /** - * Hello. + * Displays a form for creating a hat. + * The type of hat and the person are prepopulated. * - * @return string - * Return Hello string. + * @return form for creating a hat */ public function createHatForPerson($person_id, $hat_type_id) { $values = array( @@ -21,13 +21,34 @@ class HatController extends ControllerBase { 'person' => $person_id ); - $node = \Drupal::entityTypeManager() + $hat = \Drupal::entityTypeManager() ->getStorage('hat') ->create($values); $form = \Drupal::entityTypeManager() ->getFormObject('hat', 'default') - ->setEntity($node); + ->setEntity($hat); + return \Drupal::formBuilder()->getForm($form); + } + + /** + * Displays a form for editing a hat. + * The reason it is here is that the URL needs to have the person id in it + * in order to filter the contact details entity reference view to only show ones for that person. + * (The intuitive way to bring this about - changing the edit route for the entity itself - causes problems with the delete route) + * + * @return form for editing a hat + */ + public function editHatForPerson($person_id, $hat_id) { + error_log("hjhjhjhj"); + + $hat = \Drupal::entityTypeManager() + ->getStorage('hat') + ->load($hat_id); + + $form = \Drupal::entityTypeManager() + ->getFormObject('hat', 'default') + ->setEntity($hat); return \Drupal::formBuilder()->getForm($form); } diff --git a/zencrm.routing.yml b/zencrm.routing.yml index e0335da..fc56f9d 100644 --- a/zencrm.routing.yml +++ b/zencrm.routing.yml @@ -6,6 +6,14 @@ zencrm.hat.create: requirements: _permission: 'access content' +zencrm.hat.edit: + path: '/zencrm/hat/{person_id}/{hat_id}/edit' + defaults: + _controller: '\Drupal\zencrm\Controller\HatController::editHatForPerson' + _title: 'Edit Hat' + requirements: + _permission: 'access content' + zencrm.contact_details.create: path: '/zencrm/contact_details/{person_id}/add' defaults: