Error if there are entities in a comparison, that are deleted in the meanwhile

Created on 21 July 2023, over 1 year ago

Adding entities to a comparison list, which are getting deleted afterwards, results in an error while displaying the comparison page:

Error: Call to a member function hasTranslation() on null in Drupal\entity_comparison\Controller\EntityComparisonController->compare() (line 354 of modules/contrib/entity_comparison/src/Controller/EntityComparisonController.php).

There has been a similar issue in the past, which has been closed without any update: https://www.drupal.org/project/entity_comparison/issues/3042221

The problem is, that all entities in the comparison are being loaded and then there is a missing check if the entity still exists after the load.

I will try to provide a patch for that.

🐛 Bug report
Status

Needs review

Version

4.0

Component

Code

Created by

🇨🇭Switzerland megadesk3000

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

Comments & Activities

  • Issue created by @megadesk3000
  • 🇨🇭Switzerland megadesk3000

    I added a patch, that does the following:

    In EntityComparisonController::compare() a check has been added after the Entity has been loaded.

          // Go through entities.
          foreach ($entity_comparison_list[$entity_type][$bundle_type][$entity_comparison_id] as $entity_id) {
            // Get entity.
            $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id);
            // First check if the entity still exists before accessing methods on it.
            if (!$entity) {
              // Remove the entity from the list, since it does not exist.
              $this->processRequest($entity_comparison, $entity_id);
              continue;
            }
            if ($entity->hasTranslation($this->languageManager->getCurrentLanguage()->getId())) {
              $entity = $entity->getTranslation($this->languageManager->getCurrentLanguage()->getId());
            }
    

    If the entity does not exist, it gets removed from the entity comparison list as well automatically.

  • @megadesk3000 opened merge request.
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
Production build 0.71.5 2024