Do not delete rows on translation add

Created on 26 February 2025, about 1 month ago

Problem/Motivation

When adds a translation the module deletes the row by the source id, this means that if the source has multiple translations, the original and the translations links will be lost.

  public function deleteSource(SourceInterface $source): bool {
    try {
      $this->database
        ->delete(self::ENTITY_MESH_TABLE)
        ->condition('type', $source->getType())
        ->condition('source_entity_type', $source->getSourceEntityType())
        ->condition('source_entity_id', $source->getSourceEntityId())
        ->execute();
    }
    catch (\Exception $e) {
      $this->logger->error($e->getMessage());
      return FALSE;
    }
    return TRUE;
  }

Steps to reproduce

Create a node with links and save it, review entity_mesh table.
Create a translation without links and review that entity_mesh table does not have links.

Proposed resolution

Delete links by source language.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

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

Merge Requests

Comments & Activities

  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    Ready to review

  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    Full trace:

    entity_mesh_entity_insert => entity_mesh_process_entity => $entity_mesh->processEntity($entity); => processTranslatableEntity => processEntityItem => $this->entityMeshRepository->saveSource($source); => if (!$this->deleteSource($source))

      public function processEntity(EntityInterface $entity) {
        // Check if entity is translatable.
        if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
          $this->processTranslatableEntity($entity);
          return;
        }
        $this->processEntityItem($entity);
      }
    
      protected function processTranslatableEntity(EntityInterface $entity) {
        // @phpstan-ignore-next-line
        $translations = $entity->getTranslationLanguages();
        $langcodes = array_keys($translations);
        foreach ($langcodes as $langcode) {
          // @phpstan-ignore-next-line
          $translation = $entity->getTranslation($langcode);
          if ($translation instanceof EntityInterface) {
            $this->processEntityItem($translation);
          }
        }
      }
    
      protected function processEntityItem(EntityInterface $entity) {
        $source = $this->createSourceFromEntity($entity);
        if (!$source instanceof SourceInterface) {
          return;
        }
        $this->entityMeshRepository->saveSource($source);
      }
    
      public function saveSource(SourceInterface $source): bool {
        // Remove source rows from the database to avoid duplicates.
        if (!$this->deleteSource($source)) {
          return FALSE;
        }
        if (!$this->insertSource($source)) {
          return FALSE;
        }
        return TRUE;
      }
    
      public function deleteSource(SourceInterface $source): bool {
        try {
          $this->database
            ->delete(self::ENTITY_MESH_TABLE)
            ->condition('type', $source->getType())
            ->condition('source_entity_type', $source->getSourceEntityType())
            ->condition('source_entity_id', $source->getSourceEntityId())
            ->execute();
        }
        catch (\Exception $e) {
          $this->logger->error($e->getMessage());
          return FALSE;
        }
        return TRUE;
      }
    
  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    Ready to review!!

  • 🇪🇸Spain lpeidro Madrid

    It works as expected. Thank you Edu, I merged the branch.

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

Production build 0.71.5 2024