I have a custom entity (Possession) which has a non-standard edit form. The route for the edit form is the following.
entity.possession.edit:
path: '/individual/{individual}/possession/{possession}/edit'
defaults:
_entity_form: 'possession.edit'
_title: 'Edit Possession'
requirements:
_permission: 'access administration pages'
options:
_admin_route: FALSE
parameters:
individual:
type: entity:individual
possession:
type: entity:possession
That seems to work fine. Instead, the delete form doesn't work. When I click on delete, I get the following exception.
Symfony\Component\Routing\ExceptionMissingMandatoryParametersException: Some mandatory parameters are missing ("individual") to generate a URL for route "entity.possession.edit_form". in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 182 of core/lib/Drupal/Core/Routing/UrlGenerator.php).
After a little bit of digging, I noticed that the cancel link on the delete form redirects users to the edit form, which needs those parameters. I ran into this problem with the List Builder page, but I just fixed it in the code for that page.
I tried to edit the PossessionDeleteForm.php file that was generated from Drupal Console. I added a buildForm()
method.
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$form['actions']['cancel']['#url'] = Url::fromRoute('entity.possession.edit_form', [
'possession' => $entity->id(),
'individual' => $entity->get('individual_id')->target_id,
]);
return $form;
}
If I use ksm()
to inspect the form, it seems it's setup correctly, but it doesn't change the thrown exception. I also tried adding the getCancelURL()
method to the class with a basic URL, but it didn't fix the error.
public function getCancelURL() {
return new Url('system.admin_content');
}
Next I tried the urlRouteParamters()
method in the entity class.
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'edit_form') {
$uri_route_parameters['individual'] = $this->get('individual_id')->target_id;
}
return $uri_route_parameters;
}
That also didn't work. I have no idea what else to do here.
What is the correct way to redirect users to the edit form, when deleting the entity is cancelled?