Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

DeleteContentTranslationCommand.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Command;
8
9 use eZ\Publish\API\Repository\Repository;
10 use eZ\Publish\API\Repository\Values\Content\ContentInfo;
11 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Question\ChoiceQuestion;
18 use Symfony\Component\Console\Question\ConfirmationQuestion;
19 use Exception;
20
24 class DeleteContentTranslationCommand extends Command
25 {
27 private $repository;
28
30 private $contentService;
31
33 private $input;
34
36 private $output;
37
39 private $questionHelper;
40
41 public function __construct(Repository $repository)
42 {
43 parent::__construct(null);
44 $this->repository = $repository;
45 }
46
50 protected function configure()
51 {
52 $this
53 ->setName('ezplatform:delete-content-translation')
54 ->addArgument('content-id', InputArgument::REQUIRED, 'Content Object Id')
55 ->addArgument(
56 'language-code',
57 InputArgument::REQUIRED,
58 'Language code of the Translation to be deleted'
59 )
60 ->addOption(
61 'user',
62 'u',
63 InputOption::VALUE_OPTIONAL,
64 'eZ Platform username (with Role containing at least Content policies: read, versionread, edit, remove, versionremove)',
65 'admin'
66 )
67 ->setDescription('Delete Translation from all the Versions of a Content Item');
68 }
69
70 protected function initialize(InputInterface $input, OutputInterface $output)
71 {
72 parent::initialize($input, $output);
73 $this->input = $input;
74 $this->output = $output;
75 $this->questionHelper = $this->getHelper('question');
76 $this->contentService = $this->repository->getContentService();
77
78 $this->repository->getPermissionResolver()->setCurrentUserReference(
79 $this->repository->getUserService()->loadUserByLogin($input->getOption('user'))
80 );
81 }
82
86 protected function execute(InputInterface $input, OutputInterface $output)
87 {
88 $contentId = (int) ($input->getArgument('content-id'));
89 $languageCode = $input->getArgument('language-code');
90
91 if ($contentId === 0) {
92 throw new InvalidArgumentException(
93 'content-id',
94 'Content Object Id has to be an integer'
95 );
96 }
97
98 $this->output->writeln(
99 '<comment>**NOTE**: Make sure to run this command using the same SYMFONY_ENV setting as your eZ Platform installation does</comment>'
100 );
101
102 $versionInfo = $this->contentService->loadVersionInfoById($contentId);
103 $contentInfo = $versionInfo->contentInfo;
104
105 $this->repository->beginTransaction();
106 try {
107 if ($contentInfo->mainLanguageCode === $languageCode) {
108 $contentInfo = $this->promptUserForMainLanguageChange(
109 $contentInfo,
110 $languageCode,
111 // allow to change Main Translation to only those existing in the last Version
112 $versionInfo->languageCodes
113 );
114 }
115
116 // Confirm operation
117 $contentName = "#{$contentInfo->id} ($contentInfo->name)";
118 $question = new ConfirmationQuestion(
119 "Are you sure you want to delete {$languageCode} Translation from the Content {$contentName}? This operation is permanent. [y/N] ",
120 false
121 );
122 if (!$this->questionHelper->ask($this->input, $this->output, $question)) {
123 // Rollback any cleanup change (see above)
124 $this->repository->rollback();
125 $this->output->writeln('Reverting and aborting.');
126
127 return;
128 }
129
130 // Delete Translation
131 $output->writeln(
132 "<info>Deleting {$languageCode} Translation of the Content {$contentName}</info>"
133 );
134 $this->contentService->deleteTranslation($contentInfo, $languageCode);
135
136 $output->writeln('<info>Translation deleted</info>');
137
138 $this->repository->commit();
139 } catch (Exception $e) {
140 $this->repository->rollback();
141 throw $e;
142 }
143 }
144
154 private function promptUserForMainLanguageChange(
155 ContentInfo $contentInfo,
156 $languageCode,
157 array $lastVersionLanguageCodes
158 ) {
159 $contentName = "#{$contentInfo->id} ($contentInfo->name)";
160 $this->output->writeln(
161 "<comment>The specified Translation '{$languageCode}' is the Main Translation of the Content {$contentName}. It needs to be changed before removal.</comment>"
162 );
163
164 // get main Translation candidates w/o Translation being removed
165 $mainTranslationCandidates = array_filter(
166 $lastVersionLanguageCodes,
167 function ($versionLanguageCode) use ($languageCode) {
168 return $versionLanguageCode !== $languageCode;
169 }
170 );
171 if (empty($mainTranslationCandidates)) {
172 throw new InvalidArgumentException(
173 'language-code',
174 "The last Version of the Content {$contentName} has no other Translations beside the main one"
175 );
176 }
177 $question = new ChoiceQuestion(
178 "Set the Main Translation of the Content {$contentName} to:",
179 array_values($mainTranslationCandidates)
180 );
181
182 $newMainLanguageCode = $this->questionHelper->ask($this->input, $this->output, $question);
183 $this->output->writeln(
184 "<info>Updating Main Translation of the Content {$contentName} to {$newMainLanguageCode}</info>"
185 );
186
187 $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
188 $contentMetadataUpdateStruct->mainLanguageCode = $newMainLanguageCode;
189
190 return $this->contentService->updateContentMetadata(
191 $contentInfo,
192 $contentMetadataUpdateStruct
193 )->contentInfo;
194 }
195 }
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand
Console Command which deletes a given Translation from all the Versions of a given Content Item.
Definition: DeleteContentTranslationCommand.php:25
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\execute
execute(InputInterface $input, OutputInterface $output)
{}
Definition: DeleteContentTranslationCommand.php:86
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\$contentService
$contentService
Definition: DeleteContentTranslationCommand.php:30
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\__construct
__construct(Repository $repository)
Definition: DeleteContentTranslationCommand.php:41
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\$output
$output
Definition: DeleteContentTranslationCommand.php:36
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\promptUserForMainLanguageChange
promptUserForMainLanguageChange(ContentInfo $contentInfo, $languageCode, array $lastVersionLanguageCodes)
Interact with user to update main Language of a Content Object.
Definition: DeleteContentTranslationCommand.php:154
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\$input
$input
Definition: DeleteContentTranslationCommand.php:33
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\$repository
$repository
Definition: DeleteContentTranslationCommand.php:27
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\configure
configure()
{}
Definition: DeleteContentTranslationCommand.php:50
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\initialize
initialize(InputInterface $input, OutputInterface $output)
Definition: DeleteContentTranslationCommand.php:70
eZ\Bundle\EzPublishCoreBundle\Command\DeleteContentTranslationCommand\$questionHelper
$questionHelper
Definition: DeleteContentTranslationCommand.php:39
eZ\Publish\API\Repository\Values\Content\ContentInfo
Definition: API/Repository/Values/Content/ContentInfo.php:31
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Bundle\EzPublishCoreBundle\Command
Definition: CheckURLsCommand.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.