eZ Community » Forums » eZ Platform » Remove translated content
expandshrink

Remove translated content

Remove translated content

Thursday 07 July 2016 2:04:21 pm - 6 replies

Hi

I have some content created in eng-GB and translated to por_PT. I now wish to remove the eng-GB without needing to recreate the whole object.

Does anyone have any tips on how to achieve this?

Cheers & thanks
Io

Wednesday 01 February 2017 9:25:52 pm

Hi,

Same question here, did you get this to work?

Vincent

Thursday 02 February 2017 8:12:50 am

I once made a small cli script to remove translations:

http://pastebin.com/w4643TXU

As always backup your data before messing with the db.

Thursday 02 February 2017 4:29:01 pm

Hi Andreas,

This scripts seems to work with ez 5.4, i'm looking for something similar but for ezplatform.

Thank you,
Vincent

Tuesday 07 March 2017 9:07:59 am

Hello Andreas and others,

Thank you for sharing your solution script!

We have re-implemented and expanded the solution to be a bit more flexible and reusable.

https://github.com/brookinsconsulting/bcchangesubtreelanguage

We hope this helps others ...
Cheers,
Heath

Tuesday 07 March 2017 11:06:25 am

Quote from Vincent GUYARD :

Hi,

Same question here, did you get this to work?

Vincent

Hi Vicent. Sorry, I only ready you now. I've finished the command for eZ Platform, used it in my test case with success, but I cannot assure you that it holds no bugs that might destroy data.

I'm posting it here, so that you can test it as well (please do backup first)

<?php
 
namespace IoSolinf\ToolsBundle\Command;
 
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
 
class RemoveTranslationCommand extends \Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand {
 
  /**
   * Configures the command
   */
  protected function configure() {
    $this->setName('arestel:RemoveTranslation')
            ->addOption(
                    'contentId', null, InputOption::VALUE_REQUIRED, 'Which object to content operate', null
            )
            ->addOption(
                    'removeLanguage', null, InputOption::VALUE_REQUIRED, 'Language code of translation to remove', null
            )
    ;
  }
 
  /**
   * Executes the command
   * @param InputInterface $input
   * @param OutputInterface $output
   */
  protected function execute(InputInterface $input, OutputInterface $output) {
    $output->writeln("***********************************");
    $output->writeln("Removing Content Object Translation");
    $output->writeln("***********************************");
 
    $contentId = $input->getOption('contentId');
    $removeLanguage = $input->getOption('removeLanguage');
 
    $repository = $this->getContainer()->get('ezpublish.api.repository');
 
    $repository->setCurrentUser($repository->getUserService()->loadUser(14));
 
    /* @var $contentService ContentService */
    $contentService = $repository->getContentService();
 
    /* @var $contentTypeService ContentTypeService */
    $contentTypeService = $repository->getContentTypeService();
 
    /* @var $locationService LocationService */
    $locationService = $repository->getLocationService();
 
    /* @var $versionInfo VersionInfo */
    $versionInfo = $contentService->loadVersionInfoById($contentId);
    $languageCodes = implode($versionInfo->languageCodes, ', ');
 
    /* @var $content Content */
    $content = $contentService->loadContent($contentId);
 
    /* @var $location Location */
    $location = $locationService->loadLocation($content->contentInfo->mainLocationId);
 
    /* @var $contentType ContentType */
    $contentType = $contentTypeService->loadContentType($content->contentInfo->contentTypeId);
    $contentTypeName = $contentType->getName("eng-GB");
 
    $output->writeln("Processing <info>{$versionInfo->names[$versionInfo->languageCodes[0]]}</info>");
    $output->writeln("Content of type <info>$contentTypeName</info> is translated in <info>$languageCodes</info>");
 
    if (!in_array($removeLanguage, $versionInfo->languageCodes)) {
      $output->writeln("<error>Error</error>: No translation for <info>$removeLanguage</info> found.");
      return;
    }
 
    $newMainLanguage = false;
    foreach ($versionInfo->languageCodes as $languageCode)
      if (!$newMainLanguage && $languageCode != $removeLanguage)
        $newMainLanguage = $languageCode;
    if (!$newMainLanguage) {
      $output->writeln("<error>Error</error>: No translations left if <info>$removeLanguage</info> is removed.");
      return;
    }
 
    $helper = $this->getHelper('question');
    $question = new ConfirmationQuestion("Remove <info>$removeLanguage</info> translation?", false);
 
    if (!$helper->ask($input, $output, $question)) {
      return;
    }
 
    /* @var $locationCreateStruct LocationCreateStruct */
    $locationCreateStruct = $locationService->newLocationCreateStruct($location->parentLocationId);
 
    /* @var $contentCreateStruct ContentCreateStruct */
    $contentCreateStruct = $contentService->newContentCreateStruct($contentType, $newMainLanguage);
 
    foreach ($content->fields as $fieldIdentifier => $field) {
      foreach ($field as $languageCode => $fieldValue) {
        if ($languageCode != $removeLanguage) {
          $contentCreateStruct->setField($fieldIdentifier, $fieldValue, $languageCode);
        }
      }
    }
 
    $draft = $contentService->createContent($contentCreateStruct, array($locationCreateStruct));
    $newContent = $contentService->publishVersion($draft->versionInfo);
    $newLocation = $locationService->loadLocation($newContent->contentInfo->mainLocationId);
 
    $locationService->swapLocation($location, $newLocation);
 
    $content = $contentService->loadContent($contentId);
    
    $contentService->deleteContent($content->contentInfo);
    $output->writeln("Job's done!");
  }
}

Cheers
Io

Wednesday 08 March 2017 7:43:48 am

Hello Io Sol Inf and others,

Thank you for sharing your solution command!

We have re-implemented and expanded the command to be a bit more flexible, reusable and usable out of the box.

https://github.com/brookinsconsulting/BcRemoveTranslationBundle

Our solution is also can be installed via composer!

We hope this helps others ...

Cheers,
Heath

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from