Ibexa DXP Discussions

Community discussion forum for developers working with Ibexa DXP

Change the parentLocation of a draft

I’m looking for how to change the parentLocation of a draft.

$newParentLocation = $this->locationService->loadLocation($newParentLocationId);
if ($content->contentInfo->isDraft()) {
    $previewLocation = $this->previewLocationProvider->loadMainLocationByContent($content);
    // Location {#2971 ▼
    //     #contentInfo: ContentInfo {#2981 ▶}
    //     #path: null
    //     #id: null
    //     +status: 0
    //     #priority: null
    //     #hidden: null
    //     #invisible: null
    //     #explicitlyHidden: null
    //     #remoteId: null
    //     #parentLocationId: 8753
    //     #pathString: "/1/2/54/672/8753/x/"
    //     #depth: 5
    //     #sortField: null
    //     #sortOrder: null
    //     #content: Content {#3022 ▶}
    $this->locationService->moveSubtree($previewLocation, $newParentLocation); // NotFoundException Could not find 'location' with identifier '0'

Below description is only for new created draft!

You can’t use the loadLocation() as the location doesn’t exist. You can check the the ezcontentobject_tree. The question is where to find the information about the future parent location :wink: Well, you can take a look to the eznode_assignment. You will find the parent_node Column and this is where the information is saved. The bad news is that there is not API end point to change it. It happens in the LocationService->createLocation() but you can’t access it because this method load an existing location. You can’t also use the SPI layer to do so.
So, the only proper way is to create your API endpoint. I did it once to display some information bout the draft and to allow other users to edit it. You can get some insight here https://github.com/arfaram/ezplatform-drafts-tools . may be is a good Idea to reuse and extend it :wink: The UI Integration might be the challenge here not the API.

I added the first line in my previous comment because in your case the location already exist.
You could maybe create a new location and remove the main one (parentLocationId in your dump)

        $contentInfo = $this->contentService->loadContentInfo( 546 );

        $locationCreateStruct = new LocationCreateStruct();
        $locationCreateStruct->parentLocationId = 503; //New Parent

        $this->locationService->createLocation($contentInfo, $locationCreateStruct);
        $mainLocation = $this->locationService->loadLocation(515); //location to remove

Is just an example with some Ids. It doesn’t matter about the current content status (published or draft)