CloneBehavior example

$this->themeLifecycleService->refreshTheme($bundle$this->context);

        $themeEntity = $this->getTheme($bundle, true);
        $childId = Uuid::randomHex();

        static::assertInstanceOf(ThemeCollection::class$themeEntity->getDependentThemes());
        // check if we have no dependent Themes         static::assertEquals(0, $themeEntity->getDependentThemes()->count());

        // clone theme and make it child         $this->themeRepository->clone($themeEntity->getId()$this->context, $childIdnew CloneBehavior([
            'technicalName' => null,
            'name' => 'Cloned theme',
            'parentThemeId' => $themeEntity->getId(),
        ]));

        // refresh theme to get child         $themeEntity = $this->getTheme($bundle, true);

        $themeMedia = $themeEntity->getMedia();
        static::assertInstanceOf(MediaCollection::class$themeMedia);
        $ids = $themeMedia->getIds();

        
$product = (new ProductBuilder($ids, 'parent'))
            ->price(100)
            ->variant(
                (new ProductBuilder($ids, 'child'))
                    ->translation($ids->get('language'), 'description', 'update')
                    ->build()
            )
            ->build();

        $this->getContainer()->get('product.repository')->create([$product], Context::createDefaultContext());

        $behavior = new CloneBehavior(['productNumber' => 'new-parent'], false);

        $this->getContainer()->get('product.repository')->clone(
            $ids->get('parent'),
            Context::createDefaultContext(),
            $ids->get('new-parent'),
            $behavior
        );

        $behavior = new CloneBehavior([
            'parentId' => $ids->get('new-parent'),
            'productNumber' => 'new-child',
        ],
$products = $this->productRepository->searchIds($criteria$this->context);
        static::assertTrue($products->has($productId));

        /** @var array<string, array<int, mixed>> $clonedAffected */
        $clonedAffected = $this->versionManager->clone(
            $this->getContainer()->get(ProductDefinition::class),
            $productId,
            Uuid::randomHex(),
            Uuid::randomHex(),
            WriteContext::createFromContext($this->context),
            new CloneBehavior()
        );

        $clonedProduct = $clonedAffected['product'][0];
        static::assertInstanceOf(EntityWriteResult::class$clonedProduct);

        $clonedProductId = $clonedProduct->getPayload()['id'];
        $clonedManyToOneId = $clonedProduct->getPayload()['manyToOneId'];
        static::assertNotEmpty($clonedProductId);
        static::assertSame($extendableId$clonedManyToOneId);
    }

    
private readonly DefinitionInstanceRegistry $definitionRegistry,
        private readonly DecoderInterface $serializer,
        private readonly RequestCriteriaBuilder $criteriaBuilder,
        private readonly EntityProtectionValidator $entityProtectionValidator,
        private readonly AclCriteriaValidator $criteriaValidator
    ) {
    }

    #[Route(path: '/api/_action/clone/{entity}/{id}', name: 'api.clone', methods: ['POST'], requirements: ['version' => '\d+', 'entity' => '[a-zA-Z-]+', 'id' => '[0-9a-f]{32}'])]     public function clone(Context $context, string $entity, string $id, Request $request): JsonResponse
    {
        $behavior = new CloneBehavior(
            $request->request->all('overwrites'),
            $request->request->getBoolean('cloneChildren', true)
        );

        $entity = $this->urlToSnakeCase($entity);

        $definition = $this->definitionRegistry->getByEntityName($entity);
        $missing = $this->validateAclPermissions($context$definition, AclRoleDefinition::PRIVILEGE_CREATE);
        if ($missing) {
            throw ApiException::missingPrivileges([$missing]);
        }

        
'manufacturer' => ['name' => 'test'],
            'tags' => $tags,
            'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 10, 'net' => 5, 'linked' => false]],
        ];

        $repository = $this->getContainer()->get('product.repository');
        $context = Context::createDefaultContext();

        $repository->create([$data]$context);
        $newId = Uuid::randomHex();

        $behavior = new CloneBehavior(['productNumber' => 'abc']);
        $result = $repository->clone($id$context$newId$behavior);
        static::assertInstanceOf(EntityWrittenContainerEvent::class$result);

        $written = $result->getEventByEntityName(ProductDefinition::ENTITY_NAME);
        static::assertNotNull($written);
        static::assertCount(1, $written->getIds());
        static::assertContains($newId$written->getIds());

        $entities = $repository->search(new Criteria([$id$newId])$context);

        static::assertCount(2, $entities);
        
$newId ??= Uuid::randomHex();
        if (!Uuid::isValid($newId)) {
            throw new InvalidUuidException($newId);
        }

        $affected = $this->versionManager->clone(
            $this->definition,
            $id,
            $newId,
            $context->getVersionId(),
            WriteContext::createFromContext($context),
            $behavior ?? new CloneBehavior()
        );

        $event = EntityWrittenContainerEvent::createWithWrittenEvents($affected$context[], true);
        $this->eventDispatcher->dispatch($event);

        return $event;
    }

    /** * @return TEntityCollection */
    
$versionId = $versionId ?? Uuid::randomHex();
        $versionData = ['id' => $versionId];

        if ($name) {
            $versionData['name'] = $name;
        }

        $context->scope(Context::SYSTEM_SCOPE, function D$context) use ($versionData): void {
            $this->entityWriter->upsert($this->versionDefinition, [$versionData]$context);
        });

        $affected = $this->cloneEntity($definition$id$id$versionId$contextnew CloneBehavior());

        $versionContext = $context->createWithVersionId($versionId);

        $event = EntityWrittenContainerEvent::createWithWrittenEvents($affected$versionContext->getContext()[]);
        $this->eventDispatcher->dispatch($event);

        $this->writeAuditLog($affected$context$versionId, true);

        return $versionId;
    }

    
private function cloneDefaultFlow(): void
    {
        $flowId = $this->flowRepository
            ->searchIds(
                (new Criteria())
                    ->addFilter(new EqualsFilter('name', 'Deliver ordered product downloads')),
                $this->salesChannelContext->getContext()
            )->firstId();
        static::assertNotNull($flowId);

        $behavior = new CloneBehavior([
            'eventName' => CheckoutOrderPlacedEvent::EVENT_NAME,
        ]);
        $this->flowRepository->clone($flowId$this->salesChannelContext->getContext(), null, $behavior);
    }

    private function addProduct(string $productId, int $quantity, Cart $cart, CartService $cartService, SalesChannelContext $context): Cart
    {
        $factory = new ProductLineItemFactory(new PriceDefinitionFactory());
        $product = $factory->create(['id' => $productId, 'referencedId' => $productId, 'quantity' => $quantity]$context);

        return $cartService->add($cart$product$context);
    }
Home | Imprint | This part of the site doesn't use cookies.