isLocalizedData($data)) { return $this->processLocalizedData($data, $operation, $uriVariables, $context); } if ($operation instanceof DeleteOperationInterface) { return $this->removeProcessor->process($data, $operation, $uriVariables, $context); } return $this->persistProcessor->process($data, $operation, $uriVariables, $context); } private function processLocalizedData( LocalizedEntityInterface $data, Operation $operation, array $uriVariables = [], array $context = [], ): mixed { $languages = $this->getContentLanguages($context); if ($operation instanceof DeleteOperationInterface) { if ($this->isLocalizedRequest($context)) { return $this->deleteLocalizedContent($data, $operation, $uriVariables, $context, $languages); } return $this->removeProcessor->process($data, $operation, $uriVariables, $context); } $language = $languages[0] ?? $this->localizationService->getDefaultLanguage(); $data->setTranslatableLocale($language); return $this->persistProcessor->process($data, $operation, $uriVariables, $context); } private function deleteLocalizedContent( LocalizedEntityInterface $data, Operation $operation, array $uriVariables = [], array $context = [], array $languages = [], ) { $translationRepository = $this->entityManager->getRepository(Translation::class); if (!\array_diff($data->getLocales(), $languages)) { throw new \Exception('Cannot leave empty resource. Must have at least one available localization.'); } $nonLocalizedLanguages = \array_diff($languages, $data->getLocales()); if (!empty($nonLocalizedLanguages)) { throw new \Exception(\sprintf( "Could not find requested localizations '%s' of the resource.", \join(', ', $nonLocalizedLanguages) )); } foreach ($languages as $language) { $translations = $translationRepository->findBy([ 'locale' => $language, 'objectClass' => $data::class, 'foreignKey' => $data->getId(), ]); foreach ($translations as $translation) { $this->entityManager->remove($translation); } $data->removeLocale($language); } $this->entityManager->flush(); return $this->persistProcessor->process($data, $operation, $uriVariables, $context); } }