How do I modify a custom entity delete form?

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.

SymfonyComponentRoutingExceptionMissingMandatoryParametersException: Some mandatory parameters are missing ("individual") to generate a URL for route "entity.possession.edit_form". in DrupalCoreRoutingUrlGenerator->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?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How do I modify a custom entity delete form?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.