MissingMandatoryParametersException is not caught in redirect_entity_delete

Created on 7 October 2022, about 2 years ago
Updated 21 August 2024, 3 months ago

Problem/Motivation

In redirect_entity_delete() the Symfony\Component\Routing\Exception\RouteNotFoundException is caught with the following comment:

  // This can happen if a module incorrectly defines a link template, ignore
  // such errors.

However, calling the Url::getInternalPath() in the try block can also produce a

Symfony\Component\Routing\Exception\MissingMandatoryParametersException

Steps to reproduce

I have such a case with the group_permissions module where the canonical route of the group_permission entity expects the group entity and not the group_permission entity.

1. Enable group_permissions and redirect modules
2. Create a group type 'Test' on /admin/group/types/add
3. Add a group on /group/add/test
4. Select an arbitrary permission on /group/1/permissions and save the page (this step is to create the group_permission entity)
5. Try to delete the group on /group/1/delete

Result:

Drupal\Core\Entity\EntityStorageException: Some mandatory parameters are missing ("group") to generate a URL for route "entity.group_permission.canonical". in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 759 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\Core\Routing\UrlGenerator->getInternalPathFromRoute('entity.group_permission.canonical', Object, Array) (Line: 132)
Drupal\Core\Routing\UrlGenerator->getPathFromRoute('entity.group_permission.canonical', Array) (Line: 68)
Drupal\Core\Render\MetadataBubblingUrlGenerator->getPathFromRoute('entity.group_permission.canonical', Array) (Line: 802)
Drupal\Core\Url->getInternalPath() (Line: 78)
redirect_entity_delete(Object)
call_user_func_array(Object, Array) (Line: 426)
Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object, 'redirect') (Line: 405)
Drupal\Core\Extension\ModuleHandler->invokeAllWith('entity_delete', Object) (Line: 433)
Drupal\Core\Extension\ModuleHandler->invokeAll('entity_delete', Array) (Line: 251)
Drupal\Core\Entity\EntityStorageBase->invokeHook('delete', Object) (Line: 903)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('delete', Object) (Line: 496)
Drupal\Core\Entity\EntityStorageBase->delete(Array) (Line: 751)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete(Array) (Line: 350)
Drupal\Core\Entity\EntityBase->delete() (Line: 16)
group_permissions_group_delete(Object)
call_user_func_array(Object, Array) (Line: 426)
...

Proposed resolution

Also catch the MissingMandatoryParametersException in the redirect delete hook.

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Fixed

Version

1.0

Component

Code

Created by

🇧🇪Belgium keszthelyi Brussels

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India sumit-k

    After replicating the issue, I can confirm that Patch #2 resolves the issue satisfactorily. Additionally, I've introduced an alternative method to handle straightforward exceptions, which has proven effective in addressing the problem in my scenario.

    function redirect_entity_delete(EntityInterface $entity) {
      try {
        if ($entity->getEntityType()->hasLinkTemplate('canonical') && $entity->toUrl('canonical')->isRouted()) {
    
           // Generate the canonical URL after setting the required parameters.
           $canonical_url = $entity->toUrl('canonical');
          
           // Check if the canonical URL has the required parameters set.
          try {
            if ($canonical_url->getRouteParameters()) {
              redirect_delete_by_path('internal:/' . $entity->toUrl('canonical')->getInternalPath());
            }
          }
          catch (Exception $e) {
            // This can happen if some mandatory parameters are missing.
          }
          redirect_delete_by_path('entity:' . $entity->getEntityTypeId() . '/' . $entity->id());
        }
      }
      catch (RouteNotFoundException $e) {
        // This can happen if a module incorrectly defines a link template, ignore
        // such errors.
      }
    }
  • First commit to issue fork.
  • Status changed to RTBC 4 months ago
  • heddn Nicaragua

    Not sure what extra testing we want to do here. I tested this coming from 🐛 Error when removing a menu Needs work and it solved the problem perfectly.

  • Pipeline finished with Skipped
    4 months ago
    #247226
  • Status changed to Fixed 4 months ago
  • 🇨🇭Switzerland berdir Switzerland

    Merged.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024