eZ Platform Discussions

Delete ContentType Draft Command

version22

#1

Hello hello!

I’m looking to make an order that removes all drafts from ContantType.

(
I need it because I often make this mistake with Kaliop Migration Bundle

Migration aborted! Reason: Error in execution of step 1: Argument ‘$contentType’ has a bad state: Draft of the ContentType already exists in file /var/www/html/project/ezplatform/vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/Repository/ContentTypeService.php line 962

)

I can get the draft back. But I don’t know what to do with it after…

<?php

namespace Smile\ToolsBundle\Command;

use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use eZ\Publish\SPI\Persistence\Content\Type as SPIContentType;

class DeleteContentTypeDraftCommand extends SmileContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('smile:tools:delete-contenttype-draft')
            ->setDescription('Supprime les draft des ContentType.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->logAsSuperAdmin();
        $contentType = $this->getRepository()->getContentTypeService()->loadContentTypeByIdentifier('breve');
        $this->deleteDraft($contentType);
        $output->writeln('END');
    }

    private function deleteDraft(ContentType $contentType)
    {
        try {
            /** @var \eZ\Publish\SPI\Persistence\Content\Type $draft */
            $draft = $this->getContentTypeHandler()->load(
                $contentType->id,
                SPIContentType::STATUS_DRAFT
            );

            $this->getContentTypeHandler()-> ??? deleteDraft($draft); ???
            return true;
        } catch (\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) {
            // Il n'y a simplement pas de draft. Donc tous va bien.
            return false;
        }
    }

    /**
     * @return \eZ\Publish\SPI\Persistence\Content\Type\Handler
     */
    private function getContentTypeHandler()
    {
        return $this->getContainer()->get('ezpublish.spi.persistence.cache.contentTypeHandler');
    }
}

If someone can give me a little help :slight_smile:
And above all… Have a good weekend to all :wink:


#2

Ha… I thought I had found…

vendor/ezsystems/ezpublish-kernel/eZ/Publish/API/Repository/ContentTypeService.php

    /**
     * Delete a Content Type object.
     *
     * Deletes a content type if it has no instances. If content type in state STATUS_DRAFT is
     * given, only the draft content type will be deleted. Otherwise, if content type in state
     * STATUS_DEFINED is given, all content type data will be deleted.
     *
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
     */
    public function deleteContentType(ContentType $contentType);

Mais non…

    private function deleteDraft(ContentType $contentType)
    {
        try {
            /** @var \eZ\Publish\SPI\Persistence\Content\Type $draft */
            $draft = $this->getContentTypeHandler()->load(
                $contentType->id,
                SPIContentType::STATUS_DRAFT
            );
            $this->getRepository()->getContentTypeService()->deleteContentType($contentType);
            return true;
        } catch (\eZ\Publish\API\Repository\Exceptions\NotFoundException $e) {
            // Il n'y a simplement pas de draft. Donc tous va bien.
            return false;
        }
    }

Argument ‘$contentTypeId’ has a bad state: ContentType with given id still has content instances and therefore can’t be deleted


#3

I bypassed my pb by publishing my drafts instead of deleting them…

It’s not ideal because it can publish unexpected things…

        $contentTypeDraft = $contentTypeService->loadContentTypeDraft($contentType->id);
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);